Sub topic 5
Choosing column order in a composite index
This chapter assumes you can create a B-tree index, know that the optimizer picks an access path, and have a rough idea of how a B-tree stores sorted keys with ROWIDs. It covers how column order in a composite index decides which queries can use it, and how to pick that order.
What a composite index stores
A composite index (also called a concatenated index) covers more than one column. Oracle sorts its entries by the first column, then by the second column within equal first values, and so on. The index from the earlier chapters is an example:
CREATE TABLE orders (
order_id NUMBER PRIMARY KEY,
customer_id NUMBER NOT NULL,
status VARCHAR2(10) NOT NULL,
order_date DATE NOT NULL,
email VARCHAR2(100)
);
CREATE INDEX orders_cust_date_ix ON orders (customer_id, order_date);Think of it like a phone book sorted by last name, then first name. Finding every "Garcia" is fast. Finding every "Maria" regardless of last name is not, because the Marias are scattered across the whole book.
The leaf entries of orders_cust_date_ix look like this, in this order:
| customer_id | order_date | ROWID |
|---|---|---|
| 41 | 2026-03-02 | AAA...1 |
| 42 | 2026-01-05 | AAA...2 |
| 42 | 2026-02-11 | AAA...3 |
| 42 | 2026-06-30 | AAA...4 |
| 43 | 2026-01-20 | AAA...5 |
All rows for customer 42 sit next to each other, and within that block they are sorted by date. That physical adjacency is what makes a range scan possible.
The leading column rule
An index can drive an efficient range scan only when the query gives a usable condition on the leading (first) column. The conditions on the later columns then narrow the scan further, but only if the earlier columns are constrained as well.
-- Leading column with equality, second column with a range: best case
SELECT order_id
FROM orders
WHERE customer_id = 42
AND order_date >= DATE '2026-01-01';
-- Leading column only: still a range scan
SELECT order_id
FROM orders
WHERE customer_id = 42;
-- Second column only: no leading column to seek on
SELECT order_id
FROM orders
WHERE order_date >= DATE '2026-01-01';The first query reads one contiguous slice of the index: from (42, 2026-01-01) to the end of customer 42's entries. The second reads all of customer 42's entries. The third has nothing to position on, so the index entries for every customer would have to be examined.
Skip scan
Oracle has an access path called an index skip scan. When the leading column has very few distinct values, the optimizer can treat the index as several small sub-indexes, one per leading value, and probe each one for the second column. For the third query above, this is only attractive if customer_id has a handful of values. With many customers, the number of probes grows and the optimizer will usually choose a full table scan or a fast full scan of the index instead. Do not design an index counting on skip scans. Treat them as a fallback that sometimes rescues a poorly ordered index.
How the predicate type affects the scan
Equality conditions are what make later columns useful. Once the scan meets a range condition on a column, the columns after it can no longer narrow the contiguous slice of entries.
Consider an index on (order_date, customer_id) and this query:
SELECT order_id
FROM orders
WHERE order_date >= DATE '2026-01-01'
AND customer_id = 42;The index entries are sorted by date first. All entries from January 1 onward form the contiguous slice, and within that slice customer 42's entries are scattered among every other customer's entries for each date. Oracle reads the whole slice and filters on customer_id as it goes. Compare this with (customer_id, order_date), where the same query reads only customer 42's entries from January 1 onward.
The practical conclusion is that columns compared with = belong in front of columns compared with ranges (>=, BETWEEN, <).
Rules of thumb for choosing the order
Use these in sequence. They are guidelines, not laws, and the first one matters most.
- Start from the queries. List the
WHEREclauses the index should serve. A column that appears in an equality condition in nearly every one of them is a strong candidate for first position. - Equality columns before range columns. As shown above, this keeps the scanned slice as small as possible.
- Cover the most queries with one index. If some queries filter on
customer_idalone and others oncustomer_idplusorder_date, one index on(customer_id, order_date)serves both. An index on(order_date, customer_id)would serve only the second kind well. Choosing the order so the index doubles as a single-column index can save you from creating, and maintaining, a second index. - Selectivity is a tie-breaker, not the main rule. You may have read advice to put the most selective column first. When both columns are used with equality, either order gives the same number of entries in the scan, so selectivity does not change the cost of a lookup. Usage patterns decide it.
- Consider
ORDER BY. Index entries are already sorted, so a query like the following can avoid a separate sort step if the index order matches:
SELECT order_id, order_date
FROM orders
WHERE customer_id = 42
ORDER BY order_date;With (customer_id, order_date), entries for customer 42 are already in date order. With (order_date, customer_id), they are not.
Checking the choice with a plan
Do not rely on reasoning alone. Look at the plan:
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);In the output, look at the Predicate Information section. A condition listed under access was used to position the scan inside the index. A condition listed under filter was checked afterwards against entries that had already been read. For a well-ordered index you expect both conditions under access:
INDEX RANGE SCAN | ORDERS_CUST_DATE_IX
access("CUSTOMER_ID"=42 AND "ORDER_DATE">=TO_DATE(' 2026-01-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))If a column you expected to narrow the scan shows up in a filter line, the column order or a predicate form is working against you. The small table in a test environment may still choose a full table scan whatever the order, so test with realistic row counts and statistics.
Common mistakes
- Putting a range column first. An index on
(order_date, customer_id)seems natural because dates feel important, but it forces the scan to read many unrelated entries for equality lookups on customer. - Creating one index per column "just in case". Each extra index slows every insert, delete, and update of its columns. A single well-ordered composite index often replaces several.
- Creating a second index that duplicates a prefix. Given
(customer_id, order_date), a separate index on(customer_id)alone is usually redundant, since the composite index serves the same queries. It is not always redundant, because a narrower index is smaller, but it needs a reason. - Wrapping the leading column in a function.
WHERE TO_CHAR(customer_id) = '42'orWHERE UPPER(email) = ...cannot use a plain index on that column. A function-based index, covered in a later chapter, handles that case. - Leaving out a column the query needs. If every selected and filtered column is in the index, Oracle can answer from the index alone and skip table access. Adding a trailing column to enable that is sometimes worth it, but each added column makes the index bigger.# Choosing Column Order in a Composite Index
A composite index covers two or more columns. The order of those columns decides which queries can use the index efficiently. This chapter explains why, shows how to test it, and gives rules for picking an order.
This chapter assumes you can create a B-tree index, know that entries are stored in sorted order, and have seen an index range scan in an execution plan. Earlier chapters cover those topics.
How the index is sorted
A composite B-tree index sorts its entries by the first column. Entries with equal first-column values are sorted by the second column, and so on. This is the same as sorting a list of names by last name, then first name.
The index from the sample table is:
CREATE TABLE orders (
order_id NUMBER PRIMARY KEY,
customer_id NUMBER NOT NULL,
status VARCHAR2(10) NOT NULL,
order_date DATE NOT NULL,
email VARCHAR2(100)
);
CREATE INDEX orders_cust_date_ix ON orders (customer_id, order_date);The leaf entries are ordered like this:
| customer_id | order_date | ROWID |
|---|---|---|
| 41 | 2026-03-02 | AAA... |
| 42 | 2025-11-20 | AAB... |
| 42 | 2026-01-15 | AAC... |
| 42 | 2026-04-09 | AAD... |
| 43 | 2025-12-01 | AAE... |
All entries for customer 42 sit next to each other, and within that group they are sorted by date. Oracle can descend the tree to the first entry for customer 42 with a date on or after 2026-01-01, then read entries in order until the customer changes. That is a narrow range scan.
Dates alone are not grouped. The entries for 2026-01-15 are scattered across all customers. To find every order on one date, Oracle has no single place in the index to start.
Which queries can use the index
The leading column is the column that comes first in the index definition. How well a query uses a composite index depends on how the query filters that column.
| Query predicate | Access with `(customer_id, order_date)` |
|---|---|
| `customer_id = 42 AND order_date >= DATE '2026-01-01'` | Range scan that narrows on both columns |
| `customer_id = 42` | Range scan on the leading column |
| `order_date >= DATE '2026-01-01'` only | Cannot narrow by position. See the skip scan section below |
| `customer_id > 40 AND order_date = DATE '2026-01-15'` | Range scan on customer_id. The date is checked on every entry in that range |
The last row shows an important point. Once a column is filtered with a range condition (>, <, BETWEEN, LIKE 'abc%'), the columns after it no longer help narrow the starting and stopping points. They can still filter entries inside the scanned range, but Oracle reads every entry in that range first.
So the useful pattern is equality conditions on the leading columns, followed by at most one range condition on the next column.
Confirming the effect in a plan
You can see the difference in the plan. The first query filters on the leading column.
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 at the Predicate Information section of the output. For a query that uses both columns to position the scan, both conditions appear under access:
access("CUSTOMER_ID"=42 AND "ORDER_DATE">=TO_DATE(' 2026-01-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))When a column is only checked after the range is read, it appears under filter instead. A condition under filter on an index step means Oracle read entries and then discarded some. Moving that column earlier in the index, or changing the query, can reduce the work.
The exact plan text depends on your version, statistics, and data volume. With a tiny table the optimizer may choose a full table scan regardless of the index, so test with realistic row counts.
Skip scans
If a query filters only on a non-leading column, Oracle may still use the index through an index skip scan. It treats each distinct value of the leading column as a separate small index and probes each one.
A skip scan pays off only when the leading column has few distinct values. With customer_id as the leading column and many customers, a skip scan would probe the tree once per customer, and the optimizer will usually prefer a full scan. Do not design an index counting on a skip scan. Treat it as a fallback.
Rules for choosing an order
No single rule works for every case. Use these in order and check the result with a plan.
- Put columns that appear with equality conditions first. Equality on the leading columns gives the narrowest range.
- Put a range column after the equality columns. A column used with
>=,BETWEEN, orLIKE 'x%'should come after the equality columns, because columns after it cannot narrow the scan. - Look at which column prefixes your queries use. An index on
(customer_id, order_date)also serves queries oncustomer_idalone. It does not serve queries onorder_datealone. If both single-column lookups are common, you may need a second index. - Consider
ORDER BY. Entries come out of the index sorted. A query withWHERE customer_id = 42 ORDER BY order_datecan read the index in order and skip a separate sort step. Reversing the columns loses that benefit. - Selectivity is a weaker guide than it seems. A common claim is to put the most selective column first. That matters less than rules 1 to 4. If every query supplies both columns with equality, either order gives a similar range scan, and the choice is then about which prefix serves other queries. Index key compression (
COMPRESS) can favor putting the column with fewer distinct values first, which is a separate consideration.
A worked comparison
Suppose the main query is the one above: one customer, orders since a date. Compare two candidate indexes:
-- Candidate A
CREATE INDEX orders_cust_date_ix ON orders (customer_id, order_date);
-- Candidate B
CREATE INDEX orders_date_cust_ix ON orders (order_date, customer_id);With candidate A, Oracle goes to customer 42, finds the first entry on or after 2026-01-01, and reads only customer 42's later entries. With candidate B, Oracle goes to 2026-01-01 and must read every entry from that date onward, for all customers, checking customer_id = 42 on each one. If the table has a year of orders for 100,000 customers, B reads far more entries.
Now suppose a second query reports all orders on one day for any customer. Candidate B serves that query well and A does not. If both queries are frequent, you could keep A and add a separate index on order_date, accepting the extra cost on every insert, update of those columns, and delete.
Common mistakes
- Wrapping the leading column in a function.
WHERE TRUNC(order_date) = ...orWHERE UPPER(email) = ...prevents use of a plain index on that column. Function-based indexes, covered in a later chapter, address this. - Implicit conversion. Comparing a
VARCHAR2column to a number makes Oracle convert the column, which can block index use. Compare values of the same type. - Creating overlapping indexes. An index on
(customer_id)is usually redundant if(customer_id, order_date)exists, since the composite already serves queries oncustomer_id. Extra indexes add DML cost and storage for little gain. - Expecting a fixed outcome. The optimizer decides by cost. A good order makes the index eligible and cheap. It does not force the optimizer to use it.
Summary
Entries in a composite B-tree index are sorted by the first column, then the second. Equality conditions on leading columns plus one range condition give the narrowest scan. Choose the order from the queries you actually run, check the access and filter lines in the plan, and avoid duplicate prefixes.
Exercise
A table has an index on (status, orderdate). A query uses WHERE orderdate BETWEEN DATE '2026-01-01' AND DATE '2026-01-31' AND status = 'OPEN'. Another query uses WHERE order_date = DATE '2026-01-15' alone. Which query suits this index better, and why? What would you change if the second query is also frequent?
Show answer
The first query suits it. status has an equality condition on the leading column and orderdate has one range condition on the next column, so the scan is narrow. The second query filters only on the non-leading column, so Oracle cannot position the scan and would need a skip scan or a full scan. If the second query is frequent, add a separate index on orderdate (or on order_date first), accepting the extra maintenance cost on DML.
Exercise
In an execution plan for an index range scan, a column of the composite index appears under filter instead of access. What does this tell you about the work Oracle did?
Show answer
Oracle read index entries within the scanned range and then discarded those that did not match that column's condition. The column did not help set the start and stop points of the scan. Moving the column earlier in the index, or changing the preceding range condition to an equality, can reduce the entries read.