Sub topic 10
Indexing Expressions with Function-Based Indexes
Prerequisites and the problem they solve
This chapter assumes that you know SELECT, WHERE, table creation, and the basic idea of an execution plan. It also assumes that an orders table exists with an email column.
A normal B-tree index stores the value in a column. For example, an index on email helps this predicate:
WHERE email = '[email protected]'It usually does not help this predicate as directly:
WHERE UPPER(email) = '[email protected]'The query applies the UPPER function to every candidate value. An index on the original email values does not store the uppercase results.
A function-based index stores the result of an expression instead. The expression can contain a built-in function, arithmetic, or several columns.
Create and use a function-based index
Create an index on the exact expression used for a case-insensitive lookup:
CREATE INDEX orders_email_upper_ix
ON orders (UPPER(email));Now write the query with the same expression:
SELECT order_id
FROM orders
WHERE UPPER(email) = '[email protected]';Oracle can use orders_email_upper_ix to find rows whose stored expression value is [email protected]. The index avoids calculating UPPER(email) for every row during the lookup.
The expression does not have to be a single function. For example, an index can normalize a phone number:
CREATE INDEX customers_phone_digits_ix
ON customers (
REPLACE(REPLACE(phone, ' ', ''), '-', '')
);A query that uses the same normalization can then search the indexed result:
SELECT customer_id
FROM customers
WHERE REPLACE(REPLACE(phone, ' ', ''), '-', '') = '5551234567';The expression must be suitable for indexing, and the query must use a matching expression in a form Oracle can recognize. An index on UPPER(email) is not automatically an index on LOWER(email).
Requirements and common errors
Oracle must be able to produce a stable index value. Built-in functions such as UPPER are common choices. If you use your own PL/SQL function, declare it DETERMINISTIC only when the same input always produces the same output.
A function that depends on the current time, session settings, table lookups, or changing package state is a poor choice. If its result changes for the same input, the stored index value can become inconsistent with the query result.
Session and linguistic settings also matter. Case conversion and sorting can depend on NLS configuration. Decide which comparison rules the application requires before creating the index.
Expressions that produce NULL for every indexed column do not create an index entry. Therefore, a function-based index may not help a query that searches for rows where the expression is null.
After creating the index, gather statistics so the optimizer has current information:
BEGIN
DBMS_STATS.GATHER_TABLE_STATS(
ownname => USER,
tabname => 'ORDERS',
cascade => TRUE
);
END;
/Then inspect the plan:
EXPLAIN PLAN FOR
SELECT order_id
FROM orders
WHERE UPPER(email) = '[email protected]';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);Look for an index access operation involving ORDERS_EMAIL_UPPER_IX, such as an index range scan. The optimizer may still choose a full table scan if it estimates that many rows match or if the table is small.
Function-based index or virtual column?
A virtual column defines the expression in the table and lets you index the named column:
ALTER TABLE orders ADD (
email_uc VARCHAR2(100)
GENERATED ALWAYS AS (UPPER(email)) VIRTUAL
);
CREATE INDEX orders_email_uc_ix
ON orders (email_uc);A function-based index is compact when the expression is needed only for one access path. A virtual column makes the derived value visible in the table definition and can make application SQL and metadata easier to understand.
Exercise
Why does an index on email usually not directly support WHERE UPPER(email) = '[email protected]', and what index can support it?
Show answer
The normal index stores the original email values, not their uppercase results. An index on UPPER(email) stores the expression result and can support the matching predicate.