Sub topic 11
Making Queries Match a Function-Based Index
This chapter assumes you know SELECT, WHERE, table creation, and that Oracle's optimizer chooses an execution plan. A function-based index is an index on an expression rather than directly on a stored column. Oracle calculates the expression for each indexed row and stores the result in the index.
Why the Query Expression Matters
Suppose email addresses are stored with mixed capitalization. This query applies UPPER to the column:
SELECT order_id
FROM orders
WHERE UPPER(email) = '[email protected]';A normal index on email stores the original values. It cannot efficiently find the result of UPPER(email) for every row. Create an index on the same expression instead:
CREATE INDEX orders_email_upper_ix
ON orders (UPPER(email));The query must use the indexed expression for Oracle to consider this index:
SELECT order_id
FROM orders
WHERE UPPER(email) = '[email protected]';The expression also includes its arguments and operators. An index on TRIM(email) does not directly match a query using UPPER(email). An index on amount * 1.2 does not directly match a query using amount * 1.20, even if the results are numerically equivalent. Write the query in the same form as the index definition whenever possible.
| Index expression | Matching predicate | Different expression |
|---|---|---|
| `UPPER(email)` | `UPPER(email) = :email` | `LOWER(email) = :email` |
| `TRIM(code)` | `TRIM(code) = :code` | `code = :code` |
| `amount * 1.2` | `amount * 1.2 > 100` | `amount > 100` |
Requirements for Function-Based Indexes
Oracle must be able to calculate the expression consistently. Built-in functions such as UPPER, LOWER, TRIM, and arithmetic expressions are common choices.
A user-defined function has an additional requirement. It should be declared DETERMINISTIC when the same input always produces the same output:
CREATE OR REPLACE FUNCTION normalize_email (
p_email VARCHAR2
) RETURN VARCHAR2
DETERMINISTIC
IS
BEGIN
RETURN LOWER(TRIM(p_email));
END;
/A function that reads the current time, session settings, a table, or changing external data is not truly deterministic. Marking such a function DETERMINISTIC when it can return different values for the same input can make the index incorrect. The declaration tells Oracle that the function's result is stable. It does not make an unstable function safe.
Oracle may also need appropriate privileges and optimizer statistics. After creating or changing indexes, gather statistics according to your database's normal maintenance process. Without useful statistics, the optimizer may choose a full table scan even when the function-based index is valid.
Nulls and Expression Matching
A B-tree index normally does not store an entry when every indexed expression evaluates to NULL. Therefore, a function-based index on UPPER(email) does not help find rows where email IS NULL.
Session settings can also affect expressions involving character comparison, dates, or number conversion. Prefer explicit formats and stable expressions. Avoid relying on implicit conversions such as TO_DATE(order_text) without a format model.
Checking the Result
Use an execution plan to see whether Oracle selected the index:
EXPLAIN PLAN FOR
SELECT order_id
FROM orders
WHERE UPPER(email) = '[email protected]';
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);Look for an operation such as INDEX RANGE SCAN on ORDERS_EMAIL_UPPER_IX. The plan may still choose a full table scan if the table is small, the predicate returns many rows, or statistics show that the scan costs less. Index creation makes an access path available; it does not force Oracle to use it.
Exercise
An index exists on UPPER(email). Will WHERE email = '[email protected]' use that index for a case-insensitive lookup?
Show answer
No. The predicate does not apply UPPER to the column. Use WHERE UPPER(email) = '[email protected]', or use an appropriate virtual-column design.