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