OR across two tables: filter after the join → UNION ALL
When one side of an OR is a customer column and the other an order column, as in "orders of VIP customers or canceled orders", neither table can filter first, so all 14,000 orders from the last 7 days are joined before 546 rows are picked. Splitting the OR into two UNION ALL branches and removing overlapping rows with LNNVL lets each branch drive from its own condition, and Buffers drop from 14,527 to 3,408.
SQL
Situation
Every morning the customer management team checks the orders of the last 7 days that are "orders of VIP customers or canceled orders". The result is a little over 500 rows, yet the query reads the whole orders table.
Tables
| Table | Columns | Description |
|---|---|---|
ORDERS | ORD_DT, STATUS | 14,000 orders since 2025-09-20. 409 of them canceled |
CUSTOMERS | GRADE | 1,000 VIPs (1%) |
| Index | Columns |
|---|---|
CUSTOMERS_X1 | GRADE |
ORDERS_X1 | CUST_ID, ORD_DT |
ORDERS_X2 | STATUS, ORD_DT, ORD_ID |
No index starts with ORD_DT alone. The statistics have no histogram, so the optimizer treats the three grade values as equally frequent (33,333 customers each).
Query
BEFORE · OR filter after the join
SELECT o.ord_id, o.ord_dt, o.status, o.amt, c.cust_nm, c.grade
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE o.ord_dt >= DATE '2025-09-20'
AND (c.grade = 'VIP' OR o.status = 'CANCELED')
Check whether the A-Rows of HASH JOIN equal the final 546 rows.
Approach
1. See which table the OR condition is evaluated on
c.grade = 'VIP' needs only the customer, and o.status = 'CANCELED' needs only the order. Joined by OR, the condition is true or false only when both tables are present. So it is applied only after the join, and the only condition usable before the join is o.ord_dt >= ….
2. Consider how each OR branch would be read on its own
"Recent orders of VIP customers" means finding 1,000 customers through a customer index and attaching their orders. "Recent canceled orders" is a single range on ORDERS_X2. The two branches have different driving tables.
3. Work out what keeps the result the same when splitting
If the two branches are combined with UNION ALL, canceled orders of VIP customers appear in both branches. Removing duplicates with UNION can also remove rows with equal values that were in the original result. Find a way to exclude from the second branch the rows where the first branch's condition is true, without losing rows whose GRADE is NULL.
4. Check the optimizer's estimate for each branch
Even after splitting, check separately whether the optimizer picks the right plan for each branch. If it estimates 33,333 VIPs, it may pick a hash join for the first branch too.
Explanation
OR → two UNION ALL branches
AFTER · UNION ALL + LNNVL
SELECT /*+ LEADING(c) USE_NL(o) INDEX(c customers_x1) */
o.ord_id, o.ord_dt, o.status, o.amt, c.cust_nm, c.grade
FROM customers c
JOIN orders o ON o.cust_id = c.cust_id
WHERE c.grade = 'VIP'
AND o.ord_dt >= DATE '2025-09-20'
UNION ALL
SELECT o.ord_id, o.ord_dt, o.status, o.amt, c.cust_nm, c.grade
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE o.status = 'CANCELED'
AND o.ord_dt >= DATE '2025-09-20'
AND LNNVL(c.grade = 'VIP')
Why it got faster
- Before reads the whole orders table (13,950 blocks) to pick the 14,000 orders of the last 7 days, hash-joins them with 100,000 customers, and keeps 546 rows with the OR condition. Buffers 14,527.
- After's first branch finds 1,000 VIPs through
CUSTOMERS_X1and, for each customer, finds orders of the last 7 days inORDERS_X1(1,000 probes, 143 rows). 2,729 blocks. - The second branch reads only the 409 canceled orders of the last 7 days through
ORDERS_X2(102 blocks), joins them with customers, and keeps 403 rows after excluding VIPs. 679 blocks. Total Buffers 3,408, and the result is 546 rows, the same as Before. - The hint in the first branch corrects the optimizer's estimation error. Without a histogram it sees 33,333 VIPs, so a hash join looks cheaper than an NL that finds orders per customer. Run without the hint, the first branch also reads all orders, and Buffers reach 15,206, more than Before.
Why LNNVL is needed
c.grade | First branch grade = 'VIP' | Second branch LNNVL(grade = 'VIP') | If the second branch used grade <> 'VIP' |
|---|---|---|---|
'VIP' | true → returned | false → excluded | excluded |
'GOLD' | false | true → returned | returned |
| NULL | unknown → excluded | true → returned | unknown → excluded from both |
LNNVL(condition) is true when the condition is false or unknown. In the original OR, a canceled order of a customer with a NULL grade is in the result through o.status = 'CANCELED' alone, so the second branch must take that row. The current data has no NULL grades, but the column has no NOT NULL constraint.
Why this is optimal
- The second branch reads only the 409 canceled orders that become the result.
- The remaining cost of the first branch is the
ORDERS_X1probes for 1,000 VIPs (about 2,000 blocks). Most VIPs have no orders in the 7 days, but confirming that requires descending the index once per customer. Reading 14,000 orders of 7 days from the order side and looking up customers costs more.
Variation: the optimizer's OR expansion
TRY · OR_EXPAND hint
SELECT /*+ OR_EXPAND */ o.ord_id, o.ord_dt, o.status, o.amt, c.cust_nm, c.grade
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE o.ord_dt >= DATE '2025-09-20'
AND (c.grade = 'VIP' OR o.status = 'CANCELED')
- Oracle has a transformation that splits an OR into branches (OR Expansion, Scenario 9). With the hint, the execution plan shows
VIEW VW_ORE_…andUNION-ALL, and the second branch getsLNNVL. It has the same shape as the hand-written After. - Still, Buffers are 15,206. The VIP branch still reads all orders and hash-joins. Splitting into branches and picking the right plan for each branch are separate problems, and in this statement the estimate of 33,333 for 1,000 VIPs causes the second one. The same estimate is why the optimizer does not choose OR expansion without the hint.
Key points
- When conditions on different tables are joined by OR, the condition is pushed after the join. Write down separately which conditions can filter before the join.
- Splitting an OR with
UNION ALLlets each branch choose its own driving table and join method. Remove overlapping rows withLNNVL(in other DBs,condition IS NOT TRUEorNOT (…) OR … IS NULL), notUNION. - After splitting, check E-Rows and A-Rows for each branch. If the estimate is wrong, the benefit of splitting disappears.