Best way to save the large number of records in hibernate

后端 未结 2 868
旧巷少年郎
旧巷少年郎 2021-01-23 09:11

I have 5000 records to be saved. what is the best way in the database point of view, whether to save individual records(save(record) 5000 times) or saveAll(list of 5000 records)

相关标签:
2条回答
  • 2021-01-23 09:21

    Use the feature batch Insertion in Hibernate.

    Batch Insertion is a powerful feature of hibernate particularly useful when you are importing data from other systems in batch. If you do not use batch feature of hibernate, your application's performance may decrease dramatically at the time of insertion of many records.

    Here is a simple comparison on normal save and Batch insert.

    And also official docs.

    0 讨论(0)
  • 2021-01-23 09:39

    Saving 5000 records at a time may run out of the memory and get OutOfMemoryException because 5000 instances may occupy pretty large memory.

    Saving one record at a time means that you do not make use of JDBC 's batching feature which can insert records more effectively.

    So , to achieve the optimal performance , you should tell hibernate to use JDBC 's batching feature by setting hibernate.jdbc.batch_size to some non-zero value . The number of records saved at a time should equal to the value of hibernate.jdbc.batch_size.

    Also , you should clear the session to release memory to prevent memory exhaustion at the end of each batch. You could also disable the second-level cache to reduce the unnecessary overhead.

    Reference : Hibernate batch size confusion

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