简体   繁体   English

如何在休眠中启用批量插入?

[英]How do you enable batch inserts in hibernate?

With hibernate, when I attempt to enable batch inserts with使用休眠,当我尝试启用批量插入时

  <property name="jdbc.batch_size">50</property>

I get the following output:我得到以下输出:

 [...] cfg.SettingsFactory INFO  - JDBC batch updates for versioned data: disabled
 [...] cfg.SettingsFactory INFO  - Order SQL inserts for batching: disabled

And then this:然后这个:

 [...] jdbc.AbstractBatcher DEBUG - Executing batch size: 1

never more than batch size: 1 basically.从不超过batch size: 1基本上。

Am I missing a setting?我缺少设置吗?

To enable batching for both INSERT and UPDATE statements, you need to sett all the following Hibernate properties:要为 INSERT 和 UPDATE 语句启用批处理,您需要设置以下所有 Hibernate 属性:

spring.jpa.properties.hibernate.jdbc.batch_size=30
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true

If you can use a SEQUENCE , then you should not use IDENTITY entity identifier generator, since it disables batch fetching.如果您可以使用SEQUENCE ,则不应使用IDENTITY实体标识符生成器,​​因为它会禁用批量提取。

If you cannot use a SEQUENCE (eg MySQL), then try using a separate mechanism to enable batch inserts (eg JDBC) instead of using the TABLE generator which does not scale and has a high-performance penalty.如果您不能使用SEQUENCE (例如 MySQL),那么请尝试使用单独的机制来启用批量插入(例如 JDBC),而不是使用无法扩展且具有高性能损失的TABLE生成器。

Turns out what was missing in this case was:事实证明,在这种情况下缺少的是:

<property name="order_inserts">true</property>

ref: https://forum.hibernate.org/viewtopic.php?p=2374413 , https://stackoverflow.com/a/5240930/32453 Or possibly hibernate.order_inserts.参考: https : //forum.hibernate.org/viewtopic.php ? p =2374413https : //stackoverflow.com/a/5240930/32453或者可能是 hibernate.order_inserts。

Now I see现在我明白了

 [...] cfg.SettingsFactory INFO  - Order SQL inserts for batching: enabled
 ...
 [...] Executing batch size: 2

Much more frequently (anything greater than 1 basically means it's successfully doing batch inserts).更频繁(任何大于 1 的值基本上意味着它成功地进行了批量插入)。

hibernate.jdbc.batch_versioned_data may also be useful. hibernate.jdbc.batch_versioned_data 也可能有用。

jdbc:mysql://localhost:3306/batch?rewriteBatchedStatements=true type connection strings might also be related somehow. jdbc:mysql://localhost:3306/batch?rewriteBatchedStatements=true 类型的连接字符串也可能以某种方式相关。

https://forum.hibernate.org/viewtopic.php?p=2374413 and also see Hibernate batch size confusion https://forum.hibernate.org/viewtopic.php?p=2374413并参见Hibernate 批量大小混淆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM