Paging deep pages → keyset pagination

ROWNUM paging gets slower toward later pages; change it to read from the last key of the previous page.

SQL

Situation

A paid order list screen shows 20 rows per page, newest first. The first page is fast, but later pages, such as page 5,000, get slower and slower.

Tables

ORDERS (2 million rows) holds orders accumulated at 2,000 per day since 2023-01-01. There are 140,000 paid (PAID) orders.

ColumnTypeDescription
ORD_IDNUMBEROrder ID (PK)
STATUSVARCHAR2DONE 90%, PAID 7%, CANCELED 3%
ORD_DTDATEOrder date and time
IndexColumns
ORDERS_X2STATUS, ORD_DT, ORD_ID

Fetching page 5,001

Fetch the 20 paid orders ranked 100,001–100,020, newest first. This is the common three-level ROWNUM paging.

BEFORE · Three-level ROWNUM paging

-- Rows 100,001-100,020 (page 5,001 at 20 rows per page)
SELECT *
FROM (
  SELECT ROWNUM AS rn, a.*
  FROM (
    SELECT *
    FROM orders
    WHERE status = 'PAID'
    ORDER BY ord_dt DESC, ord_id DESC
  ) a
  WHERE ROWNUM <= 100020
)
WHERE rn > 100000

Run it and check Buffers and the A-Rows of each operation. The result is 20 rows; see how many rows are read.

Approach

1. First check whether the sort was eliminated

If the execution plan has no SORT ORDER BY and shows INDEX RANGE SCAN DESCENDING + COUNT STOPKEY, the index has eliminated the sort. Partial range processing itself is working. If it is still slow, the problem is where it stops.

2. See how many rows are read and thrown away

A-Rows show that 100,020 rows are read from the index and the table, and the first 100,000 of them are thrown away. Table access also happens 100,020 times. Only the last 20 rows reach the screen.

3. Change the page number and see how the cost changes

Changing it to ROWNUM <= 1000, 10000, and 100000 shows Buffers growing in proportion to the page number. A position such as "from the Nth row" can only be known by counting from the start. However the query is refined, this approach keeps the counting cost.

4. Think about switching from "which row number" to "where to start"

When requesting the next page, the screen already knows the last row of the previous page. Given that row's sort key values, the query can go straight to that position in the index and read only 20 rows from there.

  • Check that the sort key is unique. ORD_DT alone can have several orders at the same time, so ORD_ID must be used along with it.
  • In Predicate Information, check that the condition expressing "after this position" with the two columns is used as the index search start point (access).

This approach gives up "jump to page N". It works when the screen requirement is next/previous buttons or infinite scroll.

Explanation

Keyset pagination

Take the key of the last row of the previous page (:last_dt, :last_id) and read from the next one.

AFTER · Keyset pagination

SELECT *
FROM (
  SELECT *
  FROM orders
  WHERE status = 'PAID'
    AND ord_dt <= :last_dt
    AND (ord_dt < :last_dt OR ord_id < :last_id)
  ORDER BY ord_dt DESC, ord_id DESC
)
WHERE ROWNUM <= 20

Why it got faster

  • It is INDEX RANGE SCAN DESCENDING + COUNT STOPKEY. Buffers stays constant whatever the page.
  • In Predicate Information, check that ORD_DT <= :LAST_DT is used as access.
  • Run it with different bind values. The cost is the same wherever it starts.

Why this is optimal

  • The start position is found directly in the index, and the scan stops once 20 rows are filled. The cost of counting earlier pages is gone, so the amount read is on the same level as the number of result rows.
  • The remaining cost is visiting the table once for each of the 20 rows (TABLE ACCESS BY INDEX ROWID). It is unavoidable because of SELECT *.
  • (ord_dt < :last_dt OR ord_id < :last_id) remains as a filter that discards the few rows with the same time at the start position. Only a few rows are discarded, so the cost is negligible.

Key points

  • Oracle does not support tuple comparisons such as (a, b) < (x, y). Writing it out produces an OR, which may prevent the index search start point from being found. So ord_dt <= :last_dt is written once more, separately, to create an access condition.
  • The sort key must be unique (the reason ORD_ID is appended). If it is not unique, rows are skipped or duplicated at page boundaries.
  • Trade-off: "jump to page N" is not possible. This approach fits infinite scroll, next/previous buttons, and batch processing.