Search code examples
hibernateh2table-statistics

How do you get Hibernate to update database statistics?


I am putting a lot of rows into a database table that was previously nearly empty, Im then creating a report based on data in that table. Although the table is indexed the queries are taking far too long and Im guessing it is because Hibernate is doing a full table scan every time for the query instead of using the indexes because it believes the database table to only contain a couple of rows.

How do I make Hibernate update database statistics for the table so it can then construct a suitable query plan ?


Solution

  • Hibernate can run native SQL statements, this way you can also run analyze:

    Query analyze = session.createSQLQuery("analyze");
    analyze.executeUpdate();
    

    See also the official documentation about native SQL statement execution.