Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate IDENTITY vs SEQUENCE entity identifier generators

Tags:

This article says:

Unlike identity, the next number for the column value will be retrieved from memory rather than from the disk – this makes Sequence significantly faster than Identity

Does it mean that ID comes from disk in case of identity? If yes, then which disk and how?

Using sequence, I can see in the log, an extra select query to DB while inserting a new record. But I didn't find that extra select query in the log in case of identity. Then how sequence becomes faster than identity?

like image 515
cooper Avatar asked Jul 22 '13 05:07

cooper


People also ask

What is sequence generator in Hibernate?

SEQUENCE Generation. To use a sequence-based id, Hibernate provides the SequenceStyleGenerator class. This generator uses sequences if our database supports them. It switches to table generation if they aren't supported.

What is difference between generation type auto and identity?

AUTO: Hibernate selects the generation strategy based on the used dialect, IDENTITY: Hibernate relies on an auto-incremented database column to generate the primary key, SEQUENCE: Hibernate requests the primary key value from a database sequence, TABLE: Hibernate uses a database table to simulate a sequence.

What is id generator in Hibernate?

A generator class is used to generate an ID for an object, which is going to be inserted in the database as a primary key. The ID is a unique identifier for the objects of the persistent class. We can use any generator classes in our application as per our requirements.

What is the use of id GeneratedValue strategy GenerationType identity?

IDENTITY This GenerationType indicates that the persistence provider must assign primary keys for the entity using a database identity column. IDENTITY column is typically used in SQL Server. This special type column is populated internally by the table itself without using a separate sequence.


2 Answers

Strategy used by sequence:

Before inserting a new row, ask the database for the next sequence value, then insert this row with the returned sequence value as ID.

Strategy used by identity:

Insert a row without specifying a value for the ID. After inserting the row, ask the database for the last generated ID.

The number of queries is thus the same in both cases. But, Hibernate uses by default a strategy that is more efficient for the sequence generator. In fact, when it asks for the next sequence value, it keeps th 50 (that's the dafault, IIRC, and it's configurable) next values in memory, and uses these 50 next values for the next 50 inserts. Only after 50 inserts, it goes to the database to get the 50 next values. This tremendously reduces the number of needed SQL queries needed for automatic ID generation.

The identity strategy doesn't allow for such an optimization.

like image 96
JB Nizet Avatar answered Oct 05 '22 16:10

JB Nizet


The IDENTITY generator will always require a database hit for fetching the primary key value without waiting for the flush to synchronize the current entity state transitions with the database.

So the IDENTITY generator doesn't play well with Hibernate write-behind first level cache strategy, therefore JDBC batching is disabled for the IDENTITY generator.

The sequence generator can benefit from database value preallocation and you can even employ a hi/lo optimization strategy.

In my opinion, the best generators are the pooled and pooled-lo sequence generators. These generators combine the batch-friendly sequence generator with a client-side value generation optimization that's compatible with other DB clients that may insert rows without knowing anything about our generation strategy.

Anyway, you should never choose the TABLE generator because it performs really bad.

like image 20
Vlad Mihalcea Avatar answered Oct 05 '22 14:10

Vlad Mihalcea