Sub topic 6
Implicit conversions that disable an index
This chapter assumes you can read a basic execution plan (see the earlier chapter on EXPLAIN PLAN and DBMS_XPLAN) and know the Oracle data types NUMBER, VARCHAR2, DATE and TIMESTAMP. It also assumes you know that a B-tree index stores column values in sorted order.
What an implicit conversion is
An implicit conversion happens when you compare two values of different data types and Oracle changes one of them to match the other. You write no conversion function, so the rewrite is easy to miss. The problem for indexing is which side Oracle changes. If it changes the indexed column, the predicate effectively has a function on the column. The sargable-predicate chapter showed that a function on a column blocks a normal index.
The index on phone_number stores the text values '5551234', '5551234 ', '05551234' and so on, sorted as text. After a conversion to a number, those strings no longer sort the same way, so Oracle cannot use the index order to find the matching entries.
VARCHAR2 column compared to a number
Oracle's conversion rule for character versus number is that the character value becomes a number. When you compare a VARCHAR2 column to a numeric literal, the column is the one converted.
Here is a complete example you can run.
CREATE TABLE contacts (
contact_id NUMBER PRIMARY KEY,
phone_number VARCHAR2(20),
note VARCHAR2(100)
);
INSERT INTO contacts
SELECT level, TO_CHAR(5550000 + level), 'row ' || level
FROM dual
CONNECT BY level <= 100000;
COMMIT;
CREATE INDEX contacts_phone_ix ON contacts (phone_number);
EXEC DBMS_STATS.GATHER_TABLE_STATS(USER, 'CONTACTS');
EXPLAIN PLAN FOR
SELECT contact_id
FROM contacts
WHERE phone_number = 5551234;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);The plan shows a full scan of CONTACTS. In the "Predicate Information" section you see a filter like this:
1 - filter(TO_NUMBER("PHONE_NUMBER")=5551234)That TO_NUMBER("PHONE_NUMBER") is the conversion Oracle added. Now compare the version with a matching literal.
EXPLAIN PLAN FOR
SELECT contact_id
FROM contacts
WHERE phone_number = '5551234';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);This time the plan uses INDEX RANGE SCAN on CONTACTS_PHONE_IX, and the predicate section lists access("PHONE_NUMBER"='5551234'). The literal already has the column's type, so no conversion is needed.
A second risk: ORA-01722
The numeric version of the query can also fail, not just run slowly. Because Oracle applies TO_NUMBER to the column, it must convert every value it examines. If any row holds a value such as '555-1234', the query raises ORA-01722: invalid number. Whether it fails depends on the plan and the data, so the error can appear after an unrelated change, such as new statistics or a new row. That makes it a difficult bug to trace.
Number column compared to a string
The reverse case is usually harmless. If contact_id is a NUMBER column and you write the following, Oracle converts the string constant, not the column.
SELECT note FROM contacts WHERE contact_id = '42';The primary key index can still be used. This is still poor style. It depends on the string being a valid number and on the conversion rules, so write contact_id = 42.
DATE compared to TIMESTAMP
A second common case involves datetime types. If you compare a DATE column to a TIMESTAMP value, Oracle converts the DATE to a TIMESTAMP. The conversion applies to the column, so an index on the DATE column cannot be used for a range scan.
This often happens by accident. SYSTIMESTAMP, CURRENT_TIMESTAMP, a TIMESTAMP literal, and bind variables that a client driver sends as timestamps are all timestamp values.
-- order_date is DATE. SYSTIMESTAMP is a TIMESTAMP WITH TIME ZONE,
-- so the column is converted and an index on order_date is not usable.
SELECT order_id
FROM orders
WHERE order_date >= SYSTIMESTAMP - INTERVAL '1' DAY;
-- SYSDATE is a DATE, so the column is left alone.
SELECT order_id
FROM orders
WHERE order_date >= SYSDATE - 1;In a plan, the first query typically shows a predicate such as INTERNAL_FUNCTION("ORDER_DATE"). The name INTERNAL_FUNCTION is Oracle's label for a conversion it added itself. Whenever you see it applied to a column in the predicate section, look for a type mismatch.
Joins and bind variables
The same rules apply when the two values are columns, as in a join. If orders.customer_ref is a VARCHAR2 and customers.customer_id is a NUMBER, the join condition converts the VARCHAR2 column with TO_NUMBER. That blocks any index on customer_ref. It also hides the data model problem: two columns that mean the same thing have different types.
Bind variables add a further source. Application code declares the type of each bind value. If a Java or .NET driver binds a number to a VARCHAR2 column, or a timestamp to a DATE column, you get the same conversion. The SQL text looks fine, so you must check the bind type in the application and in the plan.
Diagnosing and fixing
The diagnosis rests on the predicate section of the plan, as the following steps show.
- Get the plan with
DBMS_XPLAN.DISPLAY(orDISPLAY_CURSORfor a statement that has run). - Look in "Predicate Information" for
TO_NUMBER,TO_CHAR,TO_DATE,INTERNAL_FUNCTIONor similar wrapped around a column name. - Find the two operands in the original condition and compare their types.
- Change the side that is not the indexed column, so the indexed column keeps its type.
The preferred fixes, in order, are these:
- Match the literal or bind type to the column. Write
'5551234'for aVARCHAR2column. Bind strings to character columns and dates to date columns. - Convert the constant explicitly. For a
DATEcolumn and a timestamp value, writeorder_date >= CAST(SYSTIMESTAMP AS DATE) - 1. The conversion applies to the constant, which is computed once. - Correct the column type. If a column named
customer_idholds only numbers, it should be aNUMBER. This removes the whole class of problem, though it is a schema change. - Create a function-based index as a last resort. An index on
TO_NUMBER(phone_number)can serve the numeric query, but it fails for any row with a non-numeric value, so it suits only data you can guarantee is numeric.
Common mistakes
- Converting the column to "fix" the type. Writing
TO_CHAR(order_date, 'YYYY-MM-DD') = '2026-01-15'is an explicit function on the column and blocks the index just as an implicit one does. - Trusting the query text. A statement can look correct and still convert, because the bind variable type or a view definition supplies a different type. Check the plan, not only the SQL.
- Ignoring character-set mismatches. Comparing a
VARCHAR2column with anNVARCHAR2value can also produce anINTERNAL_FUNCTIONon the column. Check the plan whenever you mix the two types.
Exercise
A table invoices has an indexed column invoice_no VARCHAR2(12). The query SELECT amount FROM invoices WHERE invoice_no = 20260117 does a full table scan, and it sometimes fails with ORA-01722. Explain both symptoms and give the corrected query.
Show answer
Oracle compares a VARCHAR2 column with a number by converting the column to a number, so the predicate becomes TONUMBER(invoiceno) = 20260117. A function on the column prevents use of the plain index, which gives the full scan. TONUMBER must also run on every row it examines, so a row with a non-numeric value such as 'INV-9' raises ORA-01722. The fix is to compare with a string of the column's type: WHERE invoiceno = '20260117'. This lets Oracle use a range scan on the index and removes the conversion error.