Filling the Index Leading Column with an IN List
With a condition only on the second column of an index, an Index Seek is impossible. If the leading column has only a few values, filling them in with an IN list turns the Index Scan into a few Seeks.
SQL
Situation
A customer service screen shows the ID, time, and status of all "orders in the last 7 days", regardless of status. There is an index orders_x2(status, ord_dt, ord_id), but the query has only an ord_dt condition.
Tables
| Table | Column | Description |
|---|---|---|
orders | status | Only three values: DONE, PAID, CANCELED |
orders | ord_dt | Order datetime (last date 2025-09-26) |
| Index | Columns |
|---|---|
orders_x2 | status, ord_dt, ord_id |
No index starts 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
FROM orders
WHERE ord_dt >= CAST('2025-09-20' AS datetime2(0))
Run it and check whether it did a Seek or a Scan on orders_x2.
Approach
1. See why the index cannot be seeked
orders_x2 is sorted by status first, and by ord_dt within the same status. Orders from the last 7 days are spread across the ends of the three status ranges, so without a status condition there is no single start position for a Seek. Since all the needed columns are in the index, the optimizer scans the whole index instead of the table.
2. Count the values of the leading column
If there are only three, it can "seek each of the three ranges".
3. Confirm that adding the condition keeps the result the same
Check that the value list is complete and whether that column can contain NULL.
Explanation
Filling the leading column with an IN list
AFTER · Add an IN list
SELECT ord_id, ord_dt, status
FROM orders
WHERE status IN ('DONE', 'PAID', 'CANCELED')
AND ord_dt >= CAST('2025-09-20' AS datetime2(0))
Why it got faster
- Before reads
orders_x2to the end with anIndex Scan. Buffers are about 6,000. - After has one
Index Seekwith three Seek Predicates (status = 'CANCELED' AND ord_dt >= …,'DONE','PAID') and reads only the last 7 days of the three ranges. Buffers are about 53.
Why the result is the same
statushas only three values, and the current data has no NULL. But there is no NOT NULL or CHECK constraint, so if NULL or a new status value arrives later, those rows silently drop out of the result. First confirm that the value list is guaranteed.
Why this is optimal
- It reads only the index pages of the three needed ranges, and since every column is in the index, it does not read the table.
Variation: when a column not in the index is needed
TRY · Include amt, one day
SELECT ord_id, ord_dt, status, amt
FROM orders
WHERE status IN ('DONE', 'PAID', 'CANCELED')
AND ord_dt >= CAST('2025-09-26' AS datetime2(0))
TRY · Include amt, one day, no leading condition
SELECT ord_id, ord_dt, status, amt
FROM orders
WHERE ord_dt >= CAST('2025-09-26' AS datetime2(0))
- Fetching
amtas well adds a Key Lookup per row. For one day (2,000 rows), the IN list version reads 18 Seek pages + about 5,700 Lookup pages, about 5,750 in total; without the leading condition it reads the whole table (about 13,200). At 7 days (14,000 rows) the Lookups cost more than the table, so the optimizer picks a full table read even with the IN list (Scenario 14).
Key points
- A composite index can be seeked only when there is an
=orINcondition on the leading column. SQL Server turns anINlist into multiple Seek Predicates. - Tuning that adds conditions also checks the basis for the result being the same (NOT NULL, a guaranteed value list).
- Differences by DB: Oracle does the same with
INLIST ITERATORorINDEX SKIP SCAN, and PostgreSQL with an= ANYlist (Skip Scan from 18). SQL Server has no Skip Scan, so the IN list is the only way.