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

TableColumnsDescription
ORDERSSTATUS, ORD_DT140,179 paid orders. ORD_DT has no NOT NULL constraint (currently 0 NULLs)
IndexColumns
ORDERS_X2STATUS, 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 with SORT ORDER BY STOPKEY. Buffers 13,950.
  • After's first branch reads ORDERS_X2 backward and stops at 20 rows (6 blocks). The second branch finds the STATUS = 'PAID' AND ORD_DT IS NULL range 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 whose ORD_DT is NULL, because the leading column STATUS is 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 NULL constraint, NULLS LAST does 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 NULLS clause 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 BY direction but also the NULL position must match the index read order. An Oracle index is ASC NULLS LAST, and DESC NULLS FIRST when 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 SORT operation appeared in the execution plan.