I know there have been quite a few similar questions but I cant find an answer to this one:
I'm not very familiar with hibernate but I'm working on an app which contains an extremely slow hibernate query. This query is generated from a very simple query in the code:
Criteria crit = session.createCriteria(clazz); // clazz is the class of the Entities to load
return crit.list();
There are probably a lot of possibility which could make this query extremey slow (hibernate bindings, drivers, etc, etc.) but what I dont understand is the following: I set hibernate to output the SQL statements it performs. The code above is translated into an awfully long query containing 3 inner joins and 9 left outer joins, the result being displayed in a table with around 80 columns. I'm sure, however, that only this single SQL statement is produced by hibernate and executed for this code.
I'm using H2 database.
- When I run the code within the application, it takes around 20 seconds to execute (result set contains approx 6000 rows)
- If I code within the application a call to
Session.createSQLQuery(query).list()
wherequery
is the sql generated by hibernate for the code above, it takes approx 30 sec to execute. - When I execute the SQL query directly using H2 shell or even browser client, it takes less than a second to execute.
What could be the reason(s) for such a huge difference between those 3 ways of executing the same query. I would expect an overhead with hibernate, but not by this amount.