Splitting OR conditions and optional search conditions
Split OR-combined conditions and the search-screen pattern "apply the condition if a value is given" so the optimizer uses a different execution plan for each branch.
SQL
The Before queries in this scenario contain the NO_OR_EXPAND hint, because the optimizer in 19c and later splits OR conditions into a UNION ALL form on its own (OR-Expansion). The hint blocks that transformation to reproduce cases where it does not happen (older versions, complex queries). Runs without the hint are compared as well.
Situation
Two order lookup screens are reported as slow. Both have an OR in the WHERE clause, and the condition columns have indexes.
Tables
ORDERS (2 million rows) holds orders accumulated at 2,000 per day since 2023-01-01. Customers 1–100 (B2B customers) each have 2,000 or more orders; the other customers have about 18 each.
| Column | Type | Description |
|---|---|---|
CUST_ID | NUMBER | Ordering customer ID. No NOT NULL constraint |
ORD_NO | VARCHAR2 | Order number |
ORD_DT | DATE | Order date and time |
| Index | Columns |
|---|---|
ORDERS_X1 | CUST_ID, ORD_DT |
ORDERS_X3 | ORD_NO |
① Optional condition on a search screen
A lookup screen that means "if a customer number is entered, only that customer; if left blank, everyone". It returns orders since a base date. Run it once with the bind cid left blank (NULL) and once with a value.
BEFORE · IS NULL OR pattern
SELECT /*+ NO_OR_EXPAND */ *
FROM orders
WHERE (:cid IS NULL OR cust_id = :cid)
AND ord_dt >= :from_dt
② Different columns combined with OR
A screen that finds orders by customer number or order number. It returns orders matching either condition, without duplicates.
BEFORE · OR condition
SELECT /*+ NO_OR_EXPAND */ *
FROM orders
WHERE cust_id = :cid OR ord_no = :ono
Run both queries and check in the execution plan which index each condition uses, or fails to use. For ①, also see whether the execution plan changes between a value in cid and a blank one.
Approach
1. Change the bind values and see whether the execution plan changes
In ①, with a value in cid, only one customer's orders are needed. ORDERS_X1 (CUST_ID, ORD_DT) is the exact index for that. If the execution plan stays the same anyway, a single execution plan was built to handle both "no value" and "a value".
2. Think about why OR blocks the index search
One index can set only one search range. :cid IS NULL OR cust_id = :cid can mean "everything" or "one customer" depending on the bind value. cust_id = :cid OR ord_no = :ono in ② needs two different indexes. Either way, it cannot be expressed as a single search.
3. List the methods the optimizer can choose
Faced with an OR condition, the optimizer has roughly three options.
- FULL SCAN: reads everything and filters. The worst choice when the result is small.
- BITMAP OR: collects ROWIDs from two indexes and merges them. Extra conversion and sort steps are added.
- OR-Expansion: splits the query per condition and connects the parts with UNION ALL. Each branch uses its matching index.
Check which one appears in the execution plan, and think about whether the last method can be written by hand.
4. Check that splitting does not change the result
Splitting with UNION ALL requires two things.
- In ①, only one branch may produce results for a given bind value. Each branch needs mutually exclusive conditions such as
:cid IS NULL/:cid IS NOT NULL. - In ②, a row satisfying both conditions must not appear twice. The second branch must exclude rows matching the first branch's condition. Here
cust_idcan be NULL, socust_id <> :cidis not enough.
Connecting with UNION (duplicate removal) is another way, but it adds the cost of removing duplicates by sort or hash. What needs to change is the query; no new index is needed.
Explanation
① Optional condition → split with UNION ALL
AFTER · Split with UNION ALL
SELECT *
FROM orders
WHERE :cid IS NULL
AND ord_dt >= :from_dt
UNION ALL
SELECT *
FROM orders
WHERE :cid IS NOT NULL
AND cust_id = :cid
AND ord_dt >= :from_dt
Why it got faster
- Before is a FULL SCAN even when
:cidhas a value, because a single execution plan has to handle both "no value" and "a value". - After adds a
FILTERto each branch. TheFILTER (:CID IS NULL)branch is a FULL SCAN, and theFILTER (:CID IS NOT NULL)branch is anORDERS_X1RANGE SCAN. At run time, only one side actually runs, depending on the bind value (check whether each branch's Starts is 0).
See the optimizer split it on its own by running without the hint.
TRY · No hint (automatic transformation)
SELECT *
FROM orders
WHERE (:cid IS NULL OR cust_id = :cid)
AND ord_dt >= :from_dt
- "No hint" is the result of the optimizer rewriting the query into the same shape as After.
VIEW VW_ORE_xxxxappears in the execution plan.
② OR condition → UNION ALL + LNNVL
AFTER · UNION ALL + LNNVL
SELECT *
FROM orders
WHERE cust_id = :cid
UNION ALL
SELECT *
FROM orders
WHERE ord_no = :ono
AND LNNVL(cust_id = :cid)
Why it got faster
- Before resolves to
BITMAP OR. It collects ROWIDs from two indexes, merges them as bitmaps, and then reads the table. Better than a FULL SCAN, but sort and conversion steps are added. - In After, each branch uses
ORDERS_X1orORDERS_X3directly. In this case the Buffers difference is small. The goal is to see which method gets chosen. LNNVL(condition)is true when the condition is false or UNKNOWN. It keeps rows already returned by the first branch from appearing again in the second branch.
Why this is optimal
- Each branch reads only the needed range with the one index that matches its condition. In ①, with a value in
cid, only that customer's orders since the base date are read; in ②, only one customer and one order number. - In ①, with
cidblank, the result is every order since the base date. A FULL SCAN remains then, but the result itself is large, so the cost cannot be reduced. There is no index usable with the base date condition alone (an index withORD_DTas the leading column), and the schema is not changed. - The remaining cost is visiting the table for each result row (
TABLE ACCESS BY INDEX ROWID). It is unavoidable because ofSELECT *. With a heavy customer (1–100), this cost grows with the number of result rows.
Key points
- When processing OR-combined conditions, the optimizer chooses among FULL SCAN, BITMAP OR, and OR-Expansion (UNION ALL). To make each branch use a different index, split with UNION ALL.
- Even for the same SQL, whether the automatic transformation happens depends on the DB version. In 11g, ① (the optional condition) had to be split by hand.
- The
cust_id = NVL(:cid, cust_id)pattern is also common, but it returns the same result as the original query only when the column is NOT NULL (for a row wherecust_idis NULL,cust_id = cust_idis not true). - For a search screen with several optional conditions, the answer is ultimately dynamic SQL (adding only the entered conditions to the WHERE clause). Bind variables are still used.