nvarchar Comparison and CONVERT_IMPLICIT: When a Seek Becomes a Scan

Comparing a varchar column with a Unicode value (N'…', an nvarchar parameter) makes SQL Server convert the column side, and the Index Seek becomes an Index Scan. Finding one order by order number reads 7,478 pages.

SQL

Situation

An API finds one order by order number. There is an index (orders_x3), yet one request takes hundreds of milliseconds, and at busy hours it uses a lot of CPU. The application sends string parameters as Unicode (nvarchar). Many drivers do this by default (JDBC's sendStringParametersAsUnicode=true, .NET's string). The sandbox reproduces the same situation with an N'…' literal.

Tables

TableColumnTypeDescription
ordersord_novarchar(20)Order number (yyyyMMdd + sequence number)
IndexColumns
orders_x3ord_no

Look up one row by order number

BEFORE · Compare with a Unicode value

SELECT *
FROM orders
WHERE ord_no = N'2025091501976001'

Run it and check whether it did a Seek or a Scan on orders_x3, and look at the warning at the top of the Raw tab.

Approach

1. Check whether a one-row lookup is a Scan

If the result is 1 row but an Index Scan read the whole index (7,478 pages), the condition failed to define a search range.

2. Read the warning and the predicate

Find the PlanAffectingConvert warning in the Raw tab and CONVERT_IMPLICIT(nvarchar(20), [ord_no], 0) = N'…' in the operator's predicate. When SQL Server compares two values of different types, it converts the lower-precedence side to the higher one. nvarchar has higher precedence than varchar, so the column, not the value, is converted. This is the same as transforming the column, so no Seek is possible (Scenario 1).

3. Match the value's type to the column

If the column cannot change, make the type of the compared value (literal, parameter) varchar.

Explanation

Unicode value → varchar value

AFTER · Compare with the same type

SELECT *
FROM orders
WHERE ord_no = '2025091501976001'

Why it got faster

  • Before reads orders_x3 to the end with an Index Scan, converting ord_no to nvarchar for every row to compare. Buffers are about 7,480, with hundreds of ms of CPU.
  • After finds the value's position directly with an Index Seek (3 pages) and fetches the remaining columns with a Key Lookup. Buffers are 6. The warning also disappears.

Why this is optimal

  • It descends the B-tree once to find one row and reads that row once. This is the minimum cost of a one-row lookup.

Variation: short status values behave the same

TRY · Status value as Unicode

SELECT COUNT(*)
FROM orders
WHERE status = N'CANCELED'
  AND ord_dt >= CAST('2025-09-01' AS datetime2(0))

TRY · Status value as varchar

SELECT COUNT(*)
FROM orders
WHERE status = 'CANCELED'
  AND ord_dt >= CAST('2025-09-01' AS datetime2(0))
  • Comparing as Unicode scans all of orders_x2 (about 6,000 pages). Comparing as varchar seeks only the ('CANCELED', 2025-09-01) range (10 pages). Once the leading column is converted, the ord_dt range after it cannot be used either.

Key points

  • When types differ, SQL Server converts one side by data type precedence. For varchar column = nvarchar value, the column is converted and the index cannot be used.
  • The signals are the PlanAffectingConvert warning in the actual execution plan and CONVERT_IMPLICIT in the predicate.
  • In the application, match the parameter type to the column (JDBC sendStringParametersAsUnicode=false, .NET SqlDbType.VarChar, column type mapping in the ORM).
  • If the server collation is a Windows collation, SQL Server sometimes does a range Seek even with the converted value, but with the default SQL collation (SQL_Latin1_General_CP1_CI_AS) it becomes a Scan as in this scenario.
  • Differences by DB: this scenario is SQL Server-only. In Oracle the same thing happens when a character column is compared with a number (Oracle Scenario 1), and PostgreSQL rejects such a comparison with an error.