I was reading this paragraph in the Spring Data JPA official documentation about the difference between Page
and Slice
(emphasis mine):
The first method lets you pass an org.springframework.data.domain.Pageable instance to the query method to dynamically add paging to your statically defined query. A Page knows about the total number of elements and pages available. It does so by the infrastructure triggering a count query to calculate the overall number. As this might be expensive (depending on the store used), you can instead return a Slice. A Slice only knows about whether a next Slice is available, which might be sufficient when walking through a larger result set.
I get it how a Page
can get the total elements by executing an additional count query with the same where clause, but how does a Slice
gets to know whether other elements are available or not with a single query? What is the SQL executed?
How does a Slice gets to know whether other elements are available or not with a single query?
Spring Data JPA selects one element more than actually needed to fill the Slice
if that additional element is present, there is another Slice
available. If not this is the last Slice
.