Sub topic 5
Function-Based Indexes for Expressions You Cannot Remove
Assume you know basic SELECT and WHERE syntax, Oracle data types, and the purpose of a normal B-tree index. This chapter shows how to index a calculated value when rewriting the predicate is not practical.
Why a normal index may not help
A normal index stores the value of a column. If a query applies a function to that column, Oracle usually cannot use the column's ordinary index for a range search.
For example, an index on last_name does not directly store UPPER(last_name):
SELECT customer_id, last_name
FROM customers
WHERE UPPER(last_name) = 'SMITH';Oracle would need to calculate UPPER(last_name) for many rows before comparing the result. A function-based index stores that calculated value instead.
CREATE INDEX customers_upper_last_name_ix
ON customers (UPPER(last_name));
SELECT customer_id, last_name
FROM customers
WHERE UPPER(last_name) = 'SMITH';The query expression matches the index expression, so Oracle can search the index for the stored value 'SMITH'.
How the index works
A function-based index calculates its expression when an index entry is created or changed. Oracle then orders those calculated values in the index's B-tree structure.
The expression must be deterministic for indexing. A deterministic expression returns the same result whenever its input values are the same. Built-in functions such as UPPER, LOWER, TRUNC, and arithmetic expressions commonly meet this requirement.
You can index more than one expression:
CREATE INDEX orders_day_amount_ix
ON orders (TRUNC(order_date), order_amount);
SELECT order_id
FROM orders
WHERE TRUNC(order_date) = DATE '2026-01-15'
AND order_amount >= 100;The index helps because it stores both the truncated date and the amount.
Matching the expression
Write the predicate using the same expression and compatible settings used by the index. Small changes can affect whether Oracle recognizes the index as a match.
For example, if the index uses UPPER(last_name), use that expression in the predicate:
CREATE INDEX customers_upper_last_name_ix
ON customers (UPPER(last_name));
-- Matches the indexed expression
SELECT customer_id
FROM customers
WHERE UPPER(last_name) = 'SMITH';After creating the index, gather optimizer statistics so Oracle can estimate its usefulness:
BEGIN
DBMS_STATS.GATHER_TABLE_STATS(
ownname => USER,
tabname => 'CUSTOMERS',
cascade => TRUE
);
END;
/Check the actual plan with DBMS_XPLAN. Look for the function-based index in the plan and for the expression under Predicate Information.
Costs and limitations
The index adds storage and must be maintained when the source column changes. It can slow inserts, updates, and deletes. Prefer rewriting the predicate when a simple range comparison can solve the problem. Use a function-based index when the expression is required by the application's search rules or cannot be removed safely.
Exercise
Which index supports this predicate, and what must match? CREATE INDEX ix1 ON customers (LOWER(email)); SELECT customer_id FROM customers WHERE LOWER(email) = '[email protected]';
Show answer
The query can use IX1 because it applies the same LOWER(email) expression. The expression and comparison value must be compatible, and current optimizer statistics help Oracle choose the index.