(Note · · 5 min read)
Page one is instant, page four hundred takes nine seconds
Pagination with an offset works beautifully for the first few pages and degrades steadily after that. The reason is not the amount of data returned — every page returns fifty rows — but the amount the database has to walk past to find them.
By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka
Offset makes the database skip, row by row
An offset of twenty thousand asks the database to produce twenty thousand and fifty rows in order and discard the first twenty thousand. The work is proportional to how deep the page is, so the last page is the slowest and gets slower every time the table grows.
It also has a correctness problem nobody notices until a user complains: if a row is inserted or deleted while someone pages through, rows shift between pages, so one is seen twice and another never appears at all.
Ask for what comes after
Instead of counting rows to skip, remember the last row seen and ask for the rows after it. With an index on the ordering column, the database seeks straight there, and page four hundred costs exactly what page one costs.
-- slower the deeper you go
SELECT * FROM events
ORDER BY created_at DESC, id DESC
LIMIT 50 OFFSET 20000;
-- constant cost: seek, then read 50
SELECT * FROM events
WHERE (created_at, id) < ($last_created_at, $last_id)
ORDER BY created_at DESC, id DESC
LIMIT 50;The ordering must be unique, which is why the identifier is in both the order and the comparison. Ordering by a timestamp alone means rows sharing a timestamp can be skipped or repeated at a page boundary.
What you give up
You cannot jump to an arbitrary page number, because the cursor describes a position rather than a count. For an infinite scroll or a next-page link that is no loss. For a table where users expect numbered pages, it is a visible change.
In practice deep numbered pages are rarely used for finding anything — people search or filter instead. Offering search and filters, and dropping the page numbers, is usually the better product decision as well as the faster one.
The total count is its own problem
Showing how many results there are means counting them, and an exact count on a large filtered table can cost more than the page itself. Options, in increasing order of honesty about the trade-off: count with a cap and show that there are more than that, use the database's estimate, or drop the number.
Check whether anything depends on the exact figure before computing it on every page load. Frequently the number is on screen because it was easy to add, not because anyone uses it.
Index for the order you actually use
Keyset pagination is only fast with an index matching the sort — the same columns, in the same direction, including the tiebreaker. Allowing users to sort by several columns means an index per supported order, or accepting that the uncommon ones are slower.
Encode the cursor as an opaque value rather than exposing the raw column values, so the interface is free to change its ordering later without breaking clients that stored a cursor.
Common questions
Why do deep pages get slower?
Because an offset asks the database to produce every row up to that point in order and discard them. An offset of twenty thousand walks past twenty thousand rows to return fifty, so cost grows with page depth and with the table.
What is keyset pagination?
Remembering the last row seen and asking for rows after it, rather than counting rows to skip. With an index on the ordering columns the database seeks straight to the position, so every page costs the same as the first.
What is the downside of cursor pagination?
You cannot jump to an arbitrary page number, since a cursor is a position rather than a count. For infinite scroll or next-page links that costs nothing; for numbered pages it is a visible change — though deep numbered pages are rarely how people actually find things.
Why does offset pagination show duplicate rows?
Because rows shift between pages when data is inserted or deleted while someone pages through, so one row is seen twice and another never appears. Keyset pagination anchors on a row rather than a count, which avoids it.