Fast paging in the real world
Replace OFFSET pagination with keyset (seek) pagination.
LIMIT n OFFSET k walks and discards prior rows. Cost grows with page number.
Keyset
SELECT id, created_at, title
FROM posts
WHERE (created_at, id) < (?, ?)
ORDER BY created_at DESC, id DESC
LIMIT 20;
Pass the last tuple from the previous page. Index (created_at, id).
Trade-offs
- No cheap “jump to page 500” without extra design
- Ideal for next/prev and infinite scroll
- Stable under inserts if ordering keys are deterministic
Fix pagination before buying a larger server. OFFSET under traffic is a self-DDoS.