Sub topic 11
Making queries match a function-based index
A function-based index stores the result of an expression instead of the raw column value. It only helps when the optimizer can see that your query uses the same expression. This chapter covers the matching rules, the requirements Oracle places on the expression, and the errors that cause an index to be ignored.
This chapter assumes you can write basic SQL, create tables and B-tree indexes in Oracle, and read an EXPLAIN PLAN output. It builds on the orders table and the UPPER(email) index from the previous chapter.
Why matching matters
A normal B-tree index on email stores the email values as they appear in the table. A query with WHERE UPPER(email) = '[email protected]' has to compute UPPER for each row before it can compare. The stored values are not in uppercase, so the index on email cannot answer the question, and Oracle falls back to a full table scan.
A function-based index fixes this by storing UPPER(email) for every row. The index entries are sorted by the computed value, so Oracle can do a range scan on them. The optimizer must still recognize that the expression in your WHERE clause is the same one that was indexed.
CREATE INDEX orders_email_upper_ix ON orders (UPPER(email));
SELECT order_id
FROM orders
WHERE UPPER(email) = '[email protected]';The expression in the query, UPPER(email), matches the indexed expression exactly. The optimizer can consider an index range scan.
EXPLAIN PLAN FOR
SELECT order_id
FROM orders
WHERE UPPER(email) = '[email protected]';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);In the plan, look for INDEX RANGE SCAN on ORDERS_EMAIL_UPPER_IX under a TABLE ACCESS BY INDEX ROWID BATCHED step. If you see TABLE ACCESS FULL instead, the index was not used. The cause is either a mismatch, covered below, or a cost decision. On a very small table, a full scan can legitimately be cheaper.
What counts as a match
Oracle compares the expression in your query with the expression stored in the index definition. Whitespace and letter case in the SQL text do not matter, because both sides are parsed. The expression itself must be equivalent.
| Query predicate | Index on `UPPER(email)` usable? | Reason |
|---|---|---|
| `WHERE UPPER(email) = '[email protected]'` | Yes | Same expression |
| `WHERE upper( email ) = '[email protected]'` | Yes | Same expression after parsing |
| `WHERE email = '[email protected]'` | No | The query uses the raw column |
| `WHERE LOWER(email) = '[email protected]'` | No | A different function |
| `WHERE UPPER(TRIM(email)) = '[email protected]'` | No | The expression is different from the indexed one |
| `WHERE UPPER(email) LIKE 'ANA%'` | Yes | A range scan on the indexed value is possible |
Two practical rules follow from this table:
- Write the query with the exact expression that you indexed.
- If several queries need slightly different expressions, create one index per expression, or change the queries to use a single standard expression.
Expressions on the other side of the comparison
The match applies to the column side of the predicate. If you write WHERE UPPER(email) = UPPER(:search_term), the left side matches the index. The right side is evaluated once per execution as a bind value expression. That pattern is common and works well.
Placing the function on the indexed column in a different arithmetic form can break the match. For a function-based index on order_id * 100, the predicate WHERE order_id * 100 = 4200 can use it. The predicate WHERE order_id = 42 cannot, because Oracle does not rewrite equality across the expression. Use the plain B-tree index on order_id for that case, which is the primary key index.
Requirements for the indexed expression
Oracle places conditions on the expression you can index.
The function must be deterministic
A deterministic function always returns the same result for the same input. UPPER, LOWER, TRUNC on a date, SUBSTR, and arithmetic are all deterministic. If a function could return a different value later, the stored index entry would be wrong. For that reason, Oracle rejects non-deterministic functions such as SYSDATE and DBMS_RANDOM.VALUE in an index expression.
For your own PL/SQL functions, you must declare them DETERMINISTIC before you can index them.
CREATE OR REPLACE FUNCTION email_domain (p_email IN VARCHAR2)
RETURN VARCHAR2
DETERMINISTIC
IS
BEGIN
RETURN LOWER(SUBSTR(p_email, INSTR(p_email, '@') + 1));
END;
/
CREATE INDEX orders_email_domain_ix ON orders (email_domain(email));Oracle trusts the DETERMINISTIC keyword. It does not check that the function body really is deterministic. If you declare a function deterministic and it reads from a table or a session setting, the index can become inconsistent with the data, and queries return wrong results. Only mark functions that depend on their arguments alone.
Session settings can affect the expression
Some functions depend on NLS (National Language Support) settings. For example, UPPER and NLS_UPPER can behave differently under different NLS_SORT or NLS_COMP settings. TO_CHAR on a date with no explicit format depends on NLS_DATE_FORMAT. If a session runs with different NLS settings from the ones used to build the index, the optimizer may not use the index, or in the worst case the results differ. Specify formats explicitly in indexed expressions.
CREATE INDEX orders_month_ix
ON orders (TO_CHAR(order_date, 'YYYY-MM'));
SELECT order_id
FROM orders
WHERE TO_CHAR(order_date, 'YYYY-MM') = '2026-03';Privileges and statistics
To create a function-based index on a user-defined function, the owner of the index needs the EXECUTE privilege on the function, granted directly and not through a role. The optimizer also needs statistics on the hidden virtual column that Oracle creates for the expression. Gather table statistics after creating the index, so the optimizer can estimate how many rows the predicate returns.
BEGIN
DBMS_STATS.GATHER_TABLE_STATS(
ownname => USER,
tabname => 'ORDERS'
);
END;
/Common reasons an index is ignored
When a query that should use a function-based index shows a full table scan, check these items in order.
- The expression differs. Compare the query text with the index definition. Look for added functions, a different function, or a different format string.
- The column is wrapped by another function.
UPPER(TRIM(email))does not matchUPPER(email). - Implicit data type conversion. If you compare an indexed
VARCHAR2expression with a number, Oracle converts the column side. The expression no longer matches. - Stale or missing statistics. Without current statistics, the optimizer can wrongly estimate that a full scan is cheaper.
- The index is invisible. An invisible index is maintained but ignored unless the session sets
OPTIMIZER_USE_INVISIBLE_INDEXEStoTRUE. - The predicate is not selective. If most rows match, a full scan is the better plan.
You can see the index definition and the stored expression in the data dictionary.
SELECT index_name, column_expression
FROM user_ind_expressions
WHERE table_name = 'ORDERS';The COLUMN_EXPRESSION column shows the text Oracle stored for each function-based index. Compare it with your query.
Nulls and function-based indexes
A B-tree index does not store an entry when every indexed column is NULL. With a function-based index, the expression result is what counts. UPPER(NULL) is NULL, so rows with a null email have no entry in orders_email_upper_ix. A query that searches for UPPER(email) = 'X' never needs those rows, so this is fine. A query such as WHERE UPPER(email) IS NULL cannot use the index to find them.
Diagram: how the optimizer decides
Summary of working rules
- Index the exact expression the queries use, and write the queries with that same expression.
- Declare custom functions
DETERMINISTIConly when they truly are, and rebuild the index after changing them. - Use explicit formats in expressions that involve dates or NLS-dependent behavior.
- Gather statistics after creating the index.
- Check
USER_IND_EXPRESSIONSand the execution plan when an index is not used.
Exercise
An index exists as CREATE INDEX ordersemailupperix ON orders (UPPER(email)). A developer runs SELECT orderid FROM orders WHERE LOWER(email) = '[email protected]' and sees TABLE ACCESS FULL in the plan. Why, and what are two ways to fix it?
Show answer
LOWER(email) is a different expression from UPPER(email), so the optimizer cannot use the index. Fix it by rewriting the query to use UPPER(email) = '[email protected]', or by creating a second function-based index on LOWER(email) if queries in that form must be kept.
Exercise
You create a PL/SQL function normalize_name that reads a lookup table, and you declare it DETERMINISTIC so that you can index it. Why is this dangerous?
Show answer
Oracle trusts the DETERMINISTIC keyword and does not check the function body. If the lookup table changes, the function returns a different result for the same input, but the index still holds the old result. Queries that use the index can then return different rows from a full table scan.