Sub topic 13
Keyset Pagination as an Alternative
Prerequisites
This chapter assumes that you can write SELECT queries, filter with WHERE, sort with ORDER BY, and identify a table's rows and columns. You should also understand that LIMIT returns a maximum number of rows and OFFSET skips rows before returning results.
Why Large OFFSET Values Get Slow
A page query often looks like this:
SELECT order_id, order_date, total
FROM orders
ORDER BY order_date DESC, order_id DESC
LIMIT 10 OFFSET 100000;The database must find the rows in the requested order, count or process the first 100,000 rows, discard them, and then return the next 10. The database may use an index, but a large offset still requires work proportional to the number of skipped rows.
Large offsets also create a consistency problem. Suppose a new order is inserted between two requests. Rows can move to later or earlier pages. A row might appear twice, or another row might be missed.
Keyset Pagination
Keyset pagination, also called seek pagination, continues from the values of the last row on the previous page. The application sends those values back as a cursor.
Assume that page 1 ended with this row:
| order_date | order_id |
|---|---|
| 2024-03-15 | 1042 |
The next query asks for rows after that position in the descending sort order:
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;For a descending sort, the less-than comparison means “comes later.” The database can use an index beginning with order_date and order_id to seek directly near the cursor, rather than scanning and discarding every earlier page.
The second sort column is important. Many orders can have the same order_date, so the date alone does not identify one exact position. order_id acts as a tie-breaker. It should be unique and stable.
Writing the Predicate Without Row Values
PostgreSQL, MySQL, and SQLite 3.15 and later support the row-value comparison shown above. In systems without that syntax, expand the comparison:
SELECT order_id, order_date, total
FROM orders
WHERE order_date < '2024-03-15'
OR (order_date = '2024-03-15' AND order_id < 1042)
ORDER BY order_date DESC, order_id DESC
FETCH FIRST 10 ROWS ONLY;The second condition handles rows tied on the first sort column.
Trade-Offs and Failure Modes
Keyset pagination is usually faster and more stable for sequential browsing, especially near later pages. It does not provide convenient random access to page 500. A user cannot usually jump directly to that page without traversing earlier cursors.
Choose cursor columns carefully:
- Include a unique, non-null tie-breaker.
- Avoid values that change while a user is paging.
- Handle
NULLsort values explicitly, because comparisons withNULLare not true. - Encode the cursor safely when sending it through an API.
- Store enough cursor data to reproduce the complete sort position.
Inserts and deletes can still change what a user sees, but keyset pagination avoids the offset shift that commonly causes duplicates and omissions.
Exercise
Rewrite this query as keyset pagination for the next page after order date 2024-03-15 and order ID 1042: SELECT orderid, orderdate FROM orders ORDER BY orderdate DESC, orderid DESC LIMIT 10;
Show answer
SELECT orderid, orderdate FROM orders WHERE (orderdate, orderid) < ('2024-03-15', 1042) ORDER BY orderdate DESC, orderid DESC LIMIT 10;