The symptom
When a query compares a column with a value of another type, the engine converts one side so the two can be compared. Each engine has fixed rules for which side changes. If the value is converted, nothing happens to the plan. If the column is converted, the query has become function(column) = value, and an index on the column can no longer be searched: the same problem as a non-sargable predicate, except that nobody wrote the function.
SQL Server shows it as CONVERT_IMPLICIT in the predicate and adds a warning to the plan. Oracle shows TO_NUMBER or INTERNAL_FUNCTION in the predicate section, PostgreSQL shows a cast on the column, and MySQL shows a full scan with the index listed under possible_keys but not used. The query plan viewer reports the conversion and the column it hit. The examples use a shop schema with 20,000 customers and 400,000 orders.
Diagnose it
SQL Server: CONVERT_IMPLICIT and the plan warning
The usual case is a varchar column compared with an nvarchar value. nvarchar has the higher type precedence, so the column is converted:
CREATE INDEX IX_Orders_Reference ON dbo.Orders (Reference);
GO
SET STATISTICS IO ON;
SET STATISTICS XML ON;
GO
DECLARE @ref nvarchar(20) = N'R4242';
SELECT OrderId FROM dbo.Orders WHERE Reference = @ref;
Table 'Orders'. Scan count 1, logical reads 1039, physical reads 0
The plan carries a PlanAffectingConvert warning, drawn as a yellow triangle on the root operator in Management Studio, and the predicate reads CONVERT_IMPLICIT(nvarchar(20),[Reference],0)=[@ref]. The same query with a varchar variable:
DECLARE @ref varchar(20) = 'R4242';
SELECT OrderId FROM dbo.Orders WHERE Reference = @ref;
Table 'Orders'. Scan count 1, logical reads 3, physical reads 0
How much the conversion costs depends on the column's collation. With a SQL Server collation (the names that start with SQL_), as here, the index is scanned from end to end. With a Windows collation the optimizer can still seek a range, and the cost is smaller.
A number compared with a string goes the other way. int has precedence over varchar, so in CustomerId = '4242' the literal is converted and the index is still searched:
CREATE INDEX IX_Orders_CustomerId ON dbo.Orders (CustomerId);
GO
SELECT OrderId FROM dbo.Orders WHERE CustomerId = '4242';
Table 'Orders'. Scan count 1, logical reads 4, physical reads 0
MySQL: a string column compared with a number
MySQL compares a string column with a number by converting every string to a number, which rules out the index. The plan lists the index under possible_keys and uses none:
CREATE TABLE customer_notes (
note_id int PRIMARY KEY,
customer_ref varchar(20) NOT NULL,
note varchar(100) NOT NULL,
KEY customer_notes_ref_idx (customer_ref)
);
INSERT INTO customer_notes
SELECT customer_id, customer_id, 'call back' FROM customers;
ANALYZE TABLE customer_notes;
EXPLAIN SELECT note FROM customer_notes WHERE customer_ref = 4242\G
EXPLAIN SELECT note FROM customer_notes WHERE customer_ref = '4242'\G
type: ALL
possible_keys: customer_notes_ref_idx
key: NULL
rows: 20405
Extra: Using where
type: ref
possible_keys: customer_notes_ref_idx
key: customer_notes_ref_idx
rows: 1
Extra: NULL
Oracle: TO_NUMBER in the predicate section
Oracle also converts the string column when it meets a number. The predicate section under the plan shows the function it added:
CREATE TABLE customer_notes AS
SELECT customer_id AS note_id, TO_CHAR(customer_id) AS customer_ref, 'call back' AS note
FROM customers;
CREATE INDEX customer_notes_ref_idx ON customer_notes (customer_ref);
EXPLAIN PLAN FOR
SELECT note FROM customer_notes WHERE customer_ref = 4242;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 16 | 23 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| CUSTOMER_NOTES | 1 | 16 | 23 (0)| 00:00:01 |
1 - filter(TO_NUMBER("CUSTOMER_REF")=4242)
EXPLAIN PLAN FOR
SELECT note FROM customer_notes WHERE customer_ref = '4242';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 16 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| CUSTOMER_NOTES | 1 | 16 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | CUSTOMER_NOTES_REF_IDX | 1 | | 1 (0)| 00:00:01 |
2 - access("CUSTOMER_REF"='4242')
PostgreSQL: a cast on the column in the filter
PostgreSQL refuses most mixed comparisons outright (a varchar column against an integer is an error), so the problem is rarer. It does appear between numeric types: an integer column compared with a numeric value is converted to numeric.
CREATE INDEX orders_customer_idx ON orders (customer_id);
EXPLAIN SELECT order_id FROM orders WHERE customer_id = 4242.0;
EXPLAIN SELECT order_id FROM orders WHERE customer_id = 4242;
-> Parallel Seq Scan on orders (cost=0.00..6982.00 rows=833 width=4)
Filter: ((customer_id)::numeric = 4242.0)
Bitmap Heap Scan on orders (cost=4.58..80.82 rows=20 width=4)
Recheck Cond: (customer_id = 4242)
-> Bitmap Index Scan on orders_customer_idx (cost=0.00..4.57 rows=20 width=0)
Index Cond: (customer_id = 4242)
This reaches production through parameters: a driver or ORM that binds a decimal type for a column that is an integer.
Joins between columns of different types
The same conversion happens when two joined columns were declared with different types. The side that gets converted cannot use its index for the join, so the join can only be driven from that side:
EXPLAIN FORMAT=TREE
SELECT c.email, n.note
FROM customers AS c
JOIN customer_notes AS n ON n.customer_ref = c.customer_id
WHERE c.country = 'DE';
-> Nested loop inner join (cost=9206 rows=2041)
-> Table scan on n (cost=2065 rows=20405)
-> Filter: ((c.country = 'DE') and (cast(n.customer_ref as double) = cast(c.customer_id as double))) (cost=0.25 rows=0.1)
-> Single-row index lookup on c using PRIMARY (customer_id=n.customer_ref) (cost=0.25 rows=1)
Fix it
Send the value in the column's type
The cheapest fix is on the calling side. Quote the literal, or declare the parameter with the column's type. In application code this means setting the parameter type explicitly: client libraries commonly send every string as nvarchar to SQL Server unless told otherwise, which is how a varchar column ends up converted on every call.
Cast the value, not the column
When you cannot control the type that arrives, convert the value in the query. A conversion on the value side runs once:
DECLARE @ref nvarchar(20) = N'R4242';
SELECT OrderId FROM dbo.Orders WHERE Reference = CAST(@ref AS varchar(20));
Table 'Orders'. Scan count 1, logical reads 3, physical reads 0
EXPLAIN SELECT note FROM customer_notes WHERE customer_ref = CAST(4242 AS CHAR)\G
type: ref
key: customer_notes_ref_idx
rows: 1
Extra: NULL
EXPLAIN SELECT order_id FROM orders WHERE customer_id = CAST(4242.0 AS integer);
Bitmap Heap Scan on orders (cost=4.58..80.82 rows=20 width=4)
Recheck Cond: (customer_id = 4242)
-> Bitmap Index Scan on orders_customer_idx (cost=0.00..4.57 rows=20 width=0)
Index Cond: (customer_id = 4242)
Change the column type when the column is wrong
If a column holds numbers and is declared as a string, or two columns that are always joined have different types, fix the schema. For the join above, make the referencing column an integer like the key it points to:
ALTER TABLE customer_notes MODIFY customer_ref int NOT NULL;
ANALYZE TABLE customer_notes;
EXPLAIN FORMAT=TREE
SELECT c.email, n.note
FROM customers AS c
JOIN customer_notes AS n ON n.customer_ref = c.customer_id
WHERE c.country = 'DE';
-> Nested loop inner join (cost=2672 rows=1962)
-> Filter: (c.country = 'DE') (cost=1986 rows=1962)
-> Table scan on c (cost=1986 rows=19616)
-> Index lookup on n using customer_notes_ref_idx (customer_ref=c.customer_id) (cost=0.25 rows=1)
Changing a column type rewrites the table on most engines and takes locks; plan it like any other schema change. The implicit cast comparison page lists the conversion rules per engine.
When to leave it alone
Leave it when the conversion is on the value side, when the table is small, or when the converted column is not the one the query needs an index for. A CONVERT_IMPLICIT in the select list changes nothing about how rows are found. SQL Server's warning also fires for conversions that only affect the row estimate; check whether the plan actually scans before you change anything.
The checklist
Work through it in order. Each step gives you the evidence for the next one.
- Look for CONVERT_IMPLICIT (SQL Server), TO_NUMBER or INTERNAL_FUNCTION (Oracle), or a cast on the column (PostgreSQL).
- In MySQL, look for an index listed in possible_keys with key empty.
- Compare the declared type of the column with the type of the literal or parameter.
- Fix the calling side first: quote the literal or set the parameter type.
- Otherwise cast the value in the query, never the column.
- For joins, make both columns the same type.
- Re-capture the plan and confirm the index is searched.
Questions that come up
What is CONVERT_IMPLICIT in a SQL Server plan?
It is a type conversion SQL Server added because the two sides of a comparison had different types. When it wraps a column, as in CONVERT_IMPLICIT(nvarchar(20),[Reference],0), every row's value has to be converted before it can be compared, and an index on that column is scanned instead of searched.
Which side gets converted?
It depends on the engine. SQL Server converts the side with the lower type precedence, so varchar becomes nvarchar and strings become numbers. MySQL and Oracle convert a string column to a number when it is compared with a number. PostgreSQL converts integer to numeric and rejects most string-to-number comparisons with an error.
Why does my ORM cause implicit conversions?
Because of how it binds parameters. Client libraries often send strings as Unicode text and may pick a numeric type that is wider than the column. Against a varchar or integer column that forces a conversion of the column. Setting the parameter type to match the column removes it.
Does an implicit conversion always cause a scan?
No. If the value is converted, the index works normally. Even when the column is converted, SQL Server can still seek for some combinations, such as varchar to nvarchar under a Windows collation. The plan tells you: look at whether the index is searched or scanned.
How do I find type mismatches between joined columns?
Compare the declared types of both sides of each join condition in the catalog: information_schema.columns on most engines. Columns that are joined should have the same type, length and, for strings, the same collation.