If there is an index, use the index: an access method that ignores selectivity

KDBS chooses the access method by the shape of the statement, not by data statistics. Even status = DONE, which matches 90%, is read through the index, with a table page read per row. Rewriting it in a shape that does not use the index and reading the table in order reads 6.6 times less.

SQL

Situation

Compute the count and amount total of completed orders in Q3 (from 2025-07-01).

Tables

TableColumnTypeDescription
ordersstatusvarchar(16)DONE 90% (about 1.8 million rows)
ordersord_dttimestamp176,000 rows from 2025-07-01, of which 158,463 are DONE
IndexColumns
orders_x2status, ord_dt

The whole table is 24,097 pages.

Q3 completed order totals

BEFORE · Index range

SELECT COUNT(*), SUM(amt)
FROM orders
WHERE status = 'DONE'
  AND ord_dt BETWEEN '2025-07-01 00:00:00' AND '2025-09-26 23:59:59'

Run it and compare the step name (IndexRange) and Buffers with the whole table's page count (24,097).

Approach

1. Know how the access method is chosen

KDBS resolves equality conditions that are not on the tuple key in a fixed order: secondary index → Cabin → filter scan. It does not look at how many rows there are (Cabin and secondary indexes). Even when status = 'DONE' is 90%, it uses the index.

2. See what happens when index order and table order differ

The table is in ord_id (= date) order, and orders_x2 is in status, ord_dt order. Each row found through the index reads a different table page.

3. Rewrite it in a shape that does not use the index

To make it read the table in order, write the condition in a shape the index cannot resolve (LIKE), or give the range on the tuple key (ord_id), which is the table's order.

Explanation

Index range → full read (LIKE)

AFTER · Full read (LIKE)

SELECT COUNT(*), SUM(amt)
FROM orders
WHERE status LIKE 'DONE'
  AND ord_dt BETWEEN '2025-07-01 00:00:00' AND '2025-09-26 23:59:59'

Why it got faster

  • In Before, IndexRange orders finds the 158,463 Q3 rows in the DONE segment and reads a table page per row. Buffers 158,464, about 1.2 seconds.
  • In After, LIKE is a condition that does not become a key, so Scan orders reads the table's 24,097 pages in order and filters. Same result, Buffers 24,097, about 0.3 seconds. It reads 6.6 times less.

Tuple key range

AFTER · Tuple key range

SELECT COUNT(*), SUM(amt)
FROM orders
WHERE ord_id BETWEEN 1640001 AND 2000000
  AND status LIKE 'DONE'
  • Knowing that date and ord_id share the same order (2,000 per day, the first order of 2025-07-01 is number 1,640,001), the range can be given on the tuple key. Range orders range=[1640001, 2000000], Buffers 4,338. ord_dt only goes up to September 26, so the result is the same as the previous two.

If the end of the range is open

Writing the same statement with ord_dt >= '2025-07-01 00:00:00' (a range with an open end) means it is not used as a range, and IndexProbe finds all 1.8 million DONE rows. The measurement is Buffers 1,799,437, about 14.7 seconds. It blocks other users sharing the sandbox in the meantime, so it is not included here as a runnable block (the same behavior can be seen on a small range in scenario 1).

Key points

  • Optimizers in other DBs choose between an index and a full read by looking at selectivity and the clustering factor. KDBS chooses by the shape of the statement and the catalog, and this choice does not change when the data changes. Instead, the person writing the SQL chooses through the shape.
  • Reading many rows through a secondary index reads as many pages as rows. When reading many rows, a shape that reads in table order (tuple key) is better.
  • In production, an administrator can fix the access method with CREATE PLAN … ACCESS orders SCAN without changing the statement (Join order and plans). The sandbox account cannot use it.