Sub topic 9
Composite indexes and the leading column
Assume you know basic SELECT, WHERE, and ORDER BY syntax, Oracle data types, and the general purpose of an index. This chapter explains how column order affects a composite index, which is an index containing two or more columns.
How a composite index is organized
Consider this index:
CREATE INDEX orders_customer_date_ix
ON orders (customer_id, order_date);Oracle stores index entries ordered first by customer_id. Within each customer, entries are ordered by order_date. The first column is the leading column.
This order supports searches that begin with customer_id:
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';Oracle can find the entries for customer 42, then scan only that customer's date range.
The same index is less useful when a query skips the leading column:
SELECT order_id
FROM orders
WHERE order_date >= DATE '2026-01-01'
AND order_date < DATE '2026-02-01';Oracle cannot jump directly to all January dates because the index is divided first by customer. It might choose a full table scan or another access path.
Equality, ranges, and column order
A common design places equality columns before range columns:
CREATE INDEX orders_status_customer_date_ix
ON orders (status, customer_id, order_date);This can help a query that fixes status and customer_id, then searches a date range. Once Oracle reaches a range on one index column, later columns generally narrow the scan less effectively as access conditions.
| Query condition | Typical index benefit |
|---|---|
| `status = 'OPEN' AND customer_id = 42` | Uses the leading columns efficiently |
| `status = 'OPEN' AND order_date >= ...` | Uses `status`; `order_date` is not the next constrained column |
| `customer_id = 42 AND order_date >= ...` | Skips `status`, so this index may not be a good match |
Column order should follow the important query patterns, not a universal rule that says the most selective column always comes first. Test candidate indexes with realistic data and execution plans.
Index skip scans
Oracle can sometimes use an index skip scan, which searches separate groups of leading-column values while using a later column. This may help when the leading column has few distinct values. It is not a substitute for a well-designed index because it can require multiple probes and may cost more than a table scan.
Verify the access path
Use an execution plan to check whether Oracle uses the intended index:
EXPLAIN PLAN FOR
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';
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);Look for ORDERS_CUSTOMER_DATE_IX and an index range scan. The plan is the evidence; the presence of an index alone does not guarantee that Oracle will choose it.
Exercise
An index exists on (customer_id, order_date). Which query generally matches its leading-column order better: a query filtering only order_date, or one filtering customer_id and a date range? Why?
Show answer
The query filtering customer_id and a date range matches better. Oracle can locate the customer portion first, then scan the relevant dates.