Computing MIN and MAX together prevents reading only the index ends
Computing only MIN or only MAX reads a few blocks at one end of the index. Computing both in one SELECT reads the whole index. Split them with UNION ALL so each reads only its end.
SQL
Situation
Every minute, a monitoring screen queries the first and last order IDs of the order table, and the oldest and latest times of paid orders. These are simple queries that each compute two values, yet every run reads thousands of blocks.
Tables
| Table | Column | Type | Description |
|---|---|---|---|
ORDERS | ORD_ID | NUMBER | Order ID (PK) |
ORDERS | STATUS | VARCHAR2 | Order status |
ORDERS | ORD_DT | DATE | Order date and time |
| Index | Columns |
|---|---|
ORDERS_PK | ORD_ID |
ORDERS_X2 | STATUS, ORD_DT, ORD_ID |
① First and last order IDs
BEFORE · MIN and MAX together
SELECT MIN(ord_id) AS first_id, MAX(ord_id) AS last_id
FROM orders
② First and last times of paid orders
BEFORE · Condition + MIN and MAX together
SELECT MIN(ord_dt) AS first_dt, MAX(ord_dt) AS last_dt
FROM orders
WHERE status = 'PAID'
Run them and see how the index is read and how many rows are read (A-Rows).
Approach
1. Recall what happens when only MAX is computed
The index is sorted, so the maximum is at the right end of the index. Computing only MAX(ord_id) means descending from the root to the rightmost leaf and reading one row (INDEX FULL SCAN (MIN/MAX)). That is 3 blocks.
2. See why computing both together does not work
This optimization reads "the first row at one end of the index", so a single descent cannot see both ends at once. The optimizer therefore chooses a plan that reads the whole index and computes both values together.
3. See whether splitting the query lets each part read only its end
Instead of computing two values in one SELECT, split each value into a separate query block so each reads only one end. Depending on how it is split, the optimizer may merge it back into one, so confirm with the execution plan.
Explanation
① Splitting with UNION ALL
AFTER · UNION ALL
SELECT 'first' AS k, MIN(ord_id) AS ord_id FROM orders
UNION ALL
SELECT 'last', MAX(ord_id) FROM orders
Why it got faster
- Before #1 reads all 2 million rows of
ORDERS_PKwithINDEX FAST FULL SCAN, using about 4,300 Buffers. - In After #1, the two queries under
UNION-ALLeach read one row withINDEX FULL SCAN (MIN/MAX). That is 3 blocks per query, 6 Buffers in total. - The result changed from one row with two columns to two rows. If the screen needs one row, combine them in the application or pivot them back outside with
MAX(CASE ...).
② With a condition: FIRST ROW
AFTER · UNION ALL
SELECT 'first' AS k, MIN(ord_dt) AS ord_dt FROM orders WHERE status = 'PAID'
UNION ALL
SELECT 'last', MAX(ord_dt) FROM orders WHERE status = 'PAID'
Why it got faster
- Before #2 reads all of
ORDERS_X2withINDEX FAST FULL SCAN, using about 8,300 Buffers. - After #2 uses
INDEX RANGE SCAN (MIN/MAX)+FIRST ROWto read only one row each at the very start and very end of theSTATUS = 'PAID'section. Buffers are 6. - When the leading column has an
=condition and the next column is the MIN/MAX target, the end of that section can be reached directly.
Why this is optimal
- Descending once from the index root to a leaf per query (3 blocks) is the minimum cost of finding one value through an index.
Variation: splitting with scalar subqueries
TRY · Two scalar subqueries
SELECT (SELECT MIN(ord_id) FROM orders) AS first_id,
(SELECT MAX(ord_id) FROM orders) AS last_id
FROM dual
- Splitting with scalar subqueries to get a single row is a common approach. But in 23ai, the optimizer merges two scalar subqueries on the same table into one (
VW_SVS_…view), so it reads the whole index again (about 4,300 Buffers). TheNO_UNNESThint does not prevent it either. - In earlier versions (19c, etc.), this approach got the MIN/MAX optimization for each. This is an example of how query transformation results can change when the optimizer version changes.
Key points
- Computing only
MINor onlyMAXreads just one end of the index (INDEX FULL SCAN (MIN/MAX),FIRST ROW). - Computing
MINandMAXtogether in one SELECT, or mixing them with other aggregates (COUNT, etc.), removes this optimization. - A split query can be merged back by optimizer transformations, so always check the execution plan after the change.
- Differences by DB: PostgreSQL splits
SELECT min(x), max(x)into two index searches (InitPlan) on its own. This problem is a trap met mainly in Oracle, so it is classified as Oracle-only.