Sub topic 14
Using Index Order for `ORDER BY` and Top-N Queries
Assume you know basic SELECT, WHERE, and ORDER BY syntax, and that you can inspect Oracle execution plans with DBMS_XPLAN. This chapter focuses on how an index can provide rows in the required order, so Oracle does not need to sort them.
How an index can provide sorted rows
A B-tree index stores key values in order. Oracle can walk the index from the smallest key to the largest key, or backward from the largest to the smallest. If the requested ORDER BY matches that order, Oracle may avoid a separate SORT ORDER BY operation.
For example, an index on orders(order_date) can support both directions:
CREATE INDEX orders_order_date_ix
ON orders (order_date);
SELECT order_id, order_date
FROM orders
ORDER BY order_date ASC;
SELECT order_id, order_date
FROM orders
ORDER BY order_date DESC;Oracle can scan the same index forward for ASC or backward for DESC. Whether it chooses the index depends on the estimated cost. If the query needs most table rows, a full table scan followed by a sort may still be cheaper.
Top-N queries and early stopping
A top-N query returns only the first N rows after ordering. With a compatible index, Oracle can read the first required index entries and stop early.
SELECT order_id, order_date
FROM orders
ORDER BY order_date DESC
FETCH FIRST 10 ROWS ONLY;With an index on order_date, Oracle may read the newest entries first and stop after ten rows. This can avoid reading and sorting the entire table.
For a filtered query, put the filtering column first when the filter is an equality condition:
CREATE INDEX orders_customer_date_ix
ON orders (customer_id, order_date);
SELECT order_id, order_date
FROM orders
WHERE customer_id = 42
ORDER BY order_date DESC
FETCH FIRST 10 ROWS ONLY;Oracle can locate the section for customer 42, walk that section by order_date in reverse order, and stop after ten rows.
Composite indexes and sort direction
Column order and direction both matter. An index on (customer_id, order_date) supports the example above because customer_id is fixed to one value. It does not generally provide global ordering by order_date when many customer IDs are being returned.
An index can be declared with descending columns when the required order is mixed:
CREATE INDEX orders_customer_date_desc_ix
ON orders (customer_id ASC, order_date DESC);
SELECT order_id, customer_id, order_date
FROM orders
ORDER BY customer_id ASC, order_date DESC
FETCH FIRST 20 ROWS ONLY;A normal ascending index can be scanned backward, but that reverses all indexed columns together. It cannot always produce a mixed direction such as ascending customer_id and descending order_date without an additional sort.
Confirming the plan
Use EXPLAIN PLAN and inspect whether Oracle uses an index range scan, an index full scan, or a sort:
EXPLAIN PLAN FOR
SELECT order_id, order_date
FROM orders
WHERE customer_id = 42
ORDER BY order_date DESC
FETCH FIRST 10 ROWS ONLY;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);A plan containing SORT ORDER BY means Oracle still sorts the rows. An index access followed by a stop-key operation indicates that Oracle can stop after finding enough rows.
Exercise
Which index best supports this query: SELECT orderid FROM orders WHERE customerid = 42 ORDER BY order_date DESC FETCH FIRST 10 ROWS ONLY?
Show answer
An index on (customerid, orderdate), optionally with orderdate DESC. The equality on customerid narrows the index section, and Oracle can read that section in date order.