Rows discarded inside an NL join: data skew and a filter index

A join fetches the 2025 canceled orders of 100 high-volume customers. The optimizer assumes one order per customer and picks an NL join, but the inner side actually reads 54,257 orders all the way to the table and discards 97%. Filtering orders first with an index that holds the filter conditions, then hash joining, cuts Buffers from 54,570 to 3,833.

SQL

Situation

An account management screen shows the 2025 canceled orders of high-volume customers (IDs 1-100) all at once. The same query over a range of 100 regular customers returns instantly, but over the high-volume range it is noticeably slow.

Tables

TableColumnDescription
CUSTOMERSCUST_ID, CUST_NMCustomers 1-100 are high-volume customers with 2,000 or more orders each
ORDERSCUST_ID, STATUS, ORD_DT, AMTStatus CANCELED is 3%
IndexColumns
CUSTOMERS_PKCUST_ID
ORDERS_X1CUST_ID, ORD_DT
ORDERS_X2STATUS, ORD_DT, ORD_ID

The statistics have no histogram. The optimizer treats each customer's order count as the overall average (about 20).

Query

BEFORE · Customers → orders NL join

SELECT c.cust_id, c.cust_nm, o.ord_id, o.ord_dt, o.amt
FROM customers c
JOIN orders o ON o.cust_id = c.cust_id
WHERE c.cust_id <= 100
  AND o.status = 'CANCELED'
  AND o.ord_dt >= DATE '2025-01-01'

Compare the A-Rows of ORDERS_X1 with the A-Rows of TABLE ACCESS BY INDEX ROWID ORDERS above it.

Approach

1. Compare the rows read with the rows kept on the inner side

The cost of an NL join is "outer rows × cost of one inner probe". The cost of one inner probe is the cost of reading the table for as many rows as the index passed. If the index filters only the join key and date while the status is filtered at the table, each discarded row reads one table block.

2. See where E-Rows and A-Rows diverge

The optimizer calculated about 1 canceled 2025 order per customer. Consider why a customer range of 1-100 breaks that assumption. Also see why the STATISTICS COLLECTOR (adaptive plan) near the top of the execution plan did not switch to a hash join in this case. What the collector counts is the number of outer rows.

3. Find another path that filters first

Of the three conditions, an index holds both STATUS and ORD_DT. If that index filters the orders side first, work out how the customer condition is applied and which join method fits.

Explanation

Filter orders first with the status index, then hash join

AFTER · Orders first + hash join

SELECT /*+ LEADING(o) USE_HASH(c) INDEX(o orders_x2) */
       c.cust_id, c.cust_nm, o.ord_id, o.ord_dt, o.amt
FROM customers c
JOIN orders o ON o.cust_id = c.cust_id
WHERE c.cust_id <= 100
  AND o.status = 'CANCELED'
  AND o.ord_dt >= DATE '2025-01-01'

Why it got faster

  • Before looks up 2025 orders through ORDERS_X1 for each of the 100 customers. The index passes 54,257 rows (about 540 per customer), and after the status check at the table, 1,648 rows remain. Each of the 52,609 discarded rows costs one table block, so the single TABLE ACCESS BY INDEX ROWID line reads 54,196 blocks. Buffers are 54,570.
  • The cause is the optimizer's E-Rows of 1 per customer. That holds for an average customer, but customers 1-100 have 100 times the average. The adaptive plan looks only at the outer row count (100, as estimated) and keeps the NL join.
  • After reads STATUS = 'CANCELED' AND ORD_DT >= 2025-01-01 as a range in ORDERS_X2 (16,326 rows, 80 blocks). It checks CUST_ID <= 100 at the table, keeping 1,648 rows (3,828 blocks), then builds a hash table from the 100 customers and joins them. Buffers are 3,833.
  • The customer condition c.cust_id <= 100 is also applied as o.cust_id <= 100 through the join condition (transitive predicate). This can be seen in the Predicate Information of the execution plan.

Why this is optimal

  • The remaining cost is the table blocks of the 16,326 canceled orders. CUST_ID is not in ORDERS_X2, so the customer condition can be filtered only at the table. Unless the index changes, the blocks holding 2025 canceled orders must be read.
  • 2025 orders sit together in date order, so the 16,326 rows are within about 3,750 blocks. That is why it reads fewer blocks than rows.

Variation: with 100 regular customers

TRY · Regular customers 1001-1100 (NL)

SELECT c.cust_id, c.cust_nm, o.ord_id, o.ord_dt, o.amt
FROM customers c
JOIN orders o ON o.cust_id = c.cust_id
WHERE c.cust_id BETWEEN 1001 AND 1100
  AND o.status = 'CANCELED'
  AND o.ord_dt >= DATE '2025-01-01'

TRY · Regular customers 1001-1100 (hash join)

SELECT /*+ LEADING(o) USE_HASH(c) INDEX(o orders_x2) */
       c.cust_id, c.cust_nm, o.ord_id, o.ord_dt, o.amt
FROM customers c
JOIN orders o ON o.cust_id = c.cust_id
WHERE c.cust_id BETWEEN 1001 AND 1100
  AND o.status = 'CANCELED'
  AND o.ord_dt >= DATE '2025-01-01'
  • Regular customers have around 5 orders each in 2025. The NL join reads 484 rows and keeps 11, with 557 Buffers. The same hash join hint must read all 16,326 canceled orders, so it uses 3,832.
  • Even for the same statement, the right answer changes with the customer range. Put the hint only in the "high-volume customer screen" query.

Key points

  • On the inner side of an NL join, compare the rows the index passed with the rows kept at the table. The difference is the number of random block reads.
  • Without a histogram, the optimizer treats even skewed keys as average. For a skewed key range, judge by A-Rows rather than trusting E-Rows.
  • Instead of an index that cannot apply the inner filter, another index that holds the filter can shrink one side first, followed by a hash join. This continues scenario 6.