SQL query within hibernate extremely slow while very fast using SQL cient

人盡茶涼 提交于 2019-12-11 00:47:45

问题


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.

  1. When I run the code within the application, it takes around 20 seconds to execute (result set contains approx 6000 rows)
  2. If I code within the application a call to Session.createSQLQuery(query).list() where query is the sql generated by hibernate for the code above, it takes approx 30 sec to execute.
  3. 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.

来源:https://stackoverflow.com/questions/20953436/sql-query-within-hibernate-extremely-slow-while-very-fast-using-sql-cient

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!