Hibernate, what is the most efficient identifier generation strategy?

I need to insert many objects into the database through Hibernate. So, I want to find the most efficient Id generation algorithm.

Accordingly, there are four commonly used generation strategies for Hibernate Documentation :

  • IDENTITY
  • SEQUENCE
  • Table
  • AUTO

I have to use a MySQL database, so I cannot apply the SEQUENCE generation strategy. What about other strategies? What is the most effective in terms of performance?

+6
source share
2 answers

The best id generators in Hibernate are an extended table and extended sequence combined with a suitable optimizer such as hilo. I have experience working with an extended table + hilo, inserting more than 10,000 records per second.

The BTW statement that “hilo needs an additional request for the generated object” is clearly false: the entire optimizer point is prevented .

+8
source

Since you cannot use SEQUENCE , and AUTO just automatically selects a supported generator algorithm from the existing ones, you are left with IDENTITY and TABLE .

  • TABLE : uses the hi / lo algorithm to efficiently generate identifiers of type long, short or int, given the table and column as the source of the hi values. The hi / lo algorithm generates identifiers that are unique to a specific database only. → Denotes an additional request for the generated object . (This is not true if you use optimizers. Unfortunately, using the optimizer is usually not used by default if the optimizer is not specified.)

  • IDENTITY : supports identity columns in DB2, MySQL, MS SQL Server, Sybase, and HypersonicSQL. → Performance is a way , like you, without Hibernate. A database has been created, almost no overhead.

There are more Hibernate-specific generators, but they will not beat the database identifier for performance. (See 5.1.2.2.1. Various additional generators in a related document.)

+1
source

All Articles