Skip to content
Advertisement

Page vs Slice when to use which?

I’ve read in Spring Jpa Data documentation about two different types of objects when you ‘page’ your dynamic queries made out of repositories.

Page and Slice

Page<User> findByLastname(String lastname, Pageable pageable);

Slice<User> findByLastname(String lastname, Pageable pageable); 

So, I’ve tried to find some articles or anything talking about main difference and different usages of both, how performance changes and how sorting affercts both type of queries.

Does anyone has this type of knowledge, articles or some good source of information?

Advertisement

Answer

Page extends Slice and knows the total number of elements and pages available by triggering a count query. From the Spring Data JPA documentation:

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, Slice can be used as return instead. A Slice only knows about whether there’s a next Slice available which might be just sufficient when walking through a larger result set.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement