OR Conditions Across Two Tables: Filter After 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 be filtered first, so the whole orders table is read, all 14,000 orders of the last 7 days are joined, and then 541 are picked. Splitting the OR into two UNION ALL branches and excluding the overlap with NULL taken into account cuts Buffers from 13,802 to 5,737.

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

TableColumnDescription
ordersord_dt, status14,000 orders since 2025-09-20, 399 of them canceled
customersgrade1,000 VIPs (1%)
IndexColumns
customers_pk (clustered)cust_id
customers_x1grade
orders_pk (clustered)ord_id
orders_x1cust_id, ord_dt
orders_x2status, ord_dt, ord_id

No index starts with ord_dt alone.

Query

BEFORE · OR filter after 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 >= '2025-09-20'
  AND (c.grade = 'VIP' OR o.status = 'CANCELED')

Look at the actual rows of the Hash Match and where the OR condition is applied above it (the Residual in the operator properties).

Approach

1. See which operator evaluates the OR condition

c.grade = 'VIP' can be decided from the customer alone, and o.status = 'CANCELED' from the order alone. Combined with OR, both tables must be present to decide true or false, so this condition becomes a residual predicate of the join operator. Only o.ord_dt >= … remains usable before the join.

2. Consider how each OR branch would read on its own

"Recent orders of VIP customers" is a matter of finding 1,000 customers and attaching their orders. "Recent canceled orders" is a matter of a single orders_x2 range. 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 on both sides. Removing duplicates with UNION can also remove rows with equal values that were in the original result. From the second branch, exclude only the rows for which the first branch's condition is true, but keep rows where grade is NULL.

Explanation

OR → two UNION ALL branches

AFTER · UNION ALL + NULL handling

SELECT 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 >= '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 >= '2025-09-20'
  AND (c.grade <> 'VIP' OR c.grade IS NULL)

Why it got faster

  • Before reads orders_pk (clustered = the table) from start to end to pick the 14,000 orders of the last 7 days (13,201 pages), joins them with all 100,000 customers (601 pages) by Hash Match, and keeps 541 rows with the OR condition. Buffers are 13,802.
  • After runs as two branches under a Concatenation. The first branch picks the 1,000 VIPs from the customers table (601 pages), searches orders_x1 per customer (1,000 times, 3,004 pages) to find 144 orders from the last 7 days, and fetches the remaining columns with a Key Lookup. That is 4,001 pages.
  • The second branch finds only the 399 canceled orders of the last 7 days through orders_x2 (5 pages), and after 399 Key Lookups (1,131 pages) joins the customers and keeps 397 rows excluding VIPs. That is 1,736 pages. Total Buffers are 5,737, and the result is 541 rows, the same as Before.

Why NULL handling is needed

c.gradeFirst branch grade = 'VIP'Second branch grade <> 'VIP' OR grade IS NULLIf the second branch used only grade <> 'VIP'
'VIP'True → includedFalse → excludedExcluded
'GOLD'FalseTrue → includedIncluded
NULLUNKNOWN → excludedTrue → includedUNKNOWN → excluded from both

In the original OR, canceled orders of customers whose grade is NULL also appear in the result through o.status = 'CANCELED' alone. The current data has no NULL grades, but grade allows NULL. SQL Server has neither Oracle's LNNVL nor PostgreSQL's IS NOT TRUE, so IS NULL is added.

Why this is optimal

  • The second branch reads only the 399 canceled orders that make up the result.
  • The remaining cost of the first branch is the orders_x1 search for 1,000 VIPs. Most VIPs have no order within 7 days, but confirming that requires one index descent per customer.
  • The first branch read the 601 pages of the customers table instead of customers_x1 (the grade index), because it is cheaper than Key Lookups for 1,000 VIPs (about 3,000 pages).

Key points

  • Combining conditions on different tables with OR pushes that condition into a residual predicate of the join operator. Write down separately which conditions can filter before the join.
  • Splitting an OR into UNION ALL lets each branch choose its own driving table and join method. It appears as Concatenation in the execution plan.
  • Exclude overlapping rows with a condition that accounts for NULL, not with UNION. Using only <> makes rows with NULL disappear from the result.