Sub topic 11
Ordering Columns in a Composite Index
Assume you know basic SELECT, WHERE, JOIN, and ORDER BY syntax, and that you can read an execution plan with DBMS_XPLAN. A composite index is an index that stores more than one column in a specified order. The order determines which queries can search the index efficiently.
The leading-column rule
Consider this index:
CREATE INDEX orders_cust_date_ix
ON orders (customer_id, order_date);Oracle sorts index entries first by customer_id, then by order_date within each customer. A query that supplies the first column can navigate to a narrow part of the index:
SELECT order_id, order_date
FROM orders
WHERE customer_id = 42
AND order_date >= DATE '2026-01-01'
AND order_date < DATE '2026-02-01';Both predicates can help locate rows. The equality condition identifies one customer, and the date range identifies part of that customer's entries.
A query that uses only order_date skips the leading column:
SELECT order_id
FROM orders
WHERE order_date >= DATE '2026-01-01'
AND order_date < DATE '2026-02-01';This index is not ordered by date across all customers. Oracle might use an index skip scan, but it must search separate portions for possible customer_id values. A full table scan may be cheaper.
Choosing an order for common predicates
For many searches, put columns tested with equality before columns tested with a range:
CREATE INDEX orders_status_date_ix
ON orders (status, order_date);This supports queries such as:
SELECT order_id, order_date
FROM orders
WHERE status = 'SHIPPED'
AND order_date >= DATE '2026-01-01'
AND order_date < DATE '2026-02-01';The range column is often placed after equality columns because a range covers many index entries. Columns after that range usually provide less benefit as access conditions. Oracle can still apply them as filters.
| Query requirement | Often useful column position |
|---|---|
| Equality predicate | Before range columns |
| Range predicate | After equality columns |
| Required sort order | Position that matches the search and ordering |
| Column used only occasionally | Include only if the workload justifies it |
Selectivity matters, but “most selective first” is not a universal rule. A less selective equality column can be the right leading column when it appears in most important queries. The index must match the workload, not just the data distribution.
Matching an `ORDER BY`
An index can also provide rows in sorted order. For example, an index beginning with customer_id, order_date can help this query for one customer:
SELECT order_id, order_date
FROM orders
WHERE customer_id = 42
ORDER BY order_date DESC
FETCH FIRST 10 ROWS ONLY;Oracle may walk the matching index entries in descending order and stop after ten rows. That can avoid sorting and reading unnecessary rows.
Exercise
Which index is generally a better match for a query filtering by customer_id = 42 and a date range: (customer_id, order_date) or (order_date, customer_id)? Why?
Show answer
(customer_id, order_date) is generally better because the equality predicate uses the leading column, allowing Oracle to search one customer's date range directly. The best choice still depends on the complete workload.