Transformed index columns and implicit conversion
A query that is slow despite an index. Transforming the column widens the index search range or prevents index use entirely.
SQL
Situation
Two queries used by the order lookup screen are reported as slow. Both are slow even though the condition columns have indexes. This is the most common case in practice.
Tables
ORDERS (2 million rows) holds orders accumulated at 2,000 per day since 2023-01-01.
| Column | Type | Description |
|---|---|---|
ORD_ID | NUMBER | Order ID (PK) |
ORD_NO | VARCHAR2 | Order number. A string made only of digits (YYYYMMDD + an 8-digit sequence number) |
ORD_DT | DATE | Order date and time |
STATUS | VARCHAR2 | DONE 90%, PAID 7%, CANCELED 3% |
| Index | Columns |
|---|---|
ORDERS_X2 | STATUS, ORD_DT, ORD_ID |
ORDERS_X3 | ORD_NO |
① One day of paid orders
Retrieve the paid orders of January 15, 2025. The result is about 140 rows.
BEFORE · Transformed date
SELECT *
FROM orders
WHERE status = 'PAID'
AND TO_CHAR(ord_dt, 'YYYYMMDD') = '20250115'
② Look up one row by order number
Find an order by a single order number. The result is one row.
BEFORE · Numeric literal
SELECT *
FROM orders
WHERE ord_no = 2025011501490001
Run both queries and check Buffers and Predicate Information. See how many blocks are read compared with the number of result rows.
Approach
1. Look at "how" the index is used, not whether it is used
An INDEX RANGE SCAN in the execution plan is not the end of the story. In Predicate Information, check whether a condition is in access (sets the index search range) or in filter (discards rows after reading them). If the result is 140 rows but Buffers is in the hundreds or thousands, the search range is wide.
2. Find whether the column side of the WHERE clause is transformed
An index is sorted in the order of the column's original values. Wrapping the column in a function, as in TO_CHAR(ord_dt, …), produces values whose order differs from the index order, so that condition cannot narrow the search range.
3. Suspect invisible transformations (implicit conversion)
Even when the SQL has no function, Oracle sometimes adds one silently. Comparing values of different types triggers a conversion, and when a string is compared with a number, the string side is converted to a number. If the column is a string type, the column is transformed. Predicate Information shows it as TO_NUMBER("ORD_NO").
4. Move the transformation to the value side
Look for a way to produce the same result while leaving the column as is and changing the constant or bind side.
- Whether the day can be expressed as a range condition instead of converting the date to a string for comparison
- Whether a string literal matching the column type can be used instead of a numeric literal
The table structure stays unchanged. Creating a function-based index (
TO_CHAR(ord_dt, 'YYYYMMDD')) is another option, but if the query can be fixed, fixing the query comes first.
Explanation
① Transformed date → range condition
AFTER · Range condition
SELECT *
FROM orders
WHERE status = 'PAID'
AND ord_dt >= DATE '2025-01-15'
AND ord_dt < DATE '2025-01-16'
Why it got faster
- Before's Predicate Information is
access("STATUS"='PAID')andfilter(TO_CHAR(INTERNAL_FUNCTION("ORD_DT"),'YYYYMMDD')='20250115'). The index search range is all ofSTATUS = 'PAID'(140,000 rows), and the date is filtered one row at a time within it. - In After, the
ORD_DTcondition also moves up to access. BecauseORDERS_X2is sorted in(STATUS, ORD_DT)order, only the segment that isPAIDand on that day, about 140 rows, is read. - Both execution plans have the same shape (
INDEX RANGE SCAN). The difference shows only in Buffers and Predicate Information.
If a column is in the index, Oracle filters on it at the index step even when it is transformed (without going to the table). So the loss comes from "how widely the index is read". The wider the range of the leading column
STATUS, the bigger the loss.
② Numeric literal → string literal
AFTER · String literal
SELECT *
FROM orders
WHERE ord_no = '2025011501490001'
Why it got faster
- Before shows
filter(TO_NUMBER("ORD_NO")=2025011501490001)and usesTABLE ACCESS FULL. It converts all 2 million rows to numbers and compares them. - In After the types match, so there is no conversion, and
INDEX RANGE SCAN (ORDERS_X3)reads only a few blocks.
Why this is optimal
- Both Afters read the index only as much as the number of result rows. The search range now equals the result, so the index leaves no room for further reduction.
- The remaining cost is visiting the table once per result row (
TABLE ACCESS BY INDEX ROWID). It is unavoidable because ofSELECT *. Selecting only the needed columns, all of which are in the index, would remove even the table access, but that changes the query's requirements.
Key points
- When reading an execution plan, look at the Predicate Information section before the operation names. Even with the same
INDEX RANGE SCAN, the amount read differs depending on whether a condition is in access or filter. - When Oracle compares a string with a number, it converts the string side to a number. If the column is a string type, the column is transformed.
- Other patterns with the same trap:
SUBSTR(col, 1, 4) = '2025',col || '' = :v,NVL(col, 'X') = :v,col + 0 = :v.