Best way to save the large number of records in hibernate

后端 未结 2 890
旧巷少年郎
旧巷少年郎 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: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

提交回复
热议问题