简体   繁体   中英

Best JPA ID Generation Strategy for Batch Insertion

I am facing an issue while inserting 100 000 records at once using spring data JPA repository. When we execute repo.save(List<Objs>) it is taking a lot of time if we use Sequence generator as it queries the database for the nextval. I am using Oracle, which ID generation is best here?

Sequence generator is probably a good choice, but you have to tweak its parameters.

In your particular case, I'd start experimenting with allocation size, and then with strategy. See for example: JPA/Hibernate bulk inserts slow

Take a look at the optimizers configuration: https://vladmihalcea.com/hibernate-hidden-gem-the-pooled-lo-optimizer/

Note that your configuration resolves to:

  • SequenceHiLoGenerator on Hibernate 4
  • SequenceStyleGenerator on Hibernate 5, (it has hibernate.id.new_generator_mappings set to true)

You cannot use identity generator (see Hibernate disabled insert batching when using an identity identifier generator )

Table generator is not the best performant one ( https://vladmihalcea.com/why-you-should-never-use-the-table-identifier-generator-with-jpa-and-hibernate/ )

Additionally, make sure that the number of nextval() is the actual problem. Maybe changing batch size or statement ordering will help (see https://vladmihalcea.com/how-to-batch-insert-and-update-statements-with-hibernate/ )

allocationSize=1 is the real issue here. With this configuration hibernate will call nextVal() for each insert so if you have 1000 inserts then hibernate will call nextVal() a 1000 times.

For more information refer to this article by Vlad Mihalcea

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM