Search code examples
javamysqlperformancehibernateid-generation

Hibernate, what is the most efficient id generation strategy?


I need to insert many entities into the database via Hibernate. So, I want to find the most effective algorithm for Id generation.

Accordingly Hibernate Documentation exists four widely used generation strategies:

  • IDENTITY
  • SEQUENCE
  • TABLE
  • AUTO

I should use MySQL database, so I cannot apply SEQUENCE generation strategy. What about other strategies? What is the most efficient from performance point of view?


Solution

  • The best id generators in Hibernate are enhanced-table and enhanced-sequence, coupled with an appropriate optimizer, such as hilo. I have experience with enhanced-table + hilo, inserting over 10,000 records per second.

    BTW the statement that "hilo needs an additional query per generated entity" is patently false: the whole point of the optimizer is to prevent this.