Sub topic 5
Choosing Column Order in a Composite Index
Assume you know basic SELECT, WHERE, JOIN, and ORDER BY syntax. You should also know that Oracle's optimizer chooses an execution plan and that a table row has a ROWID.
A composite index is an index on two or more columns. Oracle stores the column values as ordered tuples. For an index on (customer_id, order_date), entries are ordered first by customer_id, then by order_date within each customer.
The leading-column rule
The first column is the leading column. Queries that constrain the leading column can usually use the index efficiently.
CREATE INDEX orders_cust_date_ix
ON orders (customer_id, order_date);This index is a good match for the following queries:
SELECT order_id
FROM orders
WHERE customer_id = 42;
SELECT order_id
FROM orders
WHERE customer_id = 42
AND order_date >= DATE '2026-01-01';The first query uses the leading column. The second query narrows the search by customer and then searches the relevant date range.
A query that filters only by order_date does not match the index's normal starting point:
SELECT order_id
FROM orders
WHERE order_date >= DATE '2026-01-01';Oracle might choose a full table scan instead. Oracle can sometimes use an index skip scan, but that depends on statistics and data distribution. Do not design an index assuming that a skip scan will always rescue a missing leading-column predicate.
Compare possible column orders
Suppose your application runs these queries:
| Index order | Usually helps | Usually does not help efficiently |
|---|---|---|
| `(customer_id, order_date)` | Customer lookup; customer plus date range | Date-only lookup |
| `(order_date, customer_id)` | Date lookup; date plus customer filter | Customer-only lookup |
Both indexes contain the same columns, but they provide different access paths. Choose the order from the important query patterns, not from the column names alone.
A common design is to place equality predicates before a range predicate:
CREATE INDEX orders_cust_date_ix
ON orders (customer_id, order_date);Here, customer_id = 42 is an equality condition, while order_date >= ... is a range condition. Oracle can find one customer's section of the index and scan only the matching date range.
This is a useful rule, not an absolute law. Column order can also affect whether the index supports an ORDER BY, how many queries share the index, and how selective the combined predicates are. Test the actual workload.
Check the access path
Use an execution plan to see whether Oracle chose an index range scan or a full table scan.
EXPLAIN PLAN FOR
SELECT order_id
FROM orders
WHERE customer_id = 42
AND order_date >= DATE '2026-01-01';
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);Look for an operation such as INDEX RANGE SCAN on ORDERS_CUST_DATE_IX. TABLE ACCESS FULL means Oracle read the table directly. A full scan is not automatically wrong. If a query returns a large part of the table, scanning the table can cost less than visiting many index entries and table rows.
Exercise
For the index (customer_id, order_date), which query is the stronger match: a query filtering only by order_date, or one filtering by both customer_id and order_date? Why?
Show answer
The query filtering by both columns is the stronger match because it uses the leading column and then narrows the matching customer's date range. The date-only query lacks the leading-column predicate and may require a full table scan.