EXISTS and semi joins: the join method decides the cost

SQL Server always unnests EXISTS subqueries into semi joins. Whether that semi join runs as NL or hash changes the cost 7 times. It also shows a join followed by DISTINCT turning into the same plan.

SQL

Situation

A report extracts Busan customers who canceled an order in 2025. OPTION (LOOP JOIN) was attached earlier on the grounds that "EXISTS checks per row, so NL is right".

Tables

TableColumnDescription
customerscust_id, region100,000 customers, about 20,000 in Busan
orderscust_id, status, ord_dtAbout 16,000 canceled orders in 2025
IndexColumns
customers_pk (clustered)cust_id
orders_x1cust_id, ord_dt
orders_x2status, ord_dt, ord_id

Busan customers with a canceled order in 2025

BEFORE · LOOP JOIN hint

SELECT c.*
FROM customers c
WHERE c.region = 'BUSAN'
  AND EXISTS (SELECT 1
              FROM orders o
              WHERE o.cust_id = c.cust_id
                AND o.status = 'CANCELED'
                AND o.ord_dt >= CAST('2025-01-01' AS datetime2(0)))
OPTION (LOOP JOIN)

Run it and check which operator the subquery became and how many inner searches there are.

Approach

1. Check whether the subquery was unnested into a join

SQL Server execution plans rarely show a separate operator that runs a subquery per row. EXISTS turns into a join called Left Semi Join (or Right Semi Join). The remaining question is which join method it uses.

2. Compare the sizes of the two sets

There are 20,000 Busan customers and 16,000 canceled orders in 2025. Neither is small, so reading each once and matching pairs can beat repeated searches.

3. Check what the hint enforced

OPTION (LOOP JOIN) pins even the semi join to NL. Within this constraint, the optimizer picks an order that groups the orders by customer (removing duplicates) and then finds the customers.

Explanation

Hint removed → hash semi join

AFTER · No hint (hash semi join)

SELECT c.*
FROM customers c
WHERE c.region = 'BUSAN'
  AND EXISTS (SELECT 1
              FROM orders o
              WHERE o.cust_id = c.cust_id
                AND o.status = 'CANCELED'
                AND o.ord_dt >= CAST('2025-01-01' AS datetime2(0)))

Why it got faster

  • Before finds the 2025 canceled orders in the whole table (Clustered Index Scan, about 13,200), groups them by customer (Hash Match Aggregate, 13,462 customers), and seeks customers_pk per customer (Starts 13,462). Buffers about 39,300.
  • After seeks only the 2025 canceled orders with orders_x2 (60), hash joins with orders_x1 to get the customer IDs, and matches them against the customer table with Hash Match (Right Semi Join). Buffers about 5,600.

Why this is optimal

  • Each input is read once and there are no repeated searches. The remaining cost is the full read of orders_x1 (about 5,000), incurred to get the customer IDs because orders_x2 has no cust_id.

Variation: join followed by DISTINCT

TRY · Join followed by DISTINCT

SELECT DISTINCT c.*
FROM customers c
JOIN orders o ON o.cust_id = c.cust_id
WHERE c.region = 'BUSAN'
  AND o.status = 'CANCELED'
  AND o.ord_dt >= CAST('2025-01-01' AS datetime2(0))
  • This is the anti-pattern of multiplying customers by their order count and then reducing them with DISTINCT. In this case SQL Server recognizes that the meaning is the same and turns it into the same semi join plan as After (Buffers about 5,600). However, this transformation does not happen if the customer side has no PK or the SELECT list differs, so write "existence" with EXISTS from the start.

Key points

  • SQL Server unnests EXISTS/IN subqueries into semi joins. The cost depends on the join method of that semi join (NL, hash, merge).
  • Query hints such as OPTION (LOOP JOIN) apply to every join in the statement (including semi joins).
  • DB differences: Oracle can leave the subquery unnested and run it per row with FILTER (NO_UNNEST), so the difference is larger. PostgreSQL unnests EXISTS into a semi join and changes the join method with enable_* settings.