Two words, NULLS LAST, block the sort skip
After changing the 20 most recent paid orders to ORDER BY ord_dt DESC NULLS LAST, the backward index scan disappears and the whole table is read and sorted. A descending index scan returns NULLs first. Splitting non-NULL rows and NULL rows with UNION ALL brings Buffers back from 13,950 to 9.
SQL
Situation
The payment screen shows the 20 most recent paid orders. After a report that orders with an empty order date appeared at the top, a developer added NULLS LAST. Since then the screen, which took 0.01 seconds, has become slow.
Tables
| Table | Columns | Description |
|---|---|---|
ORDERS | STATUS, ORD_DT | 140,179 paid orders. ORD_DT has no NOT NULL constraint (currently 0 NULLs) |
| Index | Columns |
|---|---|
ORDERS_X2 | STATUS, ORD_DT, ORD_ID |
Query
BEFORE · NULLS LAST
SELECT * FROM (
SELECT ord_id, ord_dt, amt
FROM orders
WHERE status = 'PAID'
ORDER BY ord_dt DESC NULLS LAST
) WHERE ROWNUM <= 20
Compare the result and execution plan with a run where NULLS LAST is removed.
Approach
1. See where the index puts NULLs
An Oracle B-tree index places NULLs at the end, like the largest value. Read in ascending order, NULLs come last (ASC NULLS LAST); read backward, NULLs come first (DESC NULLS FIRST). This is also the default for ORDER BY … DESC. DESC NULLS LAST is an order that neither reading direction of the index produces.
2. See whether a condition removes the NULL position problem
Adding WHERE ord_dt IS NOT NULL leaves no NULLs in the result, so NULLS LAST and NULLS FIRST give the same order. Run it to see whether the optimizer notices this and skips the sort.
3. Split the sort order into two index orders
DESC NULLS LAST means "non-NULL rows in descending order, then NULL rows". Each part can be read in index order. If the first part fills 20 rows, the second part need not be read.
Explanation
Top-N split into non-NULL rows and NULL rows
AFTER · Split with UNION ALL
SELECT * FROM (
SELECT * FROM (
SELECT * FROM (
SELECT ord_id, ord_dt, amt FROM orders
WHERE status = 'PAID' AND ord_dt IS NOT NULL
ORDER BY ord_dt DESC
) WHERE ROWNUM <= 20
UNION ALL
SELECT * FROM (
SELECT ord_id, ord_dt, amt FROM orders
WHERE status = 'PAID' AND ord_dt IS NULL
) WHERE ROWNUM <= 20
)
ORDER BY ord_dt DESC NULLS LAST
) WHERE ROWNUM <= 20
Why it got faster
- Before cannot use the index order, so it reads the whole table (
TABLE ACCESS FULL, 13,950 blocks) and sorts the 140,179 paid orders withSORT ORDER BY STOPKEY. Buffers 13,950. - After's first branch reads
ORDERS_X2backward and stops at 20 rows (6 blocks). The second branch finds theSTATUS = 'PAID' AND ORD_DT IS NULLrange and confirms 0 rows (3 blocks). Only the combined maximum of 40 rows is sorted again. Buffers 9. - The result is the same as Before. If NULLs appear, as long as there are 20 or more non-NULL rows, the second branch's rows are pushed to the back by the outer sort and cut off.
- The
(STATUS, ORD_DT, …)index also holds rows whoseORD_DTis NULL, because the leading columnSTATUSis not NULL. So the second branch also finds its rows through the index.
Why this is optimal
- Compared with the original plan without
NULLS LAST(6 blocks), only the second branch's 3 blocks are added. That is the minimum cost of confirming that there really are no NULL rows. - If the column has a
NOT NULLconstraint,NULLS LASTdoes not affect the result, so removing it is simplest. Without the constraint, the current data having no NULLs is no basis.
Variation: add only an IS NOT NULL condition
TRY · IS NOT NULL + NULLS LAST
SELECT * FROM (
SELECT ord_id, ord_dt, amt
FROM orders
WHERE status = 'PAID' AND ord_dt IS NOT NULL
ORDER BY ord_dt DESC NULLS LAST
) WHERE ROWNUM <= 20
- The result cannot contain NULLs, but the optimizer does not choose to skip the sort. Buffers 13,950, the same as Before. The optimizer does not consider that a condition makes the
NULLSclause meaningless, so write the sort clause by hand to match the index order.
TRY · Remove NULLS LAST (NULLs on top)
SELECT * FROM (
SELECT ord_id, ord_dt, amt
FROM orders
WHERE status = 'PAID'
ORDER BY ord_dt DESC
) WHERE ROWNUM <= 20
- It stops at 20 rows with
INDEX RANGE SCAN DESCENDING. Buffers 6. This is the original screen's order.
Key points
- To skip a sort with an index, not only the
ORDER BYdirection but also the NULL position must match the index read order. An Oracle index isASC NULLS LAST, andDESC NULLS FIRSTwhen read backward. - A sort that differs from the index order can regain partial range processing when split into parts readable in index order and combined with
UNION ALL. This continues Scenario 2. - When changing a sort clause, check not only the result but also whether a new
SORToperation appeared in the execution plan.