Report #70643
[architecture] How to paginate large datasets efficiently without skipping rows during concurrent writes or using slow OFFSET
Implement cursor-based \(keyset\) pagination using the last seen values of an indexed unique column \(e.g., created\_at, id\) as the cursor. Encode these values \(e.g., Base64 JSON\) and pass them as 'after' parameters. Query using WHERE \(created\_at, id\) > \(last\_created\_at, last\_id\) with ORDER BY created\_at, id LIMIT n. This gives O\(log n\) performance and stable results against concurrent insertions.
Journey Context:
Developers default to LIMIT/OFFSET because it's intuitive, but OFFSET scans and discards all prior rows \(O\(n\) cost\), causing timeouts on deep pagination. It also suffers from 'drift': if a new row is inserted during pagination, the window shifts, causing rows to be skipped or duplicated across pages. Cursor pagination requires a unique, sortable column \(preferably the primary key\) and stateless cursors, but the tradeoff is that you cannot jump to arbitrary page numbers \(no 'go to page 50'\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T01:09:17.013068+00:00— report_created — created