【发布时间】:2013-10-23 08:00:39
【问题描述】:
我正在使用 hibernate api 的query.setMaxResults(maxResults) & query.setFirstResult(firstResult) 方法来实现分页。 MS SQLServer 2012 sp1 是我用于项目的数据库。
以下是示例查询:
SELECT a.c1, b.c2, b.c3 FROM Person as a WITH(NOLOCK), Address as b WITH(NOLOCK) where <condition> ORDER BY a.c1
query.setMaxResults(maxResults) 和query.setFirstResult(firstResult) 方法是如何工作的?它会从数据库中获取所有数据,然后根据 first 和 max 参数从result-set 中过滤吗?还是仅从first-result 索引中获取基于max-result 值的结果数?
用户觉得应用程序需要一些时间来使用数据更新 UI。那么推荐的分页方式是什么?我是否需要使用存储过程或其他方式来提高性能?请多多指教。
更新: 当用户选择第 1 页时,将执行以下查询:
SELECT top 200 a.c1, b.c2, b.c3 FROM Person as a WITH(NOLOCK), Address as b WITH(NOLOCK) where <condition> ORDER BY a.c1 - executed with 2502540 ms
当用户选择第 30 页时,将执行以下查询:
SELECT top 6000 a.c1, b.c2, b.c3 FROM Person as a WITH(NOLOCK), Address as b WITH(NOLOCK) where <condition> ORDER BY a.c1 - executed with 34220186 ms
所以很明显,时间与页码成正比。无论页码如何,有没有办法保持恒定的执行速度?因为我们总是获取相同数量的记录。
提前致谢。
【问题讨论】:
标签: java sql hibernate pagination