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.
| Table | Column | Type | Description |
|---|---|---|---|
CUSTOMERS | CUST_ID | NUMBER | Customer ID (PK) |
CUSTOMERS | REGION | VARCHAR2 | Region. There are 20,000 BUSAN customers |
ORDERS | CUST_ID | NUMBER | Ordering customer ID |
ORDERS | STATUS | VARCHAR2 | DONE 90%, PAID 7%, CANCELED 3% |
ORDERS | ORD_DT | DATE | Order date and time |
| Index | Columns |
|---|---|
CUSTOMERS_PK | CUST_ID |
ORDERS_X1 | CUST_ID, ORD_DT |
ORDERS_X2 | STATUS, 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
DISTINCTis 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
FILTERoperation 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_IDis different, so the effect is almost nil. - After shows
NESTED LOOPS SEMIandHASH 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 ofORDERS_X2or a FULL SCAN). - The remaining cost is reading
CUSTOMERS.REGIONhas no index, so finding the 20,000 Busan customers needs a FULL SCAN, and since all ofc.*is fetched, it is unavoidable. - The NL semi join probes
ORDERS_X1for each Busan customer.STATUSis not inORDERS_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 UNIQUEshrinks 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
EXISTSsemi join.
Key points
FILTERrepeats 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_SJis better when the main query is large,NL_SJwhen 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 withQB_NAMEand specify the hints by name.