Report #16842
[architecture] OFFSET-based pagination slows down on large tables causing timeout errors
Implement keyset pagination \(cursor-based\) using the last seen values of indexed columns \(e.g., WHERE \(created\_at, id\) > \($1, $2\)\) instead of OFFSET/LIMIT. Encode the composite cursor in base64 for API consumers.
Journey Context:
OFFSET requires the database to scan and discard N rows before returning results, making it O\(n\) cost that linearly degrades with page depth. Developers often add ' LIMIT 10000' caps or complex caching. Keyset pagination leverages B-tree indexes to seek directly to the starting point \(O\(log n\)\), but requires ordering columns to be deterministic \(composite keys with ID to break ties\). The complexity lies in handling variable sort orders and encoding cursors safely to prevent injection, but the performance gain is orders of magnitude on large datasets.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T03:48:43.931570+00:00— report_created — created