Sub topic 9
Composite indexes and the leading column
This chapter assumes you can read a basic execution plan, as covered earlier in the tutorial, and that you know a B-tree index stores key values in sorted order. It explains how an index on several columns is sorted, why only some queries can search it efficiently, and how to tell from a plan whether yours can.
What a composite index is
A composite index (also called a concatenated or multicolumn index) is a single B-tree index built on two or more columns. Oracle sorts the entries by the first column. Within each value of the first column, it sorts by the second column. Within each pair, it sorts by the third, and so on.
An index on (customer_id, order_date) is ordered like a phone book sorted by last name and then first name. All entries for customer 1 come first, in date order. All entries for customer 2 follow, also in date order. The dates are in order only inside one customer. Across the whole index, the dates are scattered.
That ordering decides which searches are cheap. Oracle can descend the B-tree to a starting point only when it knows a value, or a range, for the first column.
A small table to experiment with
The following script builds a table with 100,000 rows. You can use it to run every example in this chapter.
CREATE TABLE orders (
order_id NUMBER PRIMARY KEY,
customer_id NUMBER NOT NULL,
order_date DATE NOT NULL,
status VARCHAR2(10) NOT NULL
);
INSERT INTO orders
SELECT LEVEL,
MOD(LEVEL * 7, 500) + 1,
DATE '2026-01-01' + MOD(LEVEL * 13, 365),
CASE WHEN MOD(LEVEL, 10) = 0 THEN 'OPEN' ELSE 'SHIPPED' END
FROM dual
CONNECT BY LEVEL <= 100000;
COMMIT;
CREATE INDEX orders_cust_date_ix
ON orders (customer_id, order_date);
BEGIN
DBMS_STATS.GATHER_TABLE_STATS(USER, 'ORDERS');
END;
/The leading column rule
The leading column is the first column in the index definition. A query can use the index for a range scan only if its WHERE clause constrains the leading column. Constraining the leading column with an equality or range condition gives Oracle a place to start reading.
-- Equality on the leading column, range on the second: both narrow the scan
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';
-- Only the second column is constrained: a range scan on this index is not possible
SELECT order_id
FROM orders
WHERE order_date >= DATE '2026-01-01'
AND order_date < DATE '2026-02-01';In the first query, Oracle descends the tree to the entry (42, 2026-01-01) and reads forward until it passes (42, 2026-02-01). It touches only matching entries. In the second query, the matching dates are spread across all 500 customers. No single starting point exists, so Oracle cannot read a contiguous slice of the index.
When the leading column is missing, the optimizer typically chooses a full table scan. Two other choices can appear. If the index contains every column the query needs, Oracle may scan the whole index. It may also use an index skip scan, which a later chapter covers.
Which column combinations work
Take an index on (a, b, c). The pattern is a prefix rule: usable conditions must start at a and continue without gaps.
| Conditions in the WHERE clause | Narrows the index search with | Remaining conditions |
|---|---|---|
| `a` | `a` | none |
| `a` and `b` | `a`, `b` | none |
| `a`, `b` and `c` | `a`, `b`, `c` | none |
| `a` and `c` | `a` only | `c` is checked entry by entry |
| `b` only | nothing | index is not searchable by range |
| `b` and `c` | nothing | index is not searchable by range |
The "a and c" row shows a gap. Oracle can find the range for a. Inside that range, entries for different b values are interleaved, so the c condition cannot narrow the search. Oracle reads every entry in the a range and tests c on each one.
Equality on the leading column also does not guarantee that later columns narrow the scan. Narrowing continues only through the columns that have equality conditions. The first column with a range condition (>, <, BETWEEN, LIKE 'abc%') is the last one that can narrow the scan. Any column after it is only tested on entries already read.
-- Range on the leading column, equality on the second.
-- Oracle reads every entry for customer_id 40 to 45 and tests order_date on each one.
SELECT order_id
FROM orders
WHERE customer_id BETWEEN 40 AND 45
AND order_date = DATE '2026-03-10';Seeing it in the plan
Use EXPLAIN PLAN and DBMS_XPLAN.DISPLAY, then read the "Predicate Information" section.
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);The output should resemble the following. The costs and exact formatting vary by version and statistics.
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| ORDERS |
|* 2 | INDEX RANGE SCAN | ORDERS_CUST_DATE_IX |
Predicate Information (identified by operation id):
2 - access("CUSTOMER_ID"=42 AND "ORDER_DATE">=TO_DATE(' 2026-01-01 ...')
AND "ORDER_DATE"<TO_DATE(' 2026-02-01 ...'))Both columns appear under access on the INDEX RANGE SCAN step. If a column you expected to narrow the scan is missing from access, check whether it has a function applied, an implicit conversion, or a gap before it in the index.
How the search proceeds
The diagram shows the decision Oracle makes for a query against (customer_id, order_date).
Practical consequences
- One composite index replaces a single-column index. The index
(customer_id, order_date)already serves queries oncustomer_idalone. A separate index oncustomer_idadds maintenance cost and gives you almost nothing. An index onorder_datealone is a different matter, because the composite cannot serve it. - Existing indexes can be reused. Before adding an index, check whether a new query's conditions form a prefix of an index you already have.
- Order matters, and the next chapters cover how to choose it. Swapping the two columns gives an index that serves a different set of queries. Do not reorder columns just because the query lists them in a different order. The order of conditions in a
WHEREclause has no effect. - Include columns used with equality before range columns as a starting heuristic. This works because a range condition stops later columns from narrowing the scan.
Exercises
Exercise
A table has an index on (region, status, createdat). For each query condition set, say which index columns can narrow the scan: (1) region = 'EU' AND createdat >= DATE '2026-01-01'; (2) status = 'OPEN' AND createdat >= DATE '2026-01-01'; (3) region = 'EU' AND status = 'OPEN' AND createdat >= DATE '2026-01-01'.
Show answer
(1) Only region narrows the scan. status is skipped, so createdat is tested on every entry in the region range. (2) None. The leading column region is not constrained, so there is no start point for a range scan. (3) All three columns narrow the scan. region and status use equality, and createdat is the final range condition.
Exercise
Using the orders table and the index on (customerid, orderdate), you need to run "all orders on 2026-03-10 for any customer". The query is slow. Give two ways to fix it and a downside of each.
Show answer
Create an index with orderdate first, for example (orderdate, customer_id). Oracle can then start at the date. The downside is an extra index that every insert, update of those columns and delete must maintain. Alternatively, rely on an index skip scan, which can work when the leading column has few distinct values. With 500 customers it probes the index once per customer, so it is rarely a good choice here. A dedicated index is the dependable fix.