Quick revision
The short answer
I would push back on page numbers, jumping to the last page and an exact total count, because on 300 million rows they need OFFSET or full counting, both of which get slow at depth. Instead I would design cursor-based keyset pagination with an opaque next cursor that encodes the last sort key and id, offer an estimated or capped total such as more than 10,000, and keep filtering by status supported by indexes that match each sort and filter, for example (status, created_at, id). Last page becomes the first page of the reverse sort. Spring Data supports this with Window and KeysetScrollPosition; check your version.
What to remember
In this answer, we pushed back on page numbers, jumps to the last page and exact totals because on 300 million rows they rely on OFFSET or full counts that grow slower every month. We designed a keyset API with opaque, signed cursors that encode the last sort key and id, offered estimated or capped totals, kept a short list of sorts and filters backed by matching indexes such as (status, created_at, id), served last page as the reverse sort, and used Spring Data's Window and KeysetScrollPosition for the implementation.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would push back on page numbers, jumping to the last page and an exact total count, because on 300 million rows they need OFFSET or full counting, both of which get slow at depth. Instead I would design cursor-based keyset pagination with an opaque next cursor that encodes the last sort key and id, offer an estimated or capped total such as more than 10,000, and keep filtering by status supported by indexes that match each sort and filter, for example (status, created_at, id). Last page becomes the first page of the reverse sort. Spring Data supports this with Window and KeysetScrollPosition; check your version.
Why this question is asked
Product requirements for lists usually come from familiar interfaces: page 1 of 2,431, numbered links, a last page button. On small tables that is easy. On a 300 million row orders table each of those features has a real cost, and a good engineer explains the cost and offers an alternative that meets the underlying need. Interviewers ask this design question to see whether we can negotiate requirements with numbers, not just say no. In this answer, we will look at what each requested feature costs, design the cursor API, deal with the total count, choose indexes, map last page onto a reverse sort, and implement it in Spring.
1What Each Requested Feature Costs
Page numbers mean that page n starts at row (n - 1) x 50, which is OFFSET. As we know, OFFSET reads and discards every row before the page, so page 10,000 of 50 rows reads 500,000 rows. Jumping to the last page is the worst case of the same thing, because the database must count through the whole filtered set to find where it ends. And an exact total count, the number of orders matching status = 'SHIPPED', means visiting every matching row or index entry on every request; for 120 million shipped orders that is seconds of work, just to print a number in a footer.
Each of these costs grows with the table, so they get worse every month. Before pushing back, it is worth asking what the user really needs. Usually it is: see recent orders, filter them, go forward and backward, and have a rough idea of how many there are.
| Requested | Cost on 300M rows | Proposal |
|---|---|---|
| Page numbers | OFFSET reads all earlier rows | opaque next and previous cursors |
| Jump to last page | count or scan the whole filtered set | reverse sort: oldest first |
| Exact total count | visit every matching row per request | estimate or capped count |
| Filter by status | fine with the right index | keep, index (status, created_at, id) |
2The Cursor-Based API
The API returns a page of orders plus an opaque cursor for the next page. Opaque means the client treats it as a meaningless string: internally it encodes the sort key and the id of the last row, and the client just sends it back.
The cursor here is a Base64 encoding of {created_at, id} plus a short signature. Encoding hides the format, so we can change it later, for example adding a sort direction, without breaking clients. Signing it with a server-side key, or at least validating it strictly, prevents clients from crafting cursors to probe arbitrary positions or inject values. The filter and sort are echoed into the cursor too, so a cursor cannot be reused with a different filter.
3Dealing With the Total Count
We offer three options, depending on what the screen needs. An estimate is almost free: for an unfiltered list, PostgreSQL's reltuples in pg_class gives the approximate row count, and for a filter, the row estimate from EXPLAIN gives a number that is usually within a reasonable factor. A capped count answers more than 10,000 by counting at most 10,001 rows, which is fast because it stops early. And an exact count can be allowed for narrow, indexed subsets, such as one customer's orders, where it is cheap.
The response says which kind of number it is, like the relation gte above, so the interface can show about 300 million or more than 10,000 honestly instead of pretending to precision it cannot afford.
4Indexes for Each Supported Sort and Filter
Keyset pagination is fast only when an index matches the filter and the sort exactly, with the filter columns first, then the sort columns, then id as the tie-breaker. For the list of shipped orders newest first, that is (status, created_at, id): the B-tree jumps to status = 'SHIPPED', and within it the entries are already in created_at, id order, so the database seeks to the cursor and reads 50 entries.
This is why the API should support a short list of sorts and filters, not arbitrary combinations. Each one we support needs its own index, and each index slows down writes on a 300 million row table. Two or three sorts with status as an optional filter is a reasonable contract; any column, any order is not.
5Last Page Without OFFSET
Jump to the last page usually means show me the oldest orders. With cursors, that is simply the first page of the reverse sort: sort=created_at_asc with no cursor. The same index serves it, scanned in the other direction. Previous works the same way, by flipping the comparison and the order for one request. So the user can reach both ends and move in both directions; what they lose is jumping to page 4,812 in the middle, which in practice nobody does on purpose, and which a date filter serves better.
6Implementing It With Spring Data
Spring Data has built-in support for keyset scrolling. A repository method returns a Window, a page of results that knows its position, and takes a ScrollPosition; a KeysetScrollPosition holds the key values of the last row.
Spring Data generates the keyset WHERE clause from the sort and the stored key values, and hasNext tells us whether to return a next cursor. We still own the encoding and signing of the cursor at the API boundary, and the indexes. The exact API names have changed between Spring Data releases, so check your version.
7Follow-up questions
What do you say if product insists on page numbers?
I would show the numbers: the response time for page 1, page 1,000 and page 10,000 on a production-sized copy, and the cost of the exact count. Then I would offer a compromise, such as page numbers for the first few pages, where OFFSET is cheap, with cursors beyond that, or numbered pages only within a narrow filter like one customer. Usually seeing that the last pages take seconds settles the discussion.
Why encode the cursor instead of exposing created_at and id as parameters?
Because clients then build their own cursors and depend on the format, so we can never change it. An opaque, signed token lets us add fields, change the sort key or switch to another pagination strategy behind the same contract, and lets us reject tampered or mismatched cursors.
What happens if the row the cursor points to is deleted?
Nothing breaks. The cursor holds key values, not a reference to a row, and the query asks for rows after those values, so it continues from the same position in the order even if that row no longer exists.
8Conclusion
In this answer, we pushed back on page numbers, jumps to the last page and exact totals because on 300 million rows they rely on OFFSET or full counts that grow slower every month. We designed a keyset API with opaque, signed cursors that encode the last sort key and id, offered estimated or capped totals, kept a short list of sorts and filters backed by matching indexes such as (status, created_at, id), served last page as the reverse sort, and used Spring Data's Window and KeysetScrollPosition for the implementation.
Open with
I would push back on three things and keep the rest. Page numbers mean OFFSET, so page 10,000 reads half a million rows just to throw them away.
Then, if they want more
Jumping to the last page is the worst case of that. And an exact total count of, say, 120 million shipped orders means touching every matching row on every request. All three get slower as the table grows. Filtering by status is fine with the right index. So I would propose cursor-based keyset pagination.
The response contains the items and an opaque next cursor, which internally encodes the created_at and id of the last row, Base64 and signed, along with the filter and sort, so clients cannot craft or reuse cursors wrongly and we can change the format later.
The query is then WHERE status equals the filter and the pair created_at, id is less than the cursor, with a matching ORDER BY and a limit.
For counts I would offer an estimate from statistics or a capped count, like more than 10,000, which stops counting early, and exact counts only for small indexed subsets, such as one customer's orders. Each supported sort and filter gets a matching index, like status, created_at, id, so I would limit the API to a few sorts.
Last page becomes the first page of the reverse sort. In Spring Data I can implement it with Window and KeysetScrollPosition, keeping the cursor encoding at the API boundary.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Product wants a paginated orders API with page numbers, total count, filtering by status...": https://scrivio-interview-library.netlify.app/databases/query-optimization/1c112a0d10/