keyset-pagination
Installation
SKILL.md
Keyset pagination
Why OFFSET fails
SELECT * FROM events ORDER BY created_at DESC LIMIT 20 OFFSET 200000;
The database cannot jump to row 200,000. It must produce and discard 200,000 rows to reach the 20 wanted. Cost is O(offset), so page 10,000 costs 10,000 times page 1.
Two separate failures:
It slows with depth. Fine on 500 development rows. A production timeout on deep pages only, affecting crawlers and export scripts.
It returns wrong results on live data. Rows inserted and deleted between requests shift the offset: