Databases · Middle · Question 36 of 75

Why is keyset (cursor) pagination faster than OFFSET for deep pages?

SELECT * FROM posts
WHERE (created_at, id) < ($1, $2)
ORDER BY created_at DESC, id DESC
LIMIT 20;

Explanation

Include a unique column (like id) as a tiebreaker. The trade-off: no jumping to an arbitrary page number.

Further reading

Problem with a question

Databases · Middle · question 36

Test yourself

Take the Databases · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz