Top-N partial range processing (sort elimination)

When the index order matches the ORDER BY, the query reads only the N rows it needs and stops. The sort operation disappears too.

SQL

Situation

A screen shows "the latest 10 paid orders". The screen shows only 10 rows, but the response is slow.

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
AMTNUMBEROrder amount
IndexColumns
ORDERS_X2STATUS, ORD_DT, ORD_ID

Latest paid orders

The query sorts paid orders newest first and sends them all. The app uses only the first 10.

BEFORE · Full query

-- Sort and send the whole result; the app uses only the first 10 rows
SELECT *
FROM orders
WHERE status = 'PAID'
ORDER BY ord_dt DESC

Run it and check Buffers and A-Rows. The screen needs 10 rows; see how many rows the DB reads and processes, and whether there is a sort operation.

Approach

1. Compare the rows needed with the rows processed

The screen shows 10 rows. If A-Rows and Buffers in the execution plan are on the scale of 140,000 rows, the DB processed every row, including the ones that get thrown away. When the app cuts the result, the DB has already processed all of it.

2. Move the cut inside the SQL

If only N rows are needed, cut them in SQL. That way the optimizer knows that "only N rows need to be returned" and can stop partway. In Oracle the options are ROWNUM and FETCH FIRST.

3. Check when ROWNUM is assigned

ROWNUM is a number assigned at the moment a row goes out as a result. It is applied before ORDER BY, so in the same query block it picks any 10 rows and then sorts them. This is not a performance problem: the result is wrong.

TRY · ROWNUM bug

SELECT *
FROM orders
WHERE status = 'PAID'
  AND ROWNUM <= 10
ORDER BY ord_dt DESC

The cut must happen after the sort is done. Think about where the sort needs to go.

4. See whether the index can replace the sort

Moving the cut helps little if the sort remains, because sorting requires reading every target row. Check whether the index column order satisfies both the WHERE condition and the ORDER BY. If STATUS is an equality condition and the next column is ORD_DT, the order in which the index is read is the result order.

If the sort column is not in the index, this method cannot be used. In that case every target row is read even after the cut is moved.

Explanation

ROWNUM inline view

AFTER · ROWNUM inline view

SELECT *
FROM (
  SELECT *
  FROM orders
  WHERE status = 'PAID'
  ORDER BY ord_dt DESC
)
WHERE ROWNUM <= 10

Why it got faster

  • After resolves to INDEX RANGE SCAN DESCENDING + COUNT STOPKEY. There is no SORT ORDER BY.
  • Buffers drops to a few dozen blocks. This holds even with 140,000 paid orders.

FETCH FIRST

AFTER · FETCH FIRST (12c or later)

SELECT *
FROM orders
WHERE status = 'PAID'
ORDER BY ord_dt DESC
FETCH FIRST 10 ROWS ONLY

Why it got faster

  • Same principle as the inline view. FETCH FIRST is internally rewritten into a ROW_NUMBER()-based VIEW + WINDOW NOSORT STOPKEY. The amount read is the same.

Why this is optimal

  • The index is read from the end, and the scan stops as soon as 10 rows are filled. It reads only as many rows as the result, so there is no room for further reduction.
  • The remaining cost is visiting the table once for each of the 10 rows (TABLE ACCESS BY INDEX ROWID). It is unavoidable because of SELECT *.

Variation: sorting by a column not in the index

TRY · Top 10 by amount

SELECT *
FROM (
  SELECT *
  FROM orders
  WHERE status = 'PAID'
  ORDER BY amt DESC
)
WHERE ROWNUM <= 10
  • SORT ORDER BY STOPKEY appears. Sort memory holds only 10 rows, but all 140,000 target rows are read.

Key points

  • Partial range processing works only when "index read order = result order". If a sort is needed, everything must be read before it can be sorted.
  • ROWNUM is a number assigned at the moment a row goes out as a result. To cut after sorting, always wrap the query in an inline view.
  • If the screen shows only N rows, cut them in SQL. When the app cuts the result, the DB has already processed all of it.