Rows Discarded Inside an NL Join: Data Skew and Filtering Indexes
A query fetching the 2025 canceled orders of 100 high-volume customers carries over the LOOP JOIN and orders_x1 hints that were fast on the regular-customer screen. The inner side Key Lookups 54,271 orders and then discards 97%, for 156,520 Buffers. Removing the hints lets the optimizer intersect two indexes, cutting Key Lookups to 1,671 and Buffers to 5,354.
SQL
Situation
The account management screen takes a customer range and shows those customers' 2025 canceled orders. Because the execution plan once fluctuated for regular customer ranges, a developer pinned the NL join and orders_x1 with hints. Since then, the screen is slow when the high-volume customer range (customers 1 to 100) is selected.
Tables
| Table | Column | Description |
|---|---|---|
customers | cust_id, cust_nm | Customers 1 to 100 are high-volume customers with 2,000+ orders each |
orders | cust_id, status, ord_dt, amt | Status CANCELED is 3% |
| Index | Columns |
|---|---|
customers_pk (clustered) | cust_id |
orders_pk (clustered) | ord_id |
orders_x1 | cust_id, ord_dt |
orders_x2 | status, ord_dt, ord_id |
Each entry of a nonclustered index also contains the clustered key ord_id.
Query
BEFORE · LOOP JOIN + orders_x1 hint
SELECT c.cust_id, c.cust_nm, o.ord_id, o.ord_dt, o.amt
FROM customers c
INNER LOOP JOIN orders o WITH (INDEX(orders_x1)) ON o.cust_id = c.cust_id
WHERE c.cust_id <= 100
AND o.status = 'CANCELED'
AND o.ord_dt >= '2025-01-01'
Compare the actual rows of Index Seek (orders_x1), the actual rows of the Clustered Index Seek (orders_pk) (Key Lookup) above it, and the logical reads.
Approach
1. Compare rows read and rows kept on the inner side
The cost of an NL join is "outer rows × cost of one inner execution". orders_x1 filters only the join key and the date; the status is checked by going to the clustered index with a Key Lookup. Each discarded row costs one descent of the B-tree.
2. See what changes when the customer range changes
Regular customers have about 5 orders each in 2025, so the hint fits them. Customers 1 to 100 have 540 each. Consider what happens to a pinned plan when the same statement sees 100 times as many rows.
3. Find a way to filter the status condition in an index
orders_x2 holds status and ord_dt. Both orders_x1 and orders_x2 carry ord_id in every entry. Matching the ord_id values found in the two indexes can reduce rows before the Key Lookup.
Explanation
Removing the hints and leaving it to the optimizer
AFTER · No hints (index intersection)
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 >= '2025-01-01'
Why it got faster
- Before finds the 2025 orders of each of the 100 customers through
orders_x1and passes up 54,271 rows (505 pages), Key Lookups all 54,271 to check the status, and keeps 1,671. OneKey Lookupis 3 pages, so that is 156,013 pages. Buffers are 156,520. - In After, the optimizer intersects the two nonclustered indexes. It reads 16,028 rows with
status = 'CANCELED' AND ord_dt >= 2025-01-01fromorders_x2(60 pages) and 54,271 rows withcust_id <= 100 AND ord_dt >= 2025-01-01fromorders_x1(505 pages), and aHash Matchkeeps only the 1,671 rows with matchingord_id. TheKey Lookupruns only for those 1,671 (4,787 pages). Finally it joins the 100 customers. Buffers are 5,354. - The customer condition
c.cust_id <= 100is also applied aso.cust_id <= 100through the join condition, soorders_x1is searched over the customer range in one pass.
Why this is optimal
- Each of the two index searches is a single range and reads only leaf pages. The
Key Lookuphappens only for the 1,671 rows that make up the result. As long as the result includesamt, thisKey Lookupis unavoidable. - Index intersection pays off when each index filters out many rows and few rows overlap. In this query, 54,271 and 16,028 rows shrink to 1,671.
Variation: 100 regular customers
TRY · Regular customers 1001 to 1100 (no hints)
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 >= '2025-01-01'
- For regular customers,
orders_x1yields only 494 rows. The optimizer again intersects the two indexes, keeps 12 rows, and finishes with 108 Buffers. Because statistics (histograms) estimate a different row count per range, it picks a plan fitting each range without hints.
Key points
- On the inner side of an NL join, compare the rows the index passed up with the rows left after the
Key Lookup. The difference is how many times the clustered index is descended. - Join hints and index hints keep the plan fixed even when the data differs. Before adding a fixed hint to a query that receives skewed key ranges, run it for each range.
- SQL Server can intersect two nonclustered indexes of the same table on the clustered key. Two
Index Seeks on the same table combined by aHash Matchin the execution plan indicate this method.