Subquery unnesting: FILTER vs semi join

The cost of an EXISTS subquery depends on whether it runs repeatedly as a FILTER or is unnested into a semi join. The join-then-DISTINCT anti-pattern is covered too.

SQL

Situation

A customer management screen finds Busan customers who canceled an order in 2025. Only the customer list is needed, not the order details, so the query uses an EXISTS subquery. Yet fetching all results takes a long time.

Tables

CUSTOMERS (100,000 rows) holds customers, with 5 regions at 20% each. ORDERS (2 million rows) holds orders accumulated at 2,000 per day.

TableColumnTypeDescription
CUSTOMERSCUST_IDNUMBERCustomer ID (PK)
CUSTOMERSREGIONVARCHAR2Region. There are 20,000 BUSAN customers
ORDERSCUST_IDNUMBEROrdering customer ID
ORDERSSTATUSVARCHAR2DONE 90%, PAID 7%, CANCELED 3%
ORDERSORD_DTDATEOrder date and time
IndexColumns
CUSTOMERS_PKCUST_ID
ORDERS_X1CUST_ID, ORD_DT
ORDERS_X2STATUS, ORD_DT, ORD_ID

REGION has no index.

Query

Fetch the Busan customers who have at least one CANCELED order since 2025-01-01. Each customer appears only once.

BEFORE · NO_UNNEST

SELECT c.*
FROM customers c
WHERE c.region = 'BUSAN'
  AND EXISTS (SELECT /*+ NO_UNNEST */ 1
              FROM orders o
              WHERE o.cust_id = c.cust_id
                AND o.status = 'CANCELED'
                AND o.ord_dt >= DATE '2025-01-01')

Run it and check which operation the subquery appears as in the execution plan, and the Starts and Buffers on the subquery side.

Approach

1. See whether the subquery was unnested into a join

If the execution plan has a FILTER operation with the main query and the subquery hanging side by side under it, the subquery ran as is without being turned into a join. Conversely, a join with ... SEMI means it was unnested into a join.

2. Compare the subquery's Starts with the main query's row count

The FILTER approach runs the subquery once per main query row. If the Starts on the subquery side is nearly equal to the main query's A-Rows, every repetition was carried out.

3. Consider whether subquery caching helps

Oracle keeps the results of a FILTER subquery per input value. When the same input value comes several times, it does not run again. Here the input value is CUST_ID. The value differs for every customer, so think about whether caching helps.

4. Unnest the subquery into a join to give the optimizer options

Once the subquery is unnested into a join, the optimizer can choose the join order and method. Things to consider:

  • Which is larger: the main query side (Busan customers) or the subquery side (2025 canceled orders)
  • Which fits: probing an index row by row, or reading one side whole and matching in memory

Converting to a regular join and removing duplicates with DISTINCT is not a good direction. If a customer has several canceled orders, the join result grows by that much and then shrinks again. That is not work to do in a query that only needs to know "whether one exists".

Explanation

① NL semi join

AFTER · UNNEST NL_SJ

SELECT c.*
FROM customers c
WHERE c.region = 'BUSAN'
  AND EXISTS (SELECT /*+ UNNEST NL_SJ */ 1
              FROM orders o
              WHERE o.cust_id = c.cust_id
                AND o.status = 'CANCELED'
                AND o.ord_dt >= DATE '2025-01-01')

② Hash semi join

AFTER · UNNEST HASH_SJ

SELECT c.*
FROM customers c
WHERE c.region = 'BUSAN'
  AND EXISTS (SELECT /*+ UNNEST HASH_SJ */ 1
              FROM orders o
              WHERE o.cust_id = c.cust_id
                AND o.status = 'CANCELED'
                AND o.ord_dt >= DATE '2025-01-01')

Why it got faster

  • Before shows a FILTER operation in the execution plan. The subquery's Starts = the main query row count (20,000).
  • Subquery results are cached per input value. But every CUST_ID is different, so the effect is almost nil.
  • After shows NESTED LOOPS SEMI and HASH JOIN SEMI. The subquery became a join, so the optimizer can choose the join method and order.
  • A semi join stops once it finds the first match for each main query row.
  • Compare the Buffers of the three runs.

Why this is optimal

  • A semi join stops once it finds just one canceled order per customer. For a query that only asks "whether one exists", there is no way to read less.
  • The hash semi join reads the 2025 canceled orders side only once and matches it with the Busan customers in memory. There are no repeated probes. Check in the execution plan which path read the canceled orders (the (STATUS, ORD_DT) segment of ORDERS_X2 or a FULL SCAN).
  • The remaining cost is reading CUSTOMERS. REGION has no index, so finding the 20,000 Busan customers needs a FULL SCAN, and since all of c.* is fetched, it is unavoidable.
  • The NL semi join probes ORDERS_X1 for each Busan customer. STATUS is not in ORDERS_X1, so the canceled status must be checked at the table. So the larger the main query, the better the hash side.

A related anti-pattern: join then DISTINCT

TRY · Join then 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 >= DATE '2025-01-01'
  • The join multiplies rows by the number of orders per customer, and HASH UNIQUE shrinks them again. The A-Rows of the join step show how much they grew.
  • If only "whether one exists" is needed, write it as an EXISTS semi join.

Key points

  • FILTER repeats the subquery as many times as the main query has rows. It is worth using only when caching works well (few distinct input values).
  • Once unnested into a semi join, NL, hash, or merge can be chosen. HASH_SJ is better when the main query is large, NL_SJ when it is small.
  • Hints written inside the subquery (UNNEST, NL_SJ, HASH_SJ) and hints written in the main query go in different places. In complex queries, name the query blocks with QB_NAME and specify the hints by name.