Page 1 of your product list loads in 5 ms. Page 5,000 takes 4 seconds. The query is the same except for one number:
SELECT id, name, price
FROM products
ORDER BY created_at DESC, id DESC
LIMIT 20 OFFSET 100000;
This is how OFFSET works in every major database, not a MySQL bug. The fix is to change how you paginate.
Why OFFSET gets slower with every page
A database can't jump to "the 100,001st row". A B-tree index is sorted, but it doesn't record each row's position. To serve OFFSET 100000, the engine walks the index from the start, reads 100,020 rows, throws away the first 100,000, and returns 20.
The cost grows in a straight line with the page number. Page n costs O(n). A crawler or a user clicking "last page" forces a near-full scan on every request. In MySQL it's worse when the index doesn't cover the query, because InnoDB fetches the full row for each discarded entry too.
OFFSET also has a correctness problem: if a row is inserted or deleted while someone is paging, everything shifts by one. Users see duplicates or miss rows between pages.
Keyset pagination (the "seek method")
Instead of "skip N rows", ask for "the rows after the last one I saw". Remember the sort key of the last row on the page and continue from there:
-- First page
SELECT id, name, price, created_at
FROM products
ORDER BY created_at DESC, id DESC
LIMIT 20;
-- Next page: pass the last row's (created_at, id)
SELECT id, name, price, created_at
FROM products
WHERE (created_at, id) < ('2026-09-20 14:03:11', 88412)
ORDER BY created_at DESC, id DESC
LIMIT 20;
With a matching index, every page is an index seek followed by reading 20 entries. Page 5,000 costs the same as page 1.
CREATE INDEX idx_products_created_id ON products (created_at DESC, id DESC);
Three details make or break it:
-
The sort key must be unique.
created_atalone has ties, and rows with the same timestamp would be skipped or repeated. Add the primary key as a tie-breaker. -
The
ORDER BYmust match the index, including direction. -
Row-value comparison
(a, b) < (x, y)works in PostgreSQL, MySQL 8 and SQLite. In SQL Server (and when MySQL doesn't use the index for it), expand it:WHERE created_at < @ts OR (created_at = @ts AND id < @id)
Exposing it in an API
Return an opaque cursor instead of a page number:
{
"items": [ ... ],
"next_cursor": "eyJjIjoiMjAyNi0wOS0yMFQxNDowMzoxMSIsImkiOjg4NDEyfQ"
}
The cursor is just the base64-encoded last (created_at, id). Clients pass it back as ?after=.... This is how Stripe, Slack, GitHub and most large public APIs paginate.
"But I need page numbers"
Keyset pagination gives you next/previous, not "jump to page 347". Options:
-
Ask whether anyone really jumps to page 347. Infinite scroll or "load more" is usually fine, and search is better for finding something specific.
-
Cap the depth. Allow
OFFSETfor the first 50 pages and require filters after that. Google does something similar. -
Deferred join. If you must use
OFFSET, skip over a narrow covering index and only fetch full rows for the final 20:SELECT p.id, p.name, p.price FROM products AS p JOIN ( SELECT id FROM products ORDER BY created_at DESC, id DESC LIMIT 20 OFFSET 100000 ) AS page USING (id) ORDER BY p.created_at DESC, p.id DESC;It's still O(n), but it scans only index entries, which is often 10× faster in MySQL.
Don't forget COUNT(*)
"Showing page 3 of 48,112" needs an exact COUNT(*) over the whole filtered set on every request, which can cost more than the page query itself. Show "Next" without a total, cache the count, or use an estimate (for example EXPLAIN row estimates or pg_class.reltuples) when an exact number isn't needed.
Checklist
- Deep pages slow?
OFFSETreads and discards every skipped row. - Switch to keyset pagination with a unique, indexed sort key.
- Expose an opaque cursor, not a page number.
- If you keep
OFFSET, cap the depth and use a deferred join.
Get the weekly commit
New database deep dives every week.
