从 PageRequest 禁用计数查询以获取总页数的方法? [英] Way to disable count query from PageRequest for getting total pages?

查看:24
本文介绍了从 PageRequest 禁用计数查询以获取总页数的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将 Spring Data 与 PageRequest 一起使用,并处理了大量数据.除了为获取总页数而执行的查询外,所有查询均以最佳方式执行.有没有办法禁用此功能,或者我们很可能必须实现自己的 Pageable?

We are using Spring Data with the PageRequest and hitting a significantly large set of data. All queries perform optimally except the query being executed to get the total number of pages. Is there a way to disable this feature or would we most likely have to implement our own Pageable?

http://static.springsource.org/spring-data/data-commons/docs/1.3.2.RELEASE/api/org/springframework/data/domain/PageRequest.html

http://static.springsource.org/spring-data/data-commons/docs/1.3.2.RELEASE/api/org/springframework/data/domain/Pageable.html

经过进一步分析,我认为解决此问题的唯一方法是不使用 Spring Data 并使用 EntityManager,因为它允许设置起始行和返回的记录数.我们只需要下一页是否可用,所以我们只需要检索一个额外的记录.我们还需要一个在 Spring Data 中似乎不可能的动态查询.

After further analysis I believe the only way around this problem is to not use Spring Data and use EntityManager as it allows setting of start row and number of records to return. We only need whether next page is available so we just retrieve one extra record. We also need a dynamic query which doesn't seem possible in Spring Data.

编辑 2:而且我似乎没有等待足够长的时间来获得一些回复.谢谢各位!!!

Edit 2: And it would seem I just didn't wait long enough for some responses. Thanks guys!!!

推荐答案

实现这一点的方法很简单,就是使用 List 作为返回值.例如,对于这样定义的存储库:

The way to achieve this is simply by using List as return value. So for example for a repository defined like this:

interface CustomerRepository extends Repository<Customer, Long> {

  List<Customer> findByLastname(String lastname, Pageable pageable);
}

查询执行引擎将应用 Pageable 提交的偏移量和页面大小,但不会触发附加计数查询,因为我们不需要构造 Page实例.这也记录在 相关参考文档的部分.

The query execution engine would apply the offset and pagesize as handed in by the Pageable but not trigger the additional count query as we don't need to construct a Page instance. This also documented in the relevant sections of the reference documentation.

更新:如果您想要 Page 的下一页/上一页但仍跳过计数查询,您可以使用 Slice 作为返回值.

Update: If you want the next page / previous page of Page but still skip the count query you may use Slice as the return value.

这篇关于从 PageRequest 禁用计数查询以获取总页数的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆