Sub topic 12
Indexing a Virtual Column Instead
Prerequisites
This chapter assumes that you understand SELECT, WHERE, and CREATE INDEX, and that you know how Oracle chooses an execution plan. A virtual column is a table column whose value Oracle calculates from an expression instead of storing as a separate value.
A function-based index stores index entries for an expression directly:
CREATE INDEX orders_email_upper_ix ON orders (UPPER(email));A virtual-column design gives the expression a name first. You then index that 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);The virtual column is calculated when Oracle needs it. The indexed values are maintained as part of the index, so queries can search the expression efficiently.
Why use a virtual column?
A function-based index can be concise, but the expression is hidden inside the index definition. A virtual column makes the derived value visible in the table metadata. This can improve readability and make the expression easier to reuse in queries, constraints, or other database objects.
For example, applications can use the name email_uc rather than repeating UPPER(email):
SELECT order_id
FROM orders
WHERE email_uc = '[email protected]';The query uses the virtual column directly. Oracle can consider orders_email_uc_ix as an access path.
The following comparison summarizes the main difference:
| Design | Query expression | Main benefit |
|---|---|---|
| Function-based index | `UPPER(email)` | Keeps the table definition unchanged |
| Virtual column with index | `email_uc` | Names and exposes the derived value |
A virtual column does not automatically solve every expression-matching problem. Define the expression exactly as the application needs it. For example, UPPER(email) and UPPER(TRIM(email)) represent different values and require different indexing choices.
How the pieces fit
When a base column changes, Oracle recalculates the virtual-column value and updates the index entry. This adds maintenance work to inserts and updates, just as a function-based index does.
Restrictions and design checks
Before choosing this design, check the expression and the workload:
- The expression can reference columns from the same row, but it cannot depend on changing external state.
- Use deterministic logic. A value based on the current time, random output, or session settings can make an index unreliable.
- Check data types and lengths. The virtual column should represent the expression's result correctly.
- Add statistics and inspect the execution plan after creating the index.
- Remember that every index adds work to
INSERT,UPDATE, andDELETEoperations.
A virtual column is especially useful when several queries need the same derived value. If only one query needs a simple expression and exposing a new column would add unnecessary schema complexity, a function-based index may be the simpler choice.
Exercise
Exercise
Which query matches the virtual column and index defined with UPPER(email)?
Show answer
SELECT order_id FROM orders WHERE email_uc = '[email protected]';