Sub topic 10
Indexing expressions with function-based indexes
This chapter assumes you can create a B-tree index, read a basic EXPLAIN PLAN output, and know that the optimizer picks between an index access path and a full table scan. It uses the orders table from the earlier chapters.
The problem: a function hides the column
Suppose users search orders by email without caring about case. You might write this query:
SELECT order_id
FROM orders
WHERE UPPER(email) = '[email protected]';If email has an ordinary B-tree index, that index stores the raw values, such as [email protected] or [email protected]. The index entries are sorted by those raw values, not by their uppercase forms. The optimizer cannot walk the tree to find [email protected], because the uppercase values are not in the tree. It usually falls back to a full table scan and evaluates UPPER(email) on every row.
The same thing happens with any expression on an indexed column, such as TRUNC(order_date), SUBSTR(...), or col + 0. Wrapping the column in a function removes the ability to use a plain index on that column.
What a function-based index is
A function-based index (FBI) is a B-tree index whose keys are the results of an expression, not the raw column values. Oracle evaluates the expression for each row when the row is inserted or updated, and stores the result in the index, sorted like any other key.
CREATE INDEX orders_email_upper_ix ON orders (UPPER(email));
SELECT order_id
FROM orders
WHERE UPPER(email) = '[email protected]';Now the leaf entries hold UPPER(email) values and the rowid of each row. The query above becomes an ordinary equality search on the index, followed by a table access by rowid. Every B-tree property you already know still applies: the tree is balanced, the keys are sorted, range scans work, and a UNIQUE FBI is allowed.
You can also use an expression as one column of a composite index:
CREATE INDEX orders_cust_day_ix
ON orders (customer_id, TRUNC(order_date));The column-order rules from the composite index chapter apply here too. A query that filters on customer_id alone can use the leading column. A query that filters only on TRUNC(order_date) cannot use it as efficiently.
How the optimizer decides to use it
Oracle stores the expression in the data dictionary. It treats it as a hidden, system-generated virtual column on the table. When it parses a query, it compares the expressions in the query with the expressions of the available indexes. If they match, the index becomes a candidate access path. The optimizer still chooses by cost, so a small table or an unselective predicate can still produce a full table scan.
You can confirm the choice with the plan:
EXPLAIN PLAN FOR
SELECT order_id
FROM orders
WHERE UPPER(email) = '[email protected]';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);When the index is used, you should see an INDEX RANGE SCAN on ORDERS_EMAIL_UPPER_IX, usually followed by TABLE ACCESS BY INDEX ROWID (or the BATCHED variant). The predicate section shows access(UPPER("EMAIL")='[email protected]'). If you see TABLE ACCESS FULL with a filter(UPPER("EMAIL")=...) line instead, the index was not chosen.
Making queries match the index
The query expression must be equivalent to the indexed expression. Case and extra spaces in the SQL text do not matter. The logic does.
| Indexed expression | Query predicate | Matches? |
|---|---|---|
| `UPPER(email)` | `UPPER(email) = '[email protected]'` | Yes |
| `UPPER(email)` | `upper( email ) = '[email protected]'` | Yes |
| `UPPER(email)` | `LOWER(email) = '[email protected]'` | No |
| `UPPER(email)` | `UPPER(TRIM(email)) = '[email protected]'` | No |
| `TRUNC(order_date)` | `TRUNC(order_date) = DATE '2026-03-01'` | Yes |
| `TRUNC(order_date)` | `order_date = DATE '2026-03-01'` | No |
Some practical rules follow from this:
- Agree on one canonical expression for a lookup and use it everywhere. Two teams writing
UPPER(email)andLOWER(email)need two indexes, or one convention. - Watch for implicit conversions. If the expression is
UPPER(email)and you compare it to a number or a differently typed bind, the comparison may be rewritten and the index may not apply. - An FBI helps only when the expression appears in a
WHERE,JOIN, orORDER BYcondition the optimizer can satisfy from the index.
Requirements and restrictions
The expression has to produce the same result every time for the same input, because Oracle stores the result and does not recompute it when you query. Oracle enforces this in several ways:
- Built-in functions such as
UPPER,LOWER,TRUNC,SUBSTR, andTO_CHARwith explicit formats are allowed. - You cannot index non-deterministic expressions such as
SYSDATEorDBMS_RANDOM.VALUE. - You cannot use aggregate functions.
- A user-defined function must be declared
DETERMINISTIC. - The owner of the index needs
EXECUTEprivilege on any function used.
A user-defined example:
CREATE OR REPLACE FUNCTION norm_email (p_email IN VARCHAR2)
RETURN VARCHAR2 DETERMINISTIC
IS
BEGIN
RETURN LOWER(TRIM(p_email));
END;
/
CREATE INDEX orders_norm_email_ix ON orders (norm_email(email));
SELECT order_id
FROM orders
WHERE norm_email(email) = '[email protected]';Other points worth knowing:
- NULLs. A B-tree entry is not stored when every indexed expression is NULL.
UPPER(NULL)is NULL, so rows with a NULLemailare not inorders_email_upper_ix. A query such asWHERE UPPER(email) IS NULLcannot use that index. - DML cost. Every insert, delete, and update of the base column evaluates the expression and maintains the index. An expensive function slows writes.
- Statistics. Gather table statistics after you create the index, so the optimizer has statistics for the hidden expression column. Without them its estimates can be poor.
- Privileges and settings. In current releases the optimizer uses an FBI by default. Older advice about enabling query rewrite settings no longer applies to most systems.
The virtual column alternative
You can define the expression once as a virtual column and index that column. A virtual column is a table column whose value is computed from other columns and not stored in the table's rows.
ALTER TABLE orders ADD (
email_uc VARCHAR2(100) GENERATED ALWAYS AS (UPPER(email)) VIRTUAL
);
CREATE INDEX orders_email_uc_ix ON orders (email_uc);Queries can then use either WHERE email_uc = '[email protected]' or the original WHERE UPPER(email) = .... Oracle can usually match the expression to the virtual column. The index entries are the same as with an FBI. The differences are in design:
| Aspect | Function-based index | Virtual column plus index |
|---|---|---|
| Expression visible in table definition | No, only in index metadata | Yes, as a named column |
| Changes the table | No | Yes, adds a column |
| Reuse across several indexes | Repeat the expression | Reference the column |
| Column statistics and constraints | Hidden column only | Normal column; can have constraints and partitioning |
| Query readability | Expression repeated in each query | Can query by column name |
Choose the virtual column when the expression is a stable business concept that many queries and several indexes will share. Choose a plain FBI for a one-off lookup where you do not want to change the table.
Checking and maintaining the index
After creating an FBI, check the data dictionary, then run the plan:
SELECT index_name, index_type, status
FROM user_indexes
WHERE table_name = 'ORDERS';
SELECT index_name, column_expression
FROM user_ind_expressions
WHERE table_name = 'ORDERS';INDEX_TYPE shows FUNCTION-BASED NORMAL for an FBI, and USER_IND_EXPRESSIONS shows the stored expression text. An index with status UNUSABLE is not used until you rebuild it. If you suspect an FBI is not helping, make it invisible as described in the maintenance chapter, run the query, and compare plans.
Exercise
A table has a plain index on last_name and a function-based index on UPPER(last_name). Which of these predicates can use an index: (a) last_name = 'Smith', (b) UPPER(last_name) = 'SMITH', (c) LOWER(last_name) = 'smith', (d) UPPER(last_name) IS NULL? Name the index for each, or say none.
Show answer
(a) can use the plain index on last_name. (b) can use the function-based index. (c) cannot use either index, because LOWER(last_name) does not match UPPER(last_name) and the plain index holds raw values, so it needs a full scan or a new index on LOWER(last_name). (d) cannot use the function-based index, because rows whose indexed expression is NULL are not stored in a B-tree index, so the optimizer must scan the table.
Exercise
You create CREATE INDEX orders_day_ix ON orders (TRUNC(order_date)); but the query SELECT order_id FROM orders WHERE order_date = DATE '2026-03-01'; still shows a full table scan. Why, and what are two ways to fix it?
Show answer
The predicate uses the raw order_date, not TRUNC(order_date), so it does not match the indexed expression. One fix is to change the query to WHERE TRUNC(order_date) = DATE '2026-03-01'. The other is to rewrite it as a range, order_date >= DATE '2026-03-01' AND order_date < DATE '2026-03-02', and use a plain index on order_date.