One row from the index end per value: GROUP BY MAX → skipping with recursive WITH

Getting the latest order time per status with GROUP BY status reads all 2 million index entries. There are only three status values, so finding the next status value in the index once at a time with recursive WITH, and reading only one row from the index end for each value's MAX, drops Buffers from 8,304 to 19.

SQL

Situation

The operations monitoring screen shows the last order time for each order status every minute. The result is three rows, yet every run reads the whole order index.

Tables

TableColumnsDescription
ORDERSSTATUSThree values: DONE, PAID, CANCELED
ORDERSORD_DTOrder date and time
IndexColumns
ORDERS_X2STATUS, ORD_DT, ORD_ID

There is no separate table of status codes.

Query

BEFORE · GROUP BY status

SELECT status, MAX(ord_dt) AS last_dt
FROM orders
GROUP BY status

Approach

1. See how many blocks one value needs

MAX(ord_dt) for WHERE status = 'PAID' is the single last entry of the PAID range in ORDERS_X2. The execution plan shows INDEX RANGE SCAN (MIN/MAX) and reads about 3 blocks (Scenario 19). Grouped with GROUP BY, this method cannot be used and the whole index is scanned.

2. Consider where to get the list of status values

Getting MAX per value requires the list of status values first. Writing the values into the statement misses any new status. In the index, "the first value greater than the current value" can be found by descending to one place with MIN(status) … WHERE status > :current_value. Consider how to repeat this until no value remains.

3. Confirm the optimizer does not group it back

If the optimizer turns the per-value scalar subquery into a GROUP BY join (unnesting), it goes back to the same full read as at the start.

Explanation

Skip through status values with recursive WITH, then MAX per value

AFTER · Recursive WITH + MIN/MAX

WITH s (status) AS (
  SELECT MIN(status) FROM orders
  UNION ALL
  SELECT (SELECT MIN(o.status) FROM orders o WHERE o.status > s.status)
  FROM s
  WHERE s.status IS NOT NULL
)
SELECT s.status,
       (SELECT /*+ NO_UNNEST */ MAX(o.ord_dt) FROM orders o WHERE o.status = s.status) AS last_dt
FROM s
WHERE s.status IS NOT NULL

Why it got faster

  • Before reads all 2 million entries of ORDERS_X2 with INDEX FAST FULL SCAN and groups them with HASH GROUP BY. Buffers 8,304.
  • After's recursive part finds the first value (CANCELED) with INDEX FULL SCAN (MIN/MAX), and each next value (DONE, PAID) once with INDEX RANGE SCAN (MIN/MAX). The last probe returns NULL and the recursion ends. 11 blocks.
  • The MAX(ord_dt) scalar subquery attached per value runs three times, each reading one entry at the index end. 8 blocks. The scalar subquery's Buffers are not added to the SELECT STATEMENT line but appear separately, so the total is 19.
  • NO_UNNEST prevents the reversal described in step 3. The variation below checks this.

Why this is optimal

  • The cost is proportional to the number of distinct values, not the number of rows. Each value only descends the index twice (finding the next value, finding MAX), costing as many blocks as the 3-level index height.
  • If distinct values grow to tens of thousands, the probes grow as much and become more expensive than a full read. This method is for few distinct values and many rows.

Variation: without NO_UNNEST

TRY · Scalar subquery without the hint

WITH s (status) AS (
  SELECT MIN(status) FROM orders
  UNION ALL
  SELECT (SELECT MIN(o.status) FROM orders o WHERE o.status > s.status)
  FROM s
  WHERE s.status IS NOT NULL
)
SELECT s.status,
       (SELECT MAX(o.ord_dt) FROM orders o WHERE o.status = s.status) AS last_dt
FROM s
WHERE s.status IS NOT NULL
  • 23ai turns the MAX scalar subquery into a GROUP BY status inline view (VW_SSQ_1) and outer-joins it. That view again reads 2 million entries with INDEX FAST FULL SCAN, and Buffers are 8,315. When VW_SSQ appears in the execution plan, the scalar subquery has been unnested.

Key points

  • GROUP BY + MIN/MAX reads every row even when there are few groups. If an index starts with the group key, it can be turned into one row from the index end per value.
  • Without a value list, recursive WITH finds the "next value" in the index one at a time. Other DBs call this a Loose Index Scan or Skip Scan.
  • An optimizer transformation can undo a hand-rewritten shape. Check in the execution plan that the intended operation (MIN/MAX) remains.