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

The 2025 canceled orders of 100 high-volume customers are fetched with a join. The planner assumes one order per customer and picks an NL join, but the inner side actually reads 543 rows from the heap per customer and discards 526. Filtering the orders first with an index that holds the filter conditions and hash-joining cuts Buffers from 54,726 to 4,929.

SQL

Situation

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

Tables

TableColumnsDescription
customerscust_id, cust_nmCustomers 1–100 are high-volume customers with 2,000+ 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

Query

BEFORE · Customer → 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 >= TIMESTAMP '2025-01-01'

Look at Rows, Rows Removed by Filter, and Buffers of the inner Index Scan using orders_x1.

Approach

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

The cost of an NL join is "outer row count × the cost of one inner execution". orders_x1 filters only the join key and date; the status is read from the heap and then filtered by Filter. Every discarded row costs a heap block read.

2. See where the planner's estimate diverges

Running only EXPLAIN shows an estimated row count of 1 for the inner orders_x1 probe. The planner takes the average order count per customer. Consider why the customer range 1–100 breaks that assumption.

3. Find another path that filters first

Of the three conditions, status and ord_dt are held together in one index. If that index filters the orders side first, work out how the customer condition is then applied and which join method fits.

Explanation

Filter orders first through the status index and hash-join

AFTER · NL join disabled

SET LOCAL enable_nestloop = off;
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 >= TIMESTAMP '2025-01-01'

Why it got faster

  • Before finds the 2025 orders through orders_x1 for each of the 100 customers. Each probe reads 543 rows from the heap on average and discards 526 by Filter, keeping 17 per customer (1,668 rows in total). Buffers 54,726.
  • The cause is the planner treating the inner probe as 1 row. That holds for the average customer, but customers 1–100 have 100 times the average.
  • In After, with the NL join disabled, a Bitmap Index Scan on orders_x2 finds status = 'CANCELED' AND ord_dt >= 2025-01-01 as a range (15,989 rows, 99 blocks), and Bitmap Heap Scan reads 4,827 heap blocks once each in block order. The 100 customers are built into a hash table and joined, keeping 1,668 rows. Buffers 4,929.

Why this is optimal

  • The remaining cost is the heap blocks holding the 15,989 canceled orders of 2025. cust_id is not in orders_x2, so the customer condition can be checked only in the heap.
  • 2025 orders are clustered in date order, so the 15,989 rows lie within 4,827 blocks. A bitmap scan does not read the same block twice.

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 >= TIMESTAMP '2025-01-01'

TRY · Regular customers 1001–1100 (hash join)

SET LOCAL enable_nestloop = off;
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 >= TIMESTAMP '2025-01-01'
  • Regular customers have around 5 orders each in 2025. The NL join finds 12 rows with Buffers 790. With the NL join disabled, all 15,989 canceled orders must be read, giving 4,931.
  • Even for the same statement, the right answer changes with the customer range. Put the setting only in the "high-volume customer screen" query.

Key points

  • A large Rows Removed by Filter on the inner side of an NL join means the heap is read and discarded because of a condition the index could not filter.
  • The planner takes the average row count per join key. In a skewed key range, do not trust the estimated rows of EXPLAIN; judge by the actual rows of EXPLAIN ANALYZE.
  • Instead of an index that cannot apply the inner filter, another path is to shrink one side first through a different index that holds the filter, and then hash-join. This continues from scenario 6.