Filling the leading index column with an IN list
With a condition only on the second index column, the index cannot be searched by range. If the leading column has only a few values, fill them in with an IN list and turn a FULL SCAN into a few range scans.
SQL
Situation
A customer service screen shows all "orders from the last 7 days" regardless of status. There is an ORDERS_X2(STATUS, ORD_DT, ORD_ID) index, but the query has only an ORD_DT condition. The execution plan is a table FULL SCAN.
Tables
| Table | Column | Type | Description |
|---|---|---|---|
ORDERS | STATUS | VARCHAR2 | Order status. Only three values: DONE, PAID, CANCELED |
ORDERS | ORD_DT | DATE | Order date and time (last date 2025-09-26) |
ORDERS | AMT | NUMBER | Order amount |
| Index | Columns |
|---|---|
ORDERS_X2 | STATUS, ORD_DT, ORD_ID |
There is no index starting with ORD_DT. There are 14,000 orders in the last 7 days.
Query
BEFORE · No condition on the leading column
SELECT ord_id, ord_dt, status, amt
FROM orders
WHERE ord_dt >= DATE '2025-09-20'
Run it and see which operation reads the data and how many blocks it reads for 14,000 rows.
Approach
1. See why the index cannot be searched by range
ORDERS_X2 is sorted first by STATUS, then by ORD_DT within the same STATUS. The last 7 days of orders are scattered at the end of the DONE section, the end of the PAID section, and the end of the CANCELED section. Without a STATUS condition, there is no single starting point for the search.
2. Count the values of the leading column
With thousands of values there is no way, but with only three, it can "range-scan each of the three sections". The optimizer can do this on its own (INDEX SKIP SCAN), or the query can state it explicitly.
3. Confirm the result is the same after adding the condition
Tuning that adds a condition needs a guarantee that the result does not change. Check that the value list is complete and whether the column can contain NULL.
Explanation
Filling the leading column with an IN list
AFTER · IN list added
SELECT ord_id, ord_dt, status, amt
FROM orders
WHERE status IN ('DONE', 'PAID', 'CANCELED')
AND ord_dt >= DATE '2025-09-20'
Why it got faster
- Before uses
TABLE ACCESS FULLwith about 14,000 Buffers. - In After, the
ORDERS_X2INDEX RANGE SCANunderINLIST ITERATORhas Starts of 3. It range-scans three times, from('DONE', 2025-09-20),('PAID', 2025-09-20), and('CANCELED', 2025-09-20)to the end of each section. The index is about 80 blocks and the table rows sit in the recent blocks, so total Buffers are about 390. - Writing just one more condition changed the access method in the execution plan from a FULL SCAN to range scans.
Why the result is the same
STATUShas only three values, and the current data has no NULL. SoIN ('DONE', 'PAID', 'CANCELED')equals "every row".- However,
STATUShas noNOT NULLconstraint. If a NULL or a new status value (e.g.REFUNDED) arrives later, those rows silently drop out of the result. In practice, before using this method, confirm that the value list is guaranteed byNOT NULLandCHECKconstraints or a code table.
Why this is optimal
- In the index, it reads only the last 7 days. In the table, it reads only the blocks where recent data sits (clustering in scenario 14).
- The remaining cost is the table access to fetch
AMT. DroppingAMTwould let the index alone finish the job (about 80 Buffers), but the result needs the amount, so it stays.
Variation: letting the optimizer do it (INDEX SKIP SCAN)
TRY · INDEX_SS
SELECT /*+ INDEX_SS(orders orders_x2) */ ord_id, ord_dt, status, amt
FROM orders
WHERE ord_dt >= DATE '2025-09-20'
INDEX SKIP SCANsearches by skipping across each value of the leading column, doing work similar to the IN list (about 410 Buffers). The query does not need to change, and the result stays correct even when a new status value appears.- However, the optimizer chooses Skip Scan by looking at the number of distinct values (NDV) of the leading column, so depending on the statistics it may not be chosen (this query also uses a FULL SCAN without the hint).
Key points
- A composite index can be range-scanned only with an
=orINcondition on the leading column. - If the leading column has few values, fill them in with an
INlist or use Skip Scan. With many values it has no effect (number of scans = number of values). - Tuning that adds a condition must also confirm the basis for the same result (NOT NULL, a guaranteed value list). This is the same context as the NULL trap in scenario 8.
- PostgreSQL has B-tree Skip Scan from version 18; earlier versions get the same effect with an
IN(= ANY) list.