Lesson 222 of 296
Keyset pagination vs OFFSET
What you will learn
- Why OFFSET gets slow, proven with a real plan
- Keyset pagination in SQL
- Row value comparisons
- The index that makes it fast
- When to use each
On this page
OFFSET reads what it skips
Lesson 197 introduced both styles. Here is the proof. Page 5,001 of a 200,000-row table, 20 rows per page:
SELECT * FROM events ORDER BY created_at DESC, id DESC LIMIT 20 OFFSET 100000;
Limit (actual rows=20.00 loops=1)
Buffers: shared hit=1462 read=386
-> Index Scan using events_created_id_idx on events (actual rows=100020.00 loops=1)Even with a perfect index, the database had to walk through 100,020 rows to return 20, because OFFSET means "produce rows, then throw the first 100,000 away". Page 10,000 would read twice as many. (Without the index it was worse: a full Seq Scan and a Sort of all 200,000 rows.)
Keyset pagination
Remember the last row of the previous page (here: created_at = '2026-03-11 10:00:00+00', id = 98000) and ask for rows that come after it in the sort order:
SELECT * FROM events
WHERE (created_at, id) < ('2026-03-11 10:00:00+00', 98000)
ORDER BY created_at DESC, id DESC
LIMIT 20;
Limit (actual rows=20.00 loops=1)
Buffers: shared hit=5 read=1
-> Index Scan using events_created_id_idx on events (actual rows=20.00 loops=1)
Index Cond: (ROW(created_at, id) < ROW('2026-03-11 10:00:00+00'::timestamp with time zone, 98000))20 rows read, 6 pages touched, instead of 100,020 rows and 1,848 pages. And it stays that fast on page 1 or page 10,000.
Row value comparison
(created_at, id) < (X, Y) compares tuples left to right, like sorting by surname then first name. It means:
created_at < X OR (created_at = X AND id < Y)The id breaks ties when several rows share the same created_at, so no row is skipped or repeated. PostgreSQL can use a composite index directly for the row comparison. (MySQL supports the syntax too; some databases need the expanded OR form.)
The index
The index must match the sort order and include the tie-breaker:
CREATE INDEX events_created_id_idx ON events (created_at DESC, id DESC);With a filter (for example one user's events), put the equality column first: (user_id, created_at DESC, id DESC) and query WHERE user_id = $1 AND (created_at, id) < ($2, $3).
Previous page and other directions
- For "previous page", flip the comparison and sort (
>andASC), then reverse the results in code. - Sorting by a user-chosen column (price, name) works the same way: the cursor stores that column's value plus the ID, and you need an index for each supported sort.
Which to use
| Use OFFSET when | Use keyset when |
|---|---|
| Small tables (admin screens with a few thousand rows) | Large or growing tables |
| Users need page numbers and "jump to page 40" | Infinite scroll, feeds, APIs, exports, background syncs |
| Data rarely changes while paging | Data changes often (OFFSET then shows duplicates or skips rows) |
How to say it in an interview
"OFFSET still produces and discards every skipped row, so deep pages get slower linearly; on a 200,000-row table, page 5,001 read over 100,000 index entries to return 20. Keyset pagination remembers the last row's sort values and asks for rows after it with a row comparison like created_at, id less than the cursor, using a composite index in the same order with the ID as tie-breaker, so every page reads only its own rows and stays stable as data changes. I use offset for small admin tables that need page numbers and keyset for everything large."
Key words
| Word | Meaning |
|---|---|
| OFFSET pagination | Skipping a number of rows |
| Keyset pagination | Continuing after the last row's sort values |
| Row value comparison | Comparing tuples like (a, b) < (x, y) |
| Tie-breaker | A unique column that makes the sort order total |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.