Sub topic 15
Writing OR and IN Conditions for Index Use
This chapter assumes you know basic SELECT, WHERE, JOIN, and ORDER BY syntax, Oracle data types, and the general purpose of an index. You should also be able to run SQL in SQL*Plus, SQL Developer, or a similar client.
Use `IN` for alternatives on one column
When several values are allowed for the same column, write an IN condition:
SELECT order_id, customer_id, order_date
FROM orders
WHERE customer_id IN (42, 84, 126);This expresses the same logic as:
SELECT order_id, customer_id, order_date
FROM orders
WHERE customer_id = 42
OR customer_id = 84
OR customer_id = 126;If customer_id has a B-tree index, Oracle may use an IN-list iterator. The iterator performs multiple index lookups and combines the results. Oracle can still choose a full table scan when the list matches many rows or when a scan costs less.
Use `OR` when the conditions differ
OR is useful when each alternative has a different predicate:
SELECT customer_id, last_name, phone_number
FROM customers
WHERE last_name = 'Smith'
OR phone_number = '5551234';With indexes on both last_name and phone_number, Oracle may transform this into separate indexed searches and combine the results. This transformation is called OR expansion. It is cost-based, so an index is not guaranteed.
Keep each branch searchable. For example, this branch applies a function to the column:
-- The second branch may not use a normal index on last_name
SELECT customer_id
FROM customers
WHERE last_name = 'Smith'
OR UPPER(last_name) = 'JONES';Rewrite the expression when possible, or create a matching function-based index when the expression is required.
These two predicates are different:
-- customer_id must be 42, and either status is OPEN or PENDING
WHERE customer_id = 42
AND (status = 'OPEN' OR status = 'PENDING')
-- Either the whole first condition is true, or status is PENDING
WHERE customer_id = 42
OR status = 'PENDING'The first form can often use a composite index beginning with customer_id, such as (customer_id, status). The second form may require separate access paths.
Check the chosen plan
Inspect the plan instead of assuming that IN or OR uses an index:
EXPLAIN PLAN FOR
SELECT order_id
FROM orders
WHERE customer_id IN (42, 84, 126);
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);Look for an INDEX RANGE SCAN, possibly under an INLIST ITERATOR, or for an OR-related operation. Compare estimated rows and cost with the full-scan alternative.
Large, generated IN lists can become unwieldy. For many values, load them into a temporary table or use a collection and join to it. This gives Oracle statistics and a relational input that is easier to optimize.
Exercise
What is a suitable rewrite for WHERE customer_id = 42 OR customer_id = 84 OR customer_id = 126, and what plan operation might Oracle use?
Show answer
Rewrite it as WHERE customer_id IN (42, 84, 126). With a usable index, Oracle might use an IN-list iterator with multiple index range scans, but the optimizer can choose a full table scan when that is cheaper.