Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 322
Emil Hotkowski Avatar asked Apr 19 '18 10:04

Emil Hotkowski


People also ask

What is slice in pagination?

In this tutorial we will see how Slice can help us in pagination process. Slice is a sized chunk of data with an indication of whether there is more data available. Spring supports Slice as a return type of the query method. Pageable parameter must be specified by the same query method.

Why do you need paging and sorting?

Paging and sorting is mostly required when we are displaying domain data in tabular format in UI. Pagination consist of two fields – page size and page number. Sorting is done on a single of multiple fields in the table.

Which are the valid option to create Pageable instance?

The most common way to create a Pageable instance is to use the PageRequest implementation: Pageable pageable = PageRequest. of(0, 5, Sort.by( Order. asc("name"), Order.


2 Answers

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.

like image 172
cassiomolin Avatar answered Oct 03 '22 12:10

cassiomolin


The main difference between Slice and Page is the latter provides non-trivial pagination details such as total number of records(getTotalElements()), total number of pages(getTotalPages()), and next-page availability status(hasNext()) that satisfies the query conditions, on the other hand, the former only provides pagination details such as next-page availability status(hasNext()) compared to its counterpart Page. Slice gives significant performance benefits when you deal with a colossal table with burgeoning records.

Let's dig deeper into its technical implementation of both variants.

  • Page
static class PagedExecution extends JpaQueryExecution {     @Override     protected Object doExecute(final AbstractJpaQuery repositoryQuery, JpaParametersParameterAccessor accessor) {          Query query = repositoryQuery.createQuery(accessor);         return PageableExecutionUtils.getPage(query.getResultList(), accessor.getPageable(),                 () -> count(repositoryQuery, accessor));     }      private long count(AbstractJpaQuery repositoryQuery, JpaParametersParameterAccessor accessor) {          List<?> totals = repositoryQuery.createCountQuery(accessor).getResultList();         return (totals.size() == 1 ? CONVERSION_SERVICE.convert(totals.get(0), Long.class) : totals.size());     } }  

If you observe the above code snippet, PagedExecution#doExecute method underlyingly calls PagedExecution#count method to get the total number of records satisfying the condition.

  • Slice
    static class SlicedExecution extends JpaQueryExecution {     @Override     protected Object doExecute(AbstractJpaQuery query, JpaParametersParameterAccessor accessor) {          Pageable pageable = accessor.getPageable();         Query createQuery = query.createQuery(accessor);          int pageSize = 0;         if (pageable.isPaged()) {              pageSize = pageable.getPageSize();             createQuery.setMaxResults(pageSize + 1);         }          List<Object> resultList = createQuery.getResultList();          boolean hasNext = pageable.isPaged() && resultList.size() > pageSize;          return new SliceImpl<>(hasNext ? resultList.subList(0, pageSize) : resultList, pageable, hasNext);      } }  

If you observe the above code snippet, to findout whether next set of results present or not (for hasNext()) the SlicedExecution#doExecute method always fetch extra one element(createQuery.setMaxResults(pageSize + 1)) and skip it based on the pageSize condition(hasNext ? resultList.subList(0, pageSize) : resultList).

  • Application:
    • Page

      Use when UI/GUI expects to displays all the results at the initial stage of the search/query itself, with page numbers to traverse(ex., bankStatement with pagenumbers)

    • Slice

      Use when UI/GUI expects to doesnot interested to show all the results at the initial stage of the search/query itself, but intent to show the records to traverse based on scrolling or next button click event (ex., facebook feed search)

like image 26
Prasanth Rajendran Avatar answered Oct 03 '22 10:10

Prasanth Rajendran