Filling the index leading 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 to turn a full read into an index search.
SQL
Situation
A customer service screen shows all "orders in 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 full table read.
Tables
| Table | Column | Description |
|---|---|---|
orders | status | Only three values: DONE, PAID, CANCELED |
orders | ord_dt | Order date and time (last date 2025-09-26) |
orders | amt | Order amount |
| Index | Columns |
|---|---|
orders_x2 | status, ord_dt, ord_id |
There is no index starting with ord_dt. Orders in the last 7 days are 14,000 rows.
Query
BEFORE · No leading column condition
SELECT ord_id, ord_dt, status, amt
FROM orders
WHERE ord_dt >= DATE '2025-09-20'
Run it and check 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, and by ord_dt within the same status. Orders in the last 7 days are scattered at the ends of the DONE, PAID, and CANCELED sections, so no single search start position can be set.
2. Count the values of the leading column
If there are only three, it can "search each of the three sections by range".
3. Confirm the result stays the same when the condition is added
Check that the value list is complete and whether the column can receive NULLs.
Explanation
Fill 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 is a
Seq Scanwith Buffers about 18,750. - In After,
Bitmap Index Scanfinds only the recent sections of the three status values inorders_x2(Index Cond:status = ANY ('{DONE,PAID,CANCELED}'), about 80 blocks), andBitmap Heap Scanreads only the recent blocks where those rows sit. Buffers about 210. - PostgreSQL turns the
IN (…)list into an array comparison (= ANY) and finds a range per value within one index search.
Evidence that the result is the same
statushas only three values, and the current data has no NULLs.- However,
statushas noNOT NULLconstraint. If a NULL or a new status value comes in later, it silently drops out of the result. Before using this method, confirm that the value list is guaranteed byNOT NULLandCHECKconstraints or a code table.
Why this is optimal
- It reads only the last-7-days sections of the index and only the table blocks where recent data sits.
- The remaining cost is the table access that fetches
amt.
Key points
- A composite index allows a range search only with an
=orINcondition on the leading column. - Tuning that adds a condition must also confirm the evidence that the result is the same (NOT NULL, a guaranteed value list). This is the same context as the NULL trap in scenario 8.
- DB difference: Oracle searches per value with
INLIST ITERATOR, and also hasINDEX SKIP SCAN, which skips the leading column (Oracle scenario 17). PostgreSQL gained B-tree Skip Scan in 18; in this sandbox (17), theINlist takes its place.