Hibernate Offset Pagination vs Keyset Pagination for Deep Result Sets
0 reputation · 25 Jul 2021, 06:15 UTC
0 reputation · 25 Jul 2021, 06:15 UTC
The service must paginate deep result sets using Hibernate 6.x with Spring Data JPA. The team needs to choose between offset-based pagination through setFirstResult() and setMaxResults(), and keyset pagination that filters on an ordered unique column.
Offset pagination supports arbitrary page numbers, but the database typically scans and discards skipped rows, so latency can increase with page depth. Keyset pagination avoids scanning skipped rows, yet it requires a deterministic unique sort column and cannot jump directly to an arbitrary page number. Spring Data JPA Pageable may also issue a separate count query unless a custom count query is provided.
The emitted SQL varies by Hibernate dialect, and deep-offset behavior has not been profiled against the production database. It remains unclear which strategy best balances scan cost with navigation flexibility for this schema.
Which pagination strategy should the service standardize on for deep result sets? Does the loss of arbitrary page navigation in keyset pagination justify the performance gain? Should the team use offset pagination for early pages and switch to keyset only beyond a threshold?
Standardize on keyset pagination for deep result sets in Hibernate 6.x with Spring Data JPA when the result order is deterministic and unique, for example ORDER BY updated_at, id with a supporting index, and clients can pass a cursor from the last row to fetch the next page. Keep offset-based setFirstResult/setMaxResults or Spring Data Pageable for shallow pages and for callers that must jump directly to page N. The loss of arbitrary page navigation is justified for deep pages only when next-page navigation is acceptable; otherwise keyset alone is not sufficient.
setFirstResult/setMaxResults and Pageable generally produce dialect-specific LIMIT/OFFSET SQL; the database typically processes rows before the offset, so cost tends to grow with page depth even though skipped rows are not returned.(updated_at, id) > (:last_updated_at, :last_id) with the same ORDER BY and a LIMIT, avoiding skipped-row scans and moving cost to an indexed range scan.id.Pageable may issue a separate count query; return Slice or supply a custom count query when total elements are not needed, especially for deep pages.Offset latency is driven mainly by rows examined and discarded before the offset plus result-set construction, not only by page size. Keyset shifts that work to the cursor lookup and index range scan, so deep pages are expected to be faster, but the API changes from give me page 42 to give me the page after this cursor. A hybrid threshold is possible but adds two code paths and can duplicate or skip rows if sort keys are not unique and stable; prefer it only after profiling shows offset is cheap early and expensive deep.
(updated_at, id), and confirm a matching index exists on the target dialect.WHERE (updated_at, id) > (:last_updated_at, :last_id) ORDER BY updated_at, id LIMIT :size, and return the new cursor with the response.Slice instead of Page, or supply a custom count query, to avoid counting deep result sets unless totalElements is required.Do callers require direct arbitrary page numbers, such as page 1000? If yes, keyset alone cannot satisfy that requirement and you need offset or an offset-plus-keyset hybrid; if next-page cursors are sufficient, keyset is the better default for deep result sets.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.