Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a paginated query slower than a plain one with Spring Data?

Given I have a simple query:

List<Customer> findByEntity(String entity);

This query returns 7k records in 700ms.

Page<Customer> findByEntity(String entity, Pageable pageable);

this query returns 10 records in 1080ms. I am aware of the additional count query for pagination, but still something seems off. Also one strange thing I've noticed is that if I increase page size from 10 to 1900, response time is exactly the same around 1080 ms.

Any suggestions?

like image 338
SeaBiscuit Avatar asked May 17 '17 10:05

SeaBiscuit


People also ask

Does pagination improve performance?

Thanks to pagination, we can split our large dataset into chunks ( or pages ) that we can gradually fetch and display to the user, thus reducing the load on the database. Pagination also solves a lot of performance issues both on the client and server-side!

Is Spring data provide support for pagination and sorting?

Spring Data PagingAndSortingRepository To help us deal with this situation, Spring Data JPA provides way to implement pagination with PagingAndSortingRepository. PagingAndSortingRepository extends CrudRepository to provide additional methods to retrieve entities using the sorting abstraction.

What is spring data and why it more powerful?

Spring Data is a high-level Spring Source project. Its purpose is to unify and easy access to the different kinds of persistence stores, both relational database systems, and NoSQL data stores.


1 Answers

It might indeed be the count query that's expensive here. If you insist on knowing about the total number of elements matching in the collection there's unfortunately no way around that additional query. However there are two possibilities to avoid more of the overhead if you're able to sacrifice on information returned:

  1. Using Slice as return typeSlice doesn't expose a method to find out about the total number of elements but it allows you to find out about whether a next slice is available. We avoid the count query here by reading one more element than requested and using its (non-)presence as indicator of the availability of a next slice.
  2. Using List as return type — That will simply apply the pagination parameters to the query and return the window of elements selected. However it leaves you with no information about whether subsequent data is available.
like image 182
Oliver Drotbohm Avatar answered Oct 18 '22 10:10

Oliver Drotbohm