the IncrementGenerator uses to following statement to retrieve the highest ID currently in the database:
select max(ids_.ID) from ( select ID from table1 union select ID from table2) as ids_
This is very inefficient, if there are lots of records in the tables (> millions), above select will actually retrieve all IDS and then max them. I have about 1.5 mio records and the query takes almost a minute to execute.
Below query is much faster:
select max(ids_.mx) from ( select max(ID) as mx from table1 union select max(ID) as mx from table2 ) ids_
I attached a patch for the class org.hibernate.id.IncrementGenerator which implements above suggested change.
Andy
|