如何从 PageRequest 禁用计数查询以获取总页数? [英] Way to disable count query from PageRequest for getting total pages?
问题描述
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?
Edit: 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!!!
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);
}
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.
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屋!