INDEX vs FULL hint: clustering over selectivity

The rule "use an index below some percent" is only half right. At the same 3%, the index can win or lose. What decides it is how closely the rows found through the index sit together in the table (clustering).

SQL

Situation

A query returns the count and amount of canceled orders this month. Following an earlier review that said "canceled orders are as much as 3%, so a FULL SCAN beats an index", a FULL hint was added. Now the query is called thousands of times a day and has risen to the top of the DB load.

Tables

TableColumnTypeDescription
ORDERSSTATUSVARCHAR2DONE 90%, PAID 7%, CANCELED 3% (evenly mixed regardless of date)
ORDERSORD_DTDATEOrder date and time. Data is stored in date order
ORDERSAMTNUMBEROrder amount
IndexColumns
ORDERS_X2STATUS, ORD_DT, ORD_ID

ORDERS has 2 million rows in about 14,000 blocks.

Query

BEFORE · FULL hint

SELECT /*+ FULL(o) */ COUNT(*) AS cnt, SUM(o.amt) AS amt
FROM orders o
WHERE o.status = 'CANCELED'
  AND o.ord_dt >= DATE '2025-09-01'

BEFORE · INDEX hint

SELECT /*+ INDEX(o orders_x2) */ COUNT(*) AS cnt, SUM(o.amt) AS amt
FROM orders o
WHERE o.status = 'CANCELED'

The second query forces "all canceled orders" to be read through the index. Look at Buffers and the actual rows read (A-Rows) of both queries together.

Approach

1. Compute selectivity from "all the conditions"

STATUS = 'CANCELED' alone is 3%, but with the date condition added, this month's (26 days) canceled orders are about 1,500 rows, 0.08%. Check whether the conditions the hint author looked at match the conditions of the current query.

2. Measure the cost of index access as "how many table blocks are read"

Each row found through the index reads a table block. If the found rows sit in a few blocks, it is cheap; if they are scattered across the whole table, reading each block separately makes it as expensive as a FULL SCAN. This degree is called the clustering factor.

3. Consider the order in which the data was stored

Orders are stored in date order. So rows narrowed by date, such as "this month", sit together in a few hundred blocks at the end of the table. By contrast, "all canceled orders" are spread evenly across 3 years, so even at 3% nearly every block holds at least one.

4. Measure again whether the hint still holds

A hint is a judgment based on the data and conditions at the time it was added. If the conditions changed or data accumulated, measure again.

Explanation

① FULL → INDEX (canceled orders this month)

AFTER · INDEX hint

SELECT /*+ INDEX(o orders_x2) */ COUNT(*) AS cnt, SUM(o.amt) AS amt
FROM orders o
WHERE o.status = 'CANCELED'
  AND o.ord_dt >= DATE '2025-09-01'

Why it got faster

  • Before #1 uses about 14,000 Buffers. It read the whole table for 1,500 rows.
  • After #1 uses about 370 Buffers. It reads only the range from ('CANCELED', 2025-09-01) onward in ORDERS_X2 (about 10 blocks), and since those rows sit together at the end of the table, it reads only a few hundred table blocks.
  • With the hint removed, the optimizer also chooses this plan (INDEX RANGE SCAN). The wrong FULL hint was blocking a good plan.

② INDEX → FULL (all canceled orders)

AFTER · FULL hint

SELECT /*+ FULL(o) */ COUNT(*) AS cnt, SUM(o.amt) AS amt
FROM orders o
WHERE o.status = 'CANCELED'

Why this one is FULL

  • The 60,000 canceled orders (3%) are spread evenly across 3 years of blocks. The index access in Before #2 uses about 14,000 Buffers, almost the same as the FULL SCAN (about 14,000).
  • When the block count is the same, a FULL SCAN wins. A FULL SCAN reads several blocks at once (multiblock read), while index access reads one block at a time. When the blocks are not in the buffer cache (disk reads), the gap grows.

Why this is optimal

  • ① reads only the blocks where the needed rows sit. Going further would require AMT in the index too, which is a structural change.
  • In ②, the needed rows are scattered across every block, so any access method must read most of the blocks. The most efficient way to read them is a FULL SCAN.

Variation: the index wins even at 24%

TRY · INDEX: 2025 completed orders (24%)

SELECT /*+ INDEX(o orders_x2) */ COUNT(*) AS cnt, SUM(o.amt) AS amt
FROM orders o
WHERE o.status = 'DONE'
  AND o.ord_dt >= DATE '2025-01-01'

TRY · FULL: 2025 completed orders (24%)

SELECT /*+ FULL(o) */ COUNT(*) AS cnt, SUM(o.amt) AS amt
FROM orders o
WHERE o.status = 'DONE'
  AND o.ord_dt >= DATE '2025-01-01'
  • It reads 484,000 rows (24% of the total), yet the index side uses about 5,800 Buffers, fewer than the FULL SCAN (about 14,000). The 2025 rows sit together in the later blocks of the table, so TABLE ACCESS BY INDEX ROWID BATCHED reads the same block several times in a row.
  • However, it reads blocks one at a time, so when reading from disk, the FULL SCAN can be faster. Look at Buffers and elapsed time together.

Key points

  • "Use an index below some percent" is only a starting point. The real cost is the number of table blocks that hold the rows found through the index.
  • If rows sit together (a good clustering factor), the index is cheap even for a large fraction; if they are scattered, even a small fraction costs about the same as a FULL SCAN.
  • INDEX and FULL hints are judgments about the data and conditions at the moment they are added. When the conditions change, measure again.
  • The clustering factor is in USER_INDEXES.CLUSTERING_FACTOR. Close to the table block count is good; close to the row count is bad.