Hibernate批量插入如何工作? [英] How Hibernate Batch insert works?
问题描述
有人可以解释一下吗?
hibernate.jdbc.batch_size = 1000
和
if(i%100 = = 0&& i> 0){
session.flush();
session.clear();
}
一起工作吗? ...
Hibernate属性 hibernate.jdbc.batch_size
是一个方式为hibernate优化您的插入或更新statetment,而冲洗循环是关于内存耗尽。
没有批处理时,当您尝试保存一个实体hibernate fire 1 insert语句,因此如果你可以使用一个大集合,为每个保存hibernate fire 1语句
设想下面的代码块:
<$ (实体e:实体){
session.save(e); p $ p>
}
这里hibernate会在集合中为每个实体激发1个插入语句。如果你的集合中有100个元素,那么100个插入语句将被触发。
这种方法效率不高,主要有两个原因:
OutOfMemoryException
完成。
Hibernate使用第一个来控制批量中有多少实体。在封面下,Hibernate使用 java.sql.Statement.addBatch(...)
和 executeBatch()
方法。 / p>
所以hibernate.jdbc.batch_size会告诉hibernate在调用<$ c之前必须调用 addBatch()
多少次$ c $> executeBatch()。
/ p>
为了照顾内存,您必须定期刷新会话,这是刷新循环的目的。
当你写:
for(实体e:实体){
if(i% 100 == 0&& i> 0){
session.flush();
session.clear();
你要让hibernate刷新并清除会话每100个实体(你释放记忆)。
那么现在2之间的联系是什么?
为了达到最佳效果,您必须定义您的 jdbc.batch_size
和您的冲洗参数完全相同。
如果您定义的flush-param低于您选择的batch_size,那么hibernate会更频繁地刷新会话,所以它会创建一个小批量,直到它达到bb $ ch
,这是无效的。
当2是相同的hibernate只会执行批量的最佳大小,除了最后一个,如果集合的大小不是您batch_size的倍数。
您可以看到以下内容文章了解关于最后一点的更多详情
Can some one explain me how
hibernate.jdbc.batch_size=1000
and
if (i % 100 == 0 && i>0) {
session.flush();
session.clear();
}
together works ? ...
Hibernate property hibernate.jdbc.batch_size
is a way for hibernate to optimize your insert or update statetment whereas flushing loop is about memory exhaustion.
Without batchsize when you try to save an entity hibernate fire 1 insert statement, thus if you work with a big collection, for each save hibernate fire 1 statement
Imagine the following chunk of code :
for(Entity e : entities){
session.save(e);
}
Here hibernate will fire 1 insert statement per entity in your collection. if you have 100 elements in your collection so 100 insert statements will be fire. This approach is not very efficient for 2 main reasons:
- 1) You increase exponentially your 1st level cache and you'll probably finish soon with an
OutOfMemoryException
. - 2) You degrade performance due to network round trip for each statement.
hibernate.jdbc.batch_size and the flushing loop have 2 differents purposes but are complementary.
Hibernate use the first to control how many entities will be in batch. Under the cover Hibernate use java.sql.Statement.addBatch(...)
and executeBatch()
methods.
So hibernate.jdbc.batch_size tells hibernate how many times it have to call addBatch()
before calling executeBatch()
.
So setting this property doesn't prevent you of memory exhaution.
In order to take care of the memory you have to flush your session on a regular basis and this is the purpose of flushing loop.
When you write :
for(Entity e : entities){
if (i % 100 == 0 && i>0) {
session.flush();
session.clear();
}
}
you're telling hibernate to flush and clear the session every 100 entities (you release memory).
So now what is the link between the 2 ?
In order to be optimal you have to define your jdbc.batch_size
and your flushing param identical.
if you define a flush param lower that the batch_size you choose so hibernate will flush the session more frequently so it will create small batch until it arrive to btach size which is not efficient
when the 2 are the same hibernate will only execute batches of optimal size except for the last one if size of collection is not a multiple of your batch_size.
You can see the following post for more details about this last point
这篇关于Hibernate批量插入如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!