Hibernate Pagination using HQL

北慕城南 提交于 2019-12-03 16:20:12

Your query tells database to sort all records that satisfy the WHERE clause. It potentially may sort millions of records before returning you top 50.

EDIT 1/26: Now that the specific questions were clarified, I'll try to respond more specifically.

  1. Every time you execute query like that, Hibernate goes to the database. Even more, it would flush all new/updated data in the session to disk. If this is your situation, this behavior might contribute to the slowness.

  2. Using Hibernate Query API usually performs quite well in most situations and is compatible with a broad variety of the database platforms. If you are really concerned about squeezing last drop of performance out of your data access layer, you can write your own native SQL query to select top 50 results. But as soon as you do that, you'll almost certainly loose database independence. So, evaluate your costs vs. benefits.

Your query run times appear to be in the single milliseconds range. This is usually as good as it gets with the relational databases that store data on disk. So you might want to evaluate whether you indeed have a performance problem.

EDIT 1/27: OK. It looks like the problem in the overall design of the page. I had been using AJAX for last 7 years or so, so I don't usually have to wait for the filtering UI controls to redraw when going through pages of a table. I guess, switching application UI frameworks is not an option in your case. You have to figure out how to optimize loading of the data for the dropdowns and such. Does this data change frequently? Can you cache it somewhere in the application? If you have to load them every time, can you get away with just getting the display strings instead of entire objects?

Thomas

If you set the first result and the maximum number of results Hibernate will only retrieve those entries from the database. If that is slow, set max results to 1 and enable SQL logging to see which associations are loaded as well.

Hibernate also supports several caches: * first level cache: will only be used during one Hibernate session which often corresponds to one transaction * second level cache: used over session/transaction boundaries, but mostly only by find by id methods * query cache: if enabled Hibernate will lookup the query results from there first. However, the query must be the same in terms of HQL and parameters so each page might be loaded from the DB once and then be cached. (For more information have a look here: http://docs.jboss.org/hibernate/core/3.5/reference/en/html/performance.html#performance-querycache)

Please note that caching requires heap memory and depending on the size of your entities caching 3 million entities might result in a huge memory hit and thus increased garbage collection which would hit performance again.

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