How do you enable batch inserts in hibernate?

后端 未结 2 1275
再見小時候
再見小時候 2020-12-13 07:44

With hibernate, when I attempt to enable batch inserts with

  50

I get the follow

相关标签:
2条回答
  • 2020-12-13 08:02

    To enable batching for both INSERT and UPDATE statements, you need to sett all the following Hibernate properties:

    <property name="hibernate.jdbc.batch_size">30</property>
    <property name="hibernate.order_inserts">true</property>
    <property name="hibernate.order_updates">true</property>
    <property name="hibernate.jdbc.batch_versioned_data">true</property>
    

    If you can use a SEQUENCE, then you should not use IDENTITY generators, since it disables batch fetching.

    If you cannot use a SEQUENCE (e.g. MySQL), then try using a separate mechanism to enable batch inserts (e.g. jOOQ) instead of using the TABLE generator which does not scale and has a high-performance penalty.

    0 讨论(0)
  • 2020-12-13 08:11

    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.

    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).

    hibernate.jdbc.batch_versioned_data may also be useful.

    jdbc:mysql://localhost:3306/batch?rewriteBatchedStatements=true type connection strings might also be related somehow.

    https://forum.hibernate.org/viewtopic.php?p=2374413 and also see Hibernate batch size confusion

    0 讨论(0)
提交回复
热议问题