Filling the leading index column with an IN list (key_set)

With no condition on the leading column (status), the index cannot be used and the whole table is read. Filling the leading column with its possible values as an IN list makes it a key_set that reads an index range per value.

SQL

Situation

Find orders over 499,000 in amount during one day (2025-09-01).

Tables

TableColumnTypeDescription
ordersstatusvarchar(16)Three values: DONE, PAID, CANCELED
ordersord_dttimestamp2,000 rows per day
ordersamtint64Amount
IndexColumns
orders_x2status, ord_dt

High-value orders for one day

BEFORE · Full read

SELECT *
FROM orders
WHERE ord_dt BETWEEN '2025-09-01 00:00:00' AND '2025-09-01 23:59:59'
  AND amt > 499000

Approach

1. See why the index could not be used

orders_x2 is sorted by status first. With only ord_dt and no status condition, there is no way to decide which segment of the index to read.

2. List every value of the leading column

status has only three values. Adding status IN ('DONE', 'PAID', 'CANCELED') leaves the result unchanged, and the index can read the (status, that day) segment per value. KDBS reads an IN list on the leading equality column of an index as a key set (key_set=3) and uses the closed range on the next column along with it.

3. Confirm the result is the same

If another value appears, the result changes. Use this only when the list is closed, like a code table or status values.

Explanation

IN list on the leading column

AFTER · IN list

SELECT *
FROM orders
WHERE status IN ('DONE', 'PAID', 'CANCELED')
  AND ord_dt BETWEEN '2025-09-01 00:00:00' AND '2025-09-01 23:59:59'
  AND amt > 499000

Why it got faster

  • In Before, Scan orders reads all 24,097 pages and keeps 7 rows.
  • In After, IndexRange orders key_set=3 reads that day's segment for each of the three status values, finds 2,000 rows, filters by amount, and keeps 7 rows. Buffers 2,003.

When the range widens

TRY · One month

SELECT COUNT(*)
FROM orders
WHERE status IN ('DONE', 'PAID', 'CANCELED')
  AND ord_dt BETWEEN '2025-09-01 00:00:00' AND '2025-09-26 23:59:59'
  • It finds 52,000 rows through the index, Buffers 52,003. Reading the same range as a full table read is 24,097 pages. Each row found through the index reads a table page, so once the rows found exceed the table's page count (24,097), that is, about 1.2% of the whole for this table, a full read is cheaper (scenario 14).

  • Leaving the end of the range open, as in ord_dt >= '2025-09-01 00:00:00', means it is not used as a range, and the whole segment of the three values (2 million rows) is read through the index. It blocks the sandbox for a long time, so it is not included as a runnable block.

Key points

  • With no condition on the leading column of a composite index, the index cannot be used. If the values are few and closed, fill them in with an IN list (common to all DBs).
  • KDBS ANALYZE shows this read as key_set=<number of values>. An IN list on the tuple key (WHERE ord_id IN (1, 7, 42)) is also a key set that descends once per value.
  • That a full read is better once many rows are read through an index is especially clear in KDBS, because the engine does not choose for you.