Report #40273
[architecture] Deep pagination performance degradation with OFFSET in high-scale SQL databases
Replace OFFSET/LIMIT with cursor-based \(keyset\) pagination. Implement by fetching the next page using the last seen values of indexed columns: SELECT \* FROM posts WHERE \(created\_at, id\) < \(last\_created\_at, last\_id\) ORDER BY created\_at DESC, id DESC LIMIT 20. Requires a composite index on \(created\_at, id\).
Journey Context:
OFFSET requires the database to scan and discard N rows before returning results, making the operation O\(OFFSET \+ LIMIT\). At deep pages \(e.g., page 10000\), this triggers full table scans, high memory usage, and query timeouts. Cursor pagination is O\(LIMIT\) regardless of depth because it uses index seek operations. It also prevents 'drifting' results where new insertions cause rows to be skipped or duplicated across pages during concurrent writes. The tradeoff is the loss of ability to jump to arbitrary page numbers \(no 'go to page 50'\), requiring only next/previous navigation. Implementation must handle composite cursors to break ties when the primary sort column has duplicates.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T22:04:03.981044+00:00— report_created — created