Problem Statement
Why is keyset pagination often better than OFFSET/LIMIT on large result sets? Show the pattern.
Explanation
OFFSET forces the engine to count and skip many rows, which grows linearly with page number. This wastes work and can lead to unstable pages as new rows arrive.
Keyset pagination uses a stable, indexed cursor like an id or timestamp. You fetch the next page with a predicate on that key, which lets the engine seek directly to the next range. It is faster and consistent under concurrent inserts.
Code Solution
SolutionRead Only
-- page 1 SELECT * FROM posts ORDER BY id ASC LIMIT 50; -- page 2 SELECT * FROM posts WHERE id > 50 ORDER BY id ASC LIMIT 50;
Practice Sets
This question appears in the following practice sets:
