Problem Statement
Why is keyset pagination usually faster and more stable than OFFSET ... LIMIT for deep pages?
Explanation
OFFSET N means the engine must find and skip N rows in order, which grows linearly with page depth. That adds sorts and reads, and results drift when new rows arrive.
Keyset uses a stable cursor, like the last seen id and timestamp, so the query can seek to a point and fetch the next chunk. It is faster and avoids misses or duplicates under concurrency.
Code Solution
SolutionRead Only
SELECT * FROM posts WHERE user_id=:u AND (created_at,id) < (:t,:id) ORDER BY created_at DESC, id DESC LIMIT 50;
