Sub topic 4
Keeping indexed columns free of functions and expressions
This chapter assumes you can read a basic execution plan and know that a B-tree index stores column values in sorted order. It also assumes you know the difference between an index range scan and a full table scan. Earlier chapters covered both.
Why a function on a column blocks the index
A B-tree index is sorted by the stored column value. Oracle can descend the tree to a start point, read entries in order, and stop at an end point. That only works if the search condition is expressed in terms of the stored value itself.
A predicate that follows this rule is called sargable (it can use a Search ARGument). When you wrap the column in a function or an expression, Oracle sees the result of the computation, not the stored value. The index is sorted by order_date, not by TRUNC(order_date). Oracle cannot know where in the tree the matching rows lie without computing the function for every row.
The practical rule is to keep the indexed column bare on one side of the comparison. Move all computation to the other side, where it is evaluated once.
Dates: the most common case
The first example comes from the spine. Both queries return the orders placed on one day, but only the second can use a plain index on order_date.
-- Non-sargable: the function is applied to the column, so an index on order_date cannot be used for a range scan
SELECT order_id, order_date
FROM orders
WHERE TRUNC(order_date) = DATE '2026-01-15';
-- Index-friendly: compare the bare column to a half-open range
SELECT order_id, order_date
FROM orders
WHERE order_date >= DATE '2026-01-15'
AND order_date < DATE '2026-01-16';An Oracle DATE always contains a time of day. TRUNC removes it, which is why the first query looks natural. The rewrite reaches the same result by covering the whole day: from midnight inclusive to the next midnight exclusive.
Use half-open ranges, not BETWEEN
Avoid BETWEEN DATE '2026-01-15' AND DATE '2026-01-15' + 1. BETWEEN includes both ends, so it also returns rows stamped exactly at midnight on the 16th. The common workaround, + 1 - 1/86400, silently drops rows with fractional seconds on TIMESTAMP columns. A >= lower bound with a < upper bound is correct for both types and chains cleanly: the end of one range is the start of the next.
Other date functions follow the same pattern
| Non-sargable | Index-friendly equivalent |
|---|---|
| `TO_CHAR(order_date, 'YYYY') = '2026'` | `order_date >= DATE '2026-01-01' AND order_date < DATE '2027-01-01'` |
| `EXTRACT(YEAR FROM order_date) = 2026` | the same year range |
| `TRUNC(order_date, 'MM') = DATE '2026-03-01'` | `order_date >= DATE '2026-03-01' AND order_date < DATE '2026-04-01'` |
| `order_date + 7 > SYSDATE` | `order_date > SYSDATE - 7` |
The last row shows the general technique. If the column appears in an expression, use algebra to isolate it.
Arithmetic and other expressions
Arithmetic on the column has the same effect as a function call.
-- Non-sargable: the expression must be computed for every row
SELECT employee_id
FROM employees
WHERE salary * 12 > 60000;
-- Sargable: the arithmetic moves to the constant
SELECT employee_id
FROM employees
WHERE salary > 60000 / 12;Oracle evaluates 60000 / 12 once before the scan starts. The same applies to values that come from bind variables: WHERE salary > :annual / 12 is fine, because the expression contains no column.
A few more patterns you will meet in production code:
SUBSTR(col, 1, n) = 'ABC'can often becomecol LIKE 'ABC%'. The next chapters coverLIKEin detail. Note the two are equal only if the pattern contains no%or_characters.NVL(col, 0) = 0hides the column. Rewrite it ascol = 0 OR col IS NULL. TheORhas its own index considerations, covered in a later chapter, and NULL handling in B-tree indexes also needs care.col1 || col2 = 'AB'combines two columns. Compare each column separately:col1 = 'A' AND col2 = 'B'. This only works if the concatenated values split unambiguously.UPPER(col) = :nameneeds a different fix. You cannot always remove the function without changing the meaning, which is where function-based indexes come in.
Functions on the constant side are fine
The restriction applies only to the column. These queries are all sargable:
SELECT order_id
FROM orders
WHERE order_date >= TRUNC(SYSDATE) - 30;
SELECT customer_id
FROM customers
WHERE last_name = INITCAP(:name);Oracle evaluates TRUNC(SYSDATE) - 30 and INITCAP(:name) once per execution and then uses the result as an ordinary search value. If a function depends only on constants, literals, bind variables, or deterministic built-ins, it does not block the index.
Confirming the effect in a plan
Do not rely on rules of thumb alone. Check the plan.
EXPLAIN PLAN FOR
SELECT order_id, order_date
FROM orders
WHERE TRUNC(order_date) = DATE '2026-01-15';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);With only a plain index on order_date, you should expect a TABLE ACCESS FULL step and a filter("TRUNC(INTERNAL_FUNCTION("ORDER_DATE"))=...) line in the Predicate Information section. After the rewrite, you should expect INDEX RANGE SCAN with access("ORDER_DATE">=... AND "ORDER_DATE"<...). The exact plan depends on statistics and the optimizer's cost estimates. A small table or an unselective range can still produce a full scan, even for a sargable predicate. The earlier chapter on selectivity explains why.
Edge cases and gotchas
- Hidden functions from views and ORM layers. A query that looks clean may reach the database as
TRUNC(col)because a view definition or a framework applied it. Always read the predicate section of the plan, not just the source query. - Implicit conversions are functions too. If Oracle converts the column to match a literal's type, it adds a function call around the column. The next chapters cover that case separately.
- Session settings. Comparing a
DATEcolumn to a string such as'15-JAN-26'forces a conversion that depends onNLS_DATE_FORMAT. UseDATE '2026-01-15'orTO_DATEwith an explicit format mask, applied to the constant. - Deliberately blocking an index. Writing
col + 0 = 5orcol || '' = 'A'is an old tuning trick to stop the optimizer using an index oncol. It works because of the rule in this chapter. Prefer a hint or fixing statistics, since this trick hides intent from later readers.
When rewriting is not possible
Some predicates cannot be moved to the constants. Case-insensitive searches and computed business keys are common examples. For those, you create an index on the expression itself. The next chapter shows how, and why the predicate must then match the indexed expression exactly.
Exercise
The query SELECT order_id FROM orders WHERE EXTRACT(YEAR FROM order_date) = 2026 AND customer_id = 42 runs slowly. An index exists on orders (order_date). Rewrite the date predicate so the index on order_date can be used for a range scan. Why is BETWEEN DATE '2026-01-01' AND DATE '2026-12-31' a poor choice?
Show answer
Use order_date >= DATE '2026-01-01' AND order_date < DATE '2027-01-01'. The column is bare, so Oracle can descend the index to 1 January 2026 and stop at 1 January 2027. The BETWEEN version is poor because a DATE includes a time of day. DATE '2026-12-31' means midnight at the start of that day, so any order placed later on 31 December is excluded. The half-open range includes the whole year.
Exercise
Which of these predicates are sargable for a plain index on salary? (a) salary > 60000 / 12 (b) salary * 12 > 60000 (c) salary + 0 = 5000 (d) salary = ROUND(:bonus * 1.1)
Show answer
(a) and (d). In both, the column is bare and the computation involves only constants and a bind variable, so Oracle evaluates it once. (b) and (c) apply arithmetic to the column, so Oracle cannot use the index's sort order to locate matching entries.