Need to insert 100000 rows in mysql using hibernate in under 5 seconds

前端 未结 4 474
我寻月下人不归
我寻月下人不归 2021-01-30 23:38

I am trying to insert 100,000 rows in a MYSQL table under 5 seconds using Hibernate(JPA). I have tried every trick hibernate offers and still can not do better than 35 seconds.

4条回答
  •  温柔的废话
    2021-01-30 23:53

    Uff. You can do a lot of things to increase speed.

    1.) Use @DynamicInsert and @DynamicUpdate to prevent the DB from inserting non-empty columns and updating changed columns.

    2.) Try to insert the columns directly (without using hibernate) into your database to see if hibernate is really your bottleneck.

    3.) Use a sessionfactory and only commit your transaction every e.g. 100 inserts. Or only open and close the transaction once and flush your data every 100 inserts.

    4.) Use the ID generation strategy "sequence" and let hibernate preallocate (via the parameter allocationsize) the IDs.

    5.) Use caches.

    Some of this possible solutions can have timing disadvantages when not used correctly. But you have a lot of opportunities.

提交回复
热议问题