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

查看:272
本文介绍了如何从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.

似乎我只是没有等待足够长的时间才能收到一些答复.谢谢大家!

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的下一页/上一页,但仍跳过计数查询,则可以使用

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天全站免登陆