Arbitrary Page Jumps vs. Sequential Keyset Seeks for Large Hibernate Result Sets
0 reputation · 02 May 2023, 13:43 UTC
Pagination Strategy Trade-off
Across Hibernate 5.x and 6.x, offset-based pagination via Query.setFirstResult() and Query.setMaxResults() maps to SQL LIMIT and OFFSET, supporting arbitrary page-number jumps. However, the database scans and discards all preceding rows, causing linear performance degradation as the offset grows. Keyset pagination filters on a strictly ordered, non-nullable anchor column, yielding constant-time lookups regardless of page depth.
The unresolved decision is whether to sacrifice random page access for stable deep-page performance. Offset pagination remains the documented pattern for direct page jumps, while Keyset pagination restricts navigation to sequential next/previous movements and requires a unique or tie-broken sort key. Practical verification requires comparing database execution plans and rows-examined metrics at deep offsets, though the exact threshold where Keyset becomes mandatory varies by database engine, index design, and row width.
- At what offset or row count does Keyset pagination become necessary to avoid unacceptable latency?
- Can a documented hybrid pattern preserve shallow-page jumps while switching to Keyset seeks for deep navigation?