Change how Spring JPA does pagination

Dash :

I want to know if it is possible to change how Spring does pagination because the default way of PagingAndSortingRepository uses a LIMIT with OFFSET but on large datasets that becomes too slow.

The data I have does have an indexed column so there is no problem to write logic like WHERE timestamp > x AND timestamp < y LIMIT 1000; and keep track of the highest timestamp received. I am just wondering if this is already built into Spring JPA, so I could tell it to order by a column and use that rather than OFFSET.

https://electrictoolbox.com/mysql-limit-slow-large-offset/

mentallurg :

There is no magic solution for this.

Yes, it can be slow. But I find that this is not an indicator of problem in paging, but rather an indicator that one uses improper tool / solution.

I find that using paging to navigate across a huge datasets is a bad approach. I'd suggest you to use a relatively small number of pages like 20 pages. If you want to navigate across a bigger volume of data, consider proper splitting / structuring of data in your requests. For instance, if your data can be filtered by time, consider using proper filters in your request (and corresponding GUI elements if it is triggered from GUI).

Ask user first to select a time period, like from/to years, or from/to dates, or from/to hours, depending how much data you have. And use paging for such query (which will return relatively small number of pages).

Alternatively, no matter what filter / search criteria user defined, in the first request check the number of pages. If it exceeds some limit (like 20 pages), throw an exception and ask user to refine the filter / search criteria. Continue that until the number of pages does not exceed the limit you want. Paging with such filter can be much faster.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=417509&siteId=1