Skip to content

MySQL and InnoDB in depth · Part 10 of 10

Pagination in MySQL: OFFSET, keyset and deferred join

LIMIT with a large OFFSET gets slower with every page because MySQL reads and discards the skipped rows. Keyset pagination and the deferred join keep deep pages fast.

· 4 min read

mysqlpaginationperformance

Almost every list in an application is paginated, and the obvious way to do it works well until a table gets large. Understanding why it slows down tells you exactly how to fix it.

Why OFFSET gets slower

SQL
SELECT * FROM transactions
WHERE wallet_id = 7
ORDER BY id DESC
LIMIT 20 OFFSET 100000;

MySQL cannot jump to row number 100,001. It must read 100,020 rows in order, throw away the first 100,000 and return 20. The work grows with the offset, so page 1 is fast and page 5,000 is slow, and every row that is skipped may also require a lookup into the clustered index to fetch columns that are then discarded. Concurrent inserts add a second problem: the content of each page shifts, so users see duplicates or miss rows when moving between pages.

Keyset (cursor) pagination

Instead of counting rows to skip, remember where the previous page ended and ask for rows after that point. The client sends the key of the last row it received.

SQL
SELECT * FROM transactions
WHERE wallet_id = 7 AND id < 98765
ORDER BY id DESC
LIMIT 20;

With an index on (wallet_id, id), MySQL seeks straight to the starting point and reads 20 entries, no matter how deep the user is. The cost no longer depends on the page number, and results stay stable while rows are inserted at the top.

Sorting by a column that is not unique

Often you sort by a timestamp, and many rows can share the same value. A cursor on created_at alone would skip or repeat rows at the boundary. Add the primary key as a tie-breaker in both the sort and the condition.

SQL
SELECT * FROM transactions
WHERE wallet_id = 7
  AND (created_at < '2026-03-01 10:00:00'
       OR (created_at = '2026-03-01 10:00:00' AND id < 98765))
ORDER BY created_at DESC, id DESC
LIMIT 20;

This pairs with an index on (wallet_id, created_at, id). A row-value comparison such as (created_at, id) < (?, ?) is shorter to write, but check with EXPLAIN that your MySQL version turns it into an index range, because older versions handled it poorly. The expanded OR form above is the safe choice.

Trade-offs of keyset pagination

  • You can go to the next or previous page, but not jump to "page 372". It suits infinite scroll, feeds and APIs, and less so numbered page links.
  • The cursor should be opaque to clients, for example an encoded string, so you can change its contents later.
  • Going backwards needs the reverse comparison and sort order, then reversing the result.

Deferred join when OFFSET is required

If the product really needs page numbers, reduce the cost of skipped rows. Select only the primary keys through a narrow index first, then join back to fetch full rows for just the 20 you need.

SQL
SELECT t.*
FROM transactions t
JOIN (
  SELECT id FROM transactions
  WHERE wallet_id = 7
  ORDER BY id DESC
  LIMIT 20 OFFSET 100000
) page ON page.id = t.id
ORDER BY t.id DESC;

The inner query reads only index entries, which is much cheaper than reading wide rows to discard them. It still walks the skipped entries, so it is a mitigation, not a cure. Combine it with a cap on how deep users can page.

Counting totals. Showing "page 3 of 500" requires COUNT(*) over the whole filtered set, which is expensive on large tables. Options are to drop the total and show only next and previous, to show an approximate figure ("about 10,000 results"), or to cache the count and refresh it periodically.

Takeaway

OFFSET pagination reads and discards every skipped row, so deep pages get slow and unstable. Use keyset pagination with a unique tie-breaker for feeds and APIs, a deferred join when page numbers are unavoidable, and avoid exact totals on big tables. Verify the plan with EXPLAIN.

A knowledge article, drafted with AI assistance and checked with automated tests. It is general background, not a description of my own experience or of any client's work.

Comments

Leave a comment

No account or email needed. Comments appear after review. A one-way hash of your IP address is kept for 30 days to fight spam, then erased.