Sub topic 4
Keeping Indexed Columns Free of Functions and Expressions
Prerequisites: you should know basic SELECT and WHERE syntax, Oracle DATE values, and the general purpose of an index.
A predicate is the condition in a WHERE clause. A sargable predicate is written so that Oracle can search an index using the column's stored values. The usual shape is:
column operator valueFor example, order_date >= DATE '2026-01-15' lets Oracle find the first matching index entry and scan forward. When you wrap the indexed column in a function or calculate with it, Oracle may need to evaluate the expression for many rows before it knows whether each row matches.
Move date arithmetic to the constant side
Suppose orders.order_date has a normal B-tree index. This predicate applies TRUNC to the column:
SELECT order_id, order_date
FROM orders
WHERE TRUNC(order_date) = DATE '2026-01-15';TRUNC removes the time portion. However, the index stores the original order_date values, not their truncated values. Oracle may therefore scan many index entries or read the table and evaluate TRUNC row by row.
Rewrite the condition as a half-open range. A half-open range includes its lower boundary and excludes its upper boundary:
SELECT order_id, order_date
FROM orders
WHERE order_date >= DATE '2026-01-15'
AND order_date < DATE '2026-01-16';This includes every time on January 15, including midnight, but excludes midnight at the start of January 16. The column remains bare, so the predicate can become an index range scan.
Rewrite other expressions
The same idea applies beyond dates. Move calculations away from the indexed column when algebra allows it.
| Less index-friendly predicate | Possible rewrite |
|---|---|
| `salary + 1000 > 50000` | `salary > 49000` |
| `order_date - 7 >= DATE '2026-02-01'` | `order_date >= DATE '2026-02-08'` |
| `quantity * 2 = 20` | `quantity = 10` |
Check the rewrite carefully when NULL, rounding, negative values, or data types affect the result. A rewrite must preserve the original meaning, not merely make the text look simpler.
A function on a column is not always wrong. If the expression is required by the business rule and cannot be rewritten, use a function-based index, which is covered in the next chapter. First try to express the condition using the original column.
Exercise
Rewrite this predicate so that an index on eventtime can search a single calendar day: WHERE TRUNC(eventtime) = DATE '2026-03-10'
Show answer
WHERE eventtime >= DATE '2026-03-10' AND eventtime < DATE '2026-03-11'