Sub topic 12
Why Large Offsets Get Slow
Assume you know basic SELECT, FROM, WHERE, ORDER BY, LIMIT, and OFFSET. This chapter focuses on what the database must do when a query skips many rows, and how keyset pagination avoids unnecessary work.
What `OFFSET` Costs
OFFSET tells the database to skip rows before returning results. For example, this query requests page 1,001 when each page has 10 rows:
SELECT order_id, order_date, total
FROM orders
ORDER BY order_date DESC, order_id DESC
LIMIT 10 OFFSET 10000;The database cannot usually jump directly to row 10,001. It must find rows in the requested order, count the first 10,000 rows, discard them, and return the next 10.
The query returns only 10 rows, but the database may process at least 10,010 rows. As the offset grows, the amount of discarded work grows too.
An index can make finding rows in order faster, but it does not automatically remove the cost of counting and skipping a large number of rows. An index on the sorting columns is often useful:
CREATE INDEX orders_by_date_id
ON orders (order_date DESC, order_id DESC);The exact index syntax and usefulness depend on the database system, table size, filters, and query plan. Use EXPLAIN or your database's equivalent to inspect the work.
Why Results Can Shift Between Pages
Pagination with OFFSET also depends on a stable ordering. Without ORDER BY, SQL does not promise a row order. Even with ORDER BY order_date, multiple orders may have the same date, so their relative order can change.
Add a unique tie-breaker, such as order_id:
SELECT order_id, order_date, total
FROM orders
ORDER BY order_date DESC, order_id DESC
LIMIT 10 OFFSET 20;Data changes can still shift offset pages. If a new order is inserted near the beginning, every later row moves by one position. A user going from one page to the next may see a duplicate or miss a row.
Keyset Pagination
Keyset pagination, also called seek pagination, uses the last row from the current page as a cursor. The next query asks for rows after that cursor in the chosen sort order.
Suppose the last row on the current page has order_date = '2024-03-15' and order_id = 1042. For descending order, request rows that sort after it:
SELECT order_id, order_date, total
FROM orders
WHERE (order_date, order_id) < ('2024-03-15', 1042)
ORDER BY order_date DESC, order_id DESC
LIMIT 10;The database can use an index to seek near the cursor and read the next 10 matching rows. It does not need to scan and discard all earlier pages.
Keyset pagination needs a cursor containing every sort key required to identify a position. It also works best when the WHERE and ORDER BY columns have a suitable index. The row-value comparison shown above is supported by PostgreSQL, MySQL, and SQLite 3.15 and later. Other systems may require equivalent AND and OR conditions.
Choosing Between the Methods
| Requirement | Suitable approach |
|---|---|
| First few pages with numbered navigation | `LIMIT` and `OFFSET` |
| Very deep pages | Keyset pagination |
| A “next” or “previous” feed | Keyset pagination |
| Jump directly to page 100 | `OFFSET`, with its performance cost |
Keyset pagination is faster for deep, sequential navigation, but it is less convenient for jumping to an arbitrary page number. Store the cursor safely, validate it, and keep the ordering consistent between requests.
Exercise
A query uses ORDER BY order_date DESC, order_id DESC LIMIT 20 OFFSET 50000. What work can the database perform unnecessarily, and what keyset condition could replace the offset if the previous page ended at date '2024-06-01' and ID 900?
Show answer
It may scan and discard about 50,000 earlier rows before returning 20 rows. A keyset version can use WHERE (order_date, order_id) < ('2024-06-01', 900) with the same descending ORDER BY and LIMIT 20.