Sub topic 15
Writing OR and IN Conditions for Index Use
This chapter assumes you can read a basic DBMS_XPLAN plan and know how a B-tree index range scan works. It also assumes you know that a composite index is searched from its leading column. The queries use an orders table with these indexes:
CREATE INDEX orders_customer_ix ON orders (customer_id);
CREATE INDEX orders_salesrep_ix ON orders (salesrep_id);
CREATE INDEX orders_cust_date_ix ON orders (customer_id, order_date);How the optimizer treats IN and OR
An IN list of literals or bind variables is shorthand for several equality conditions joined by OR. These two statements mean the same thing:
SELECT order_id FROM orders WHERE customer_id IN (42, 43, 44);
SELECT order_id FROM orders
WHERE customer_id = 42 OR customer_id = 43 OR customer_id = 44;When all the conditions are on one indexed column, Oracle usually runs the index once per value. The plan shows this as an INLIST ITERATOR step above an INDEX RANGE SCAN:
EXPLAIN PLAN FOR
SELECT order_id FROM orders WHERE customer_id IN (42, 43, 44);
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);In the output, look for INLIST ITERATOR followed by an index range scan. The predicate section lists the values under access(...). Each value is a separate, narrow descent of the B-tree. This is efficient while the list is short and the values are selective. With a very long list, the repeated descents can cost more than a single full scan, and the optimizer may choose the scan.
OR across different columns
If the branches test different columns, a single index range cannot cover them. Oracle has two main strategies:
- OR expansion (concatenation): the query is split into separate branches, each using its own index, and the results are combined. The plan shows
CONCATENATIONor, in newer releases, aUNION-ALLview. Oracle addsLNNVLconditions to later branches so that a row matching both conditions is returned once. - B-tree to bitmap conversion: the row IDs from each index are converted to bitmaps, combined with
BITMAP OR, and converted back. This appears asBITMAP CONVERSION. It depends on the edition and configuration, so do not rely on it.
SELECT order_id
FROM orders
WHERE customer_id = 42
OR salesrep_id = 7;Both strategies need a usable index on every branch.
One unindexed branch forces a full scan
Rows that satisfy only the unindexed branch can be anywhere in the table. Oracle must therefore read the whole table to find them, and the indexes on the other branches add nothing.
-- notes has no index. Oracle cannot skip any rows, so expect a full table scan.
SELECT order_id
FROM orders
WHERE customer_id = 42
OR notes LIKE '%urgent%';The same happens when a branch applies a function to its column, such as TRUNC(order_date) = ..., because that branch is not sargable. Fix each branch as described in the earlier chapters on functions, conversions and LIKE. Then check that every branch has an index it can use.
Rewriting an OR as UNION ALL
When the optimizer does not expand an OR and you know each branch is selective, you can write the expansion yourself. Use LNNVL in the second branch to keep rows from appearing twice:
SELECT order_id FROM orders WHERE customer_id = 42
UNION ALL
SELECT order_id FROM orders
WHERE salesrep_id = 7
AND LNNVL(customer_id = 42);LNNVL(condition) is true when the condition is false or unknown, so it also keeps rows where customer_id is NULL. Plain UNION removes duplicates by sorting, which is extra work. Use UNION ALL with LNNVL instead.
The USE_CONCAT and NO_EXPAND hints force or prevent expansion. Use them to compare plans while testing, not as permanent fixes. Statistics change, and a hint that helps today can hurt later.
IN lists and composite indexes
An IN list on the leading column of a composite index works well, because each value becomes its own range scan. Conditions on later columns apply within each scan:
SELECT order_id, order_date
FROM orders
WHERE customer_id IN (42, 43, 44)
AND order_date >= DATE '2026-01-01'
AND order_date < DATE '2026-02-01';With orders_cust_date_ix, the plan has three narrow range scans. An IN list on a later column behaves differently. If customer_id is only constrained by a range, the IN on order_date becomes a filter inside a wider scan. Check the predicate section of the plan to see whether a condition is an access or a filter predicate.
The scans run one value after another, so the combined output is not guaranteed to be in index order. With IN (42, 43) you get the rows for 42 in date order, then the rows for 43 in date order. An ORDER BY order_date therefore cannot use the index order and needs a sort. Top-N queries across several IN values are affected in the same way. Oracle can sometimes avoid the full sort, but do not assume it.
NULL and NOT IN
IN never matches NULL, because NULL = NULL is unknown. This list matches no row where customer_id is NULL:
SELECT order_id FROM orders WHERE customer_id IN (42, NULL);Only 42 matches. To include NULL rows, add OR customer_id IS NULL. This is a second branch, and a single-column B-tree index cannot answer it, as the NULL chapter explains.
NOT IN and <> are poor candidates for index access. They ask for everything except a few values, which is usually most of the table. If the subquery behind a NOT IN can return a NULL, the whole condition returns no rows. Prefer NOT EXISTS for subqueries.
Long IN lists
A list of literals in an IN condition is limited to 1000 items, and exceeding it raises ORA-01795. Long literal lists also produce many distinct SQL texts, which fills the shared pool with parsed statements. There are better options:
SELECT order_id
FROM orders
WHERE customer_id IN (SELECT column_value
FROM TABLE(SYS.ODCINUMBERLIST(42, 43, 44)));You can also load the values into a table, such as a global temporary table, and join to it. In both cases the optimizer treats the list as a join, so see the join chapter for indexing the join column. A collection built with TABLE() gives the optimizer only a default row estimate unless you correct it, so check the cardinality shown in the plan.
Summary of patterns
| Condition | Typical index use |
|---|---|
| `col IN (a, b, c)`, col indexed | `INLIST ITERATOR`, one range scan per value |
| `col1 = a OR col2 = b`, both indexed | OR expansion or bitmap conversion |
| `col1 = a OR unindexed = b` | Full table scan |
| `lead_col IN (...) AND next_col range` | One range scan per list value |
| `col NOT IN (...)` | Rarely an index range scan |
Exercise
A query has WHERE customerid = 42 OR UPPER(email) = '[email protected]'. There is an index on customerid and a plain index on email. The plan shows a full table scan. Why, and what are two ways to fix it?
Show answer
The second branch applies UPPER to the column, so the plain index on email cannot be used. Because one branch has no usable index, Oracle must scan the whole table. Fix it either by creating a function-based index on UPPER(email), or by storing and comparing the email in one agreed case so the function can be dropped. After that, each branch has an index and OR expansion becomes possible.
Exercise
With an index on orders (customerid, orderdate), why can the query WHERE customerid IN (42, 43) ORDER BY orderdate still need a sort step?
Show answer
The IN list runs one range scan per value, giving all rows for 42 in date order, followed by all rows for 43 in date order. The combined stream is not sorted by order_date, so Oracle must sort it unless it applies a special merge step.