Sub topic 5
Function-based indexes for expressions you cannot remove
The previous chapter showed how to move a function off an indexed column so a plain B-tree index can be used. Some expressions cannot be removed. Case-insensitive search needs UPPER, and matching on a calendar month needs TO_CHAR. For these, you index the expression instead of the column.
This chapter assumes you can create ordinary indexes, read a DBMS_XPLAN plan, and know why WHERE UPPER(last_name) = 'SMITH' cannot use a plain index on last_name.
What a function-based index stores
A function-based index is a B-tree index whose key is the result of an expression computed from one or more columns. Oracle evaluates the expression when a row is inserted or updated and stores the result in the index, sorted like any other key. A predicate that uses the same expression can then be searched by a range scan on those stored results.
CREATE INDEX customers_upper_last_name_ix
ON customers (UPPER(last_name));
EXPLAIN PLAN FOR
SELECT customer_id, last_name
FROM customers
WHERE UPPER(last_name) = 'SMITH';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);In the plan, look for an INDEX RANGE SCAN on CUSTOMERS_UPPER_LAST_NAME_IX. The predicate section should list the expression as an access predicate:
access(UPPER("LAST_NAME")='SMITH')The index does not contain last_name itself, so Oracle still visits the table to fetch customer_id and last_name. Only the search step is faster.
Behind the scenes, Oracle adds a hidden virtual column to the table for the expression. It has a system-generated name such as SYS_NC00005$. This is why the optimizer can keep statistics on the expression, and why the index appears in the plan even though the column name does not.
The expression must match
The optimizer matches the expression in your query against the indexed expression. The match is on the expression, not on what it means. Different spelling or different functions mean no match.
-- Matches the index above
WHERE UPPER(last_name) = 'SMITH'
-- Does not match: LOWER is a different expression
WHERE LOWER(last_name) = 'smith'
-- Does not match: the expression is applied to a different column
WHERE UPPER(first_name) = 'SMITH'Oracle ignores differences in whitespace and letter case in the SQL text, so upper( last_name ) still matches. It does not recognize that LOWER(x) = 'smith' is logically equivalent to UPPER(x) = 'SMITH'.
Matching also applies to the literal side. If you compare UPPER(last_name) to a bind variable, pass the value already in upper case, or wrap it as UPPER(:name). The index key is upper case, so a mixed-case search value will find nothing.
Common uses
Dates and truncation
If many queries ask for all orders on one day and you cannot rewrite them as ranges, index the truncated value.
CREATE INDEX orders_trunc_date_ix
ON orders (TRUNC(order_date));
SELECT order_id
FROM orders
WHERE TRUNC(order_date) = DATE '2026-01-15';Prefer the half-open range from the previous chapter when you control the query. It uses the existing index on order_date and needs no extra index to maintain. Use the function-based index only when the TRUNC form is fixed, for example in generated SQL or reporting tools you cannot change.
Formatted text
Always give TO_CHAR an explicit format when you index it.
CREATE INDEX orders_month_ix
ON orders (TO_CHAR(order_date, 'YYYY-MM'));TO_CHAR(order_date) with no format depends on the session's NLS_DATE_FORMAT, so its result is not fixed for a given input. Oracle will reject such an expression in an index. Even with a format, month and day names depend on the language, so use numeric formats in indexed expressions.
Conditional ("partial") indexes with CASE
Oracle does not have a WHERE clause on CREATE INDEX, but CASE gives a similar effect. Because a B-tree index omits entries whose keys are all NULL, an expression that returns NULL for uninteresting rows keeps them out of the index.
CREATE INDEX orders_open_cust_ix
ON orders (CASE WHEN status = 'OPEN' THEN customer_id END);
SELECT order_id
FROM orders
WHERE CASE WHEN status = 'OPEN' THEN customer_id END = 42;If only 1% of orders are open, this index is about 1% the size of an index on customer_id. It is cheap to maintain and fast to search. The query must repeat the full CASE expression, so teams often hide it behind a view or a virtual column.
Your own functions
A function you write can be indexed if you declare it DETERMINISTIC, which tells Oracle it always returns the same result for the same input.
CREATE OR REPLACE FUNCTION digits_only (p_text IN VARCHAR2)
RETURN VARCHAR2 DETERMINISTIC
IS
BEGIN
RETURN REGEXP_REPLACE(p_text, '[^0-9]', '');
END digits_only;
/
CREATE INDEX customers_phone_digits_ix
ON customers (digits_only(phone_number));Oracle trusts the DETERMINISTIC keyword and does not check it. If the function reads a table, a package variable, or the clock, the stored keys can become wrong and queries will return wrong rows without any error. If you change the function body, the existing index entries are stale. Drop and recreate the index after changing the function.
What is not allowed
An indexed expression must be repeatable from the row's own columns. These are rejected:
- Functions that depend on time or session state, such as
SYSDATE,SYS_CONTEXT, orUSER. - Subqueries and aggregate functions.
- Non-deterministic user functions.
Expressions that reference columns of one row only are allowed, including arithmetic such as salary * 12 and concatenation such as first_name || ' ' || last_name.
Virtual columns as an alternative
A virtual column gives the expression a visible name in the table definition.
ALTER TABLE customers ADD (
last_name_uc VARCHAR2(100)
GENERATED ALWAYS AS (UPPER(last_name)) VIRTUAL
);
CREATE INDEX customers_last_name_uc_ix
ON customers (last_name_uc);Queries can filter on last_name_uc directly. The table definition documents the expression, and you can attach statistics and constraints to the column. Queries written with UPPER(last_name) can also be matched to it. Check the plan to confirm, because the exact matching behavior depends on the Oracle version. Use the plain function-based index when you want no change to the table's visible structure. Use a virtual column when several indexes, constraints, or queries share the same expression.
Costs and statistics
A function-based index has the same write cost as other indexes, plus the cost of evaluating the expression for each insert and each update to the referenced columns. An expensive expression, such as a regular expression, makes bulk loads slower.
After creating the index, gather table statistics so the optimizer has data on the hidden expression column.
BEGIN
DBMS_STATS.GATHER_TABLE_STATS(
ownname => USER,
tabname => 'CUSTOMERS'
);
END;
/Without statistics on the expression, the optimizer may estimate its selectivity badly and choose a full table scan, even when the index is correct.
Common errors
| Symptom | Likely cause |
|---|---|
| Index exists but plan shows a full table scan | The query expression differs from the indexed one, or statistics are missing |
| `ORA-01743: only pure functions can be indexed` | The function is not deterministic, or uses session state such as `SYSDATE` |
| Search returns no rows although data exists | The comparison value is not in the same form as the key, for example mixed case against an `UPPER` index |
| Wrong results after changing a user function | Existing index entries were built with the old function body |
Exercise
Your application runs WHERE TO_CHAR(order_date, 'YYYY-MM') = '2026-03' on a large orders table, and you cannot change the SQL. Write the index that lets Oracle use an index range scan, and name one way to confirm it is used.
Show answer
CREATE INDEX ordersmonthix ON orders (TOCHAR(orderdate, 'YYYY-MM')); The expression and format must match the query exactly. Gather table statistics, then run EXPLAIN PLAN FOR the query and display it with DBMSXPLAN.DISPLAY. The plan should show an INDEX RANGE SCAN on ORDERSMONTHIX with an access predicate on TOCHAR("ORDER_DATE",'YYYY-MM').
Exercise
You created an index on UPPER(lastname). A developer's query uses WHERE LOWER(lastname) = 'smith' and does a full table scan. Give two ways to fix it.
Show answer
Change the query to WHERE UPPER(lastname) = 'SMITH' so it matches the indexed expression. Or create a second function-based index on LOWER(lastname), which costs extra maintenance on every write. The first option is usually better.