tbaz
tbaz

Reputation: 1016

JPA/Hibernate bulk inserts slow

I'm doing bulk inserts with JPA using Hibernate as my provider. The DB is Oracle. It created a sequence generator, and each time it does an insert it queries the sequence generator for nextval. If I'm doing 1K inserts, it will hit the sequence generator 1K times. Any way to speed this up, if I want to stick with JPA?

Upvotes: 4

Views: 7640

Answers (2)

JavaRocky
JavaRocky

Reputation: 19945

Use the allocationSize in the JPA @SequenceGenerator.

See this example, where it is set to 150:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MY_ENTITY_SEQ")
@SequenceGenerator(name = "MY_ENTITY_SEQ", sequenceName = "MY_ENTITY_SEQ", allocationSize = 150)
@Column(name = "MY_ENTITY", nullable = false)
private Long id;

Upvotes: 8

topchef
topchef

Reputation: 19813

Have a shot with sequence preallocation feature:

Sequence objects provide the optimal sequencing option, as they are the most efficient and have the best concurrency, however they are the least portable as most databases do not support them. Sequence objects support sequence preallocation through setting the INCREMENT on the database sequence object to the sequence preallocation size.

Upvotes: 2

Related Questions