Sub topic 11
Ordering columns in a composite index
A composite index has several columns, and Oracle can only search it efficiently from the left. This chapter covers how to choose the column order. It assumes you can read a basic DBMS_XPLAN output and know what a composite index is, as covered in the earlier chapters on composite indexes and on access and filter predicates.
Why order matters
A B-tree index stores entries sorted by the first column. Entries with the same first value are sorted by the second column, and so on. This is the same ordering as a phone book sorted by last name, then first name.
Oracle can descend the tree to a precise position only while each column so far has an equality condition. After the first column with a range condition (>=, <, BETWEEN, LIKE 'abc%'), the entries that match are no longer in one contiguous block for the later columns. Oracle then scans the whole range and checks the remaining columns one entry at a time.
That produces the central rule:
- Columns compared with
=go first. - The column with a range condition goes after them.
- Columns used only for sorting or covering come last.
The same columns in two orders
Take a query that finds one customer's orders in one month:
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';Create two candidate indexes and compare them:
CREATE INDEX orders_cust_date_ix ON orders (customer_id, order_date);
CREATE INDEX orders_date_cust_ix ON orders (order_date, customer_id);With (customer_id, order_date), both predicates narrow the search. Oracle finds the first entry for customer 42 on January 1 and reads forward until the date reaches February 1. Every entry it reads is a match.
With (order_date, customer_id), only the date range narrows the search. Oracle reads every entry for January, for all customers, and tests customer_id = 42 on each one. The work grows with the number of orders in the month, not the number of orders for that customer.
Seeing the difference in a plan
Check each index with EXPLAIN PLAN. Use the INDEX hint to force the one you want to inspect:
EXPLAIN PLAN FOR
SELECT /*+ INDEX(o orders_date_cust_ix) */ order_id, order_date
FROM orders o
WHERE customer_id = 42
AND order_date >= DATE '2026-01-01'
AND order_date < DATE '2026-02-01';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +PREDICATE'));For orders_date_cust_ix, the predicate section typically looks like this:
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ORDER_DATE">=TO_DATE(' 2026-01-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss')
AND "ORDER_DATE"<TO_DATE(' 2026-02-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
filter("CUSTOMER_ID"=42)customer_id = 42 appears under filter, not access. The index is used, but it reads every January entry before throwing most of them away. With orders_cust_date_ix, all three conditions appear under access. A plan that uses the index but shows your most selective condition as a filter is a sign that the column order is wrong.
Selectivity is not the first rule
A common belief is that the most selective column belongs first. Selectivity is the fraction of rows a condition matches. For a single query that uses only equality conditions, the order of the columns makes little difference to how many entries are read, because all the equality conditions narrow the search together.
What matters more is how the conditions are used:
- Equality versus range. An equality column placed after a range column loses its power to narrow the search.
- Reuse by other queries. An index on
(a, b)can also serve queries that filter only ona. An index on(b, a)serves queries that filter only onb. Put first the column that other queries use on its own. - Sorting. Columns in the right position can remove a sort step.
Selectivity is a tie-breaker. When two equality columns are used together in every query, either order works for that query. Choose the order that helps your other queries.
Serving several queries with one index
Suppose the application runs these three queries:
-- Q1: orders for one customer
SELECT order_id FROM orders WHERE customer_id = 42;
-- Q2: one customer, one status
SELECT order_id FROM orders
WHERE customer_id = 42 AND status = 'OPEN';
-- Q3: all open orders
SELECT order_id FROM orders WHERE status = 'OPEN';An index on (customer_id, status) serves Q1 and Q2 with an access predicate on the leading column. It does not help Q3 much. An index on (status, customer_id) serves Q3 and Q2, but not Q1. If Q1 is frequent, the first order is better. If Q3 returns a large share of the table, an index may not help it anyway, because the optimizer will likely choose a full table scan for such a low-selectivity condition.
Do not create (customer_id) as a separate index when (customer_id, status) exists. The composite index already covers every query the single-column index could serve, and the extra index only adds maintenance cost on every insert, update and delete.
Order and sorting
Index order also gives you sorted output. With orders_cust_date_ix, this query needs no sort step:
SELECT order_id, order_date
FROM orders
WHERE customer_id = 42
ORDER BY order_date;Within one customer_id, the entries are already sorted by order_date. The plan shows INDEX RANGE SCAN and no SORT ORDER BY. This works only because customer_id has an equality condition. If the query used customer_id >= 42, the entries would span many customers and the dates would no longer be in one sorted run.
Compare three layouts for WHERE customer_id = 42 AND status = 'OPEN' ORDER BY order_date:
| Index columns | Access predicates | Sort step needed |
|---|---|---|
| `(customer_id, status, order_date)` | `customer_id`, `status` | No |
| `(customer_id, order_date, status)` | `customer_id` only; `status` is checked while reading in date order | No |
| `(status, order_date, customer_id)` | `status` only | Yes, because `customer_id` filters rows out of the date order only after the scan |
The first layout is usually best: equality columns, then the sort column.
Skip scan is a fallback
If a query omits the leading column, Oracle can sometimes perform an index skip scan. It treats each distinct value of the leading column as a separate small index and searches each one. This only pays off when the leading column has very few distinct values, such as a flag with two values. Do not design an index relying on it. A leading column with many distinct values makes a skip scan about as slow as reading the whole index.
A checklist for choosing the order
- List the real queries that will use the index, with the conditions on each column.
- Put the columns tested with
=first. - Add at most one range column next. Columns after it are checked as filters.
- If the query sorts, add the
ORDER BYcolumns after the equality columns, in the same order and direction as the sort. - For ties among equality columns, put first the column that other queries use alone.
- Confirm in
DBMS_XPLANthat the conditions you expected are underaccess.
Exercise
A query is WHERE status = 'OPEN' AND region_id = 7 AND created_at >= DATE '2026-01-01' ORDER BY created_at. Another frequent query filters only on region_id. Propose a composite index, say which columns will appear as access predicates, and explain your column order.
Show answer
Use (region_id, status, created_at). The two equality columns come first, with region_id leading so the second query, which uses only region_id, can also use the index. created_at is last because it has the range condition and the ORDER BY. All three conditions are access predicates, and the rows come out in created_at order, so no sort step is needed. (status, region_id, created_at) would serve the main query equally well, but not the region_id-only query.