A developer sends me a query plan from production. Same query, same index as in test. In test it does an Index Seek and returns in a millisecond. In production it does an Index Scan, reads every row of a big table, and runs a few thousand times a minute.
Nobody changed the code. The difference is a setting most people never look at: the collation of the column.
The setup is everywhere
You need three ingredients, and most .NET applications on SQL Server have all three:
- A
VARCHARcolumn with an index, for exampleEmail varchar(100). - An application that sends strings as
NVARCHAR. That is what ADO.NET does withAddWithValue, and what Entity Framework does for astringproperty unless you tell it otherwise. - A comparison between the two:
WHERE Email = @Email.
NVARCHAR has higher data type precedence than VARCHAR, so SQL Server converts the column side, not the parameter. In the plan you see a CONVERT_IMPLICIT on the column, and usually a warning on the SELECT operator.
What happens next depends on the collation
Windows collation (for example Latin1_General_CI_AS): VARCHAR and NVARCHAR follow the same comparison rules. The optimizer can still compute a range on the converted value and do a seek. You get an Index Seek with a warning. Not perfect, but fast.
SQL collation (for example SQL_Latin1_General_CP1_CI_AS): VARCHAR data is compared with the old SQL Server rules, NVARCHAR with the Unicode rules. The two sort orders are different, so the optimizer cannot turn the converted value into a range on the index. It has to scan. Every row, every call.
The twist: SQL_Latin1_General_CP1_CI_AS is the default that setup proposes on servers with a US English Windows locale. Your test server was installed by someone in Germany, production by a hosting provider in the US, and the same code behaves completely differently.
Strictly speaking it is the collation of the column that matters. Columns inherit it from the database, and databases usually inherit it from the instance, which is why it looks like an instance setting.
Reproduce it in two minutes
Run this on a test instance. The table has the same data twice, once in a column with a SQL collation and once with a Windows collation:
CREATE TABLE dbo.CollationDemo (
Id int IDENTITY PRIMARY KEY,
EmailSql varchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
EmailWin varchar(100) COLLATE Latin1_General_CI_AS NOT NULL
);
INSERT dbo.CollationDemo (EmailSql, EmailWin)
SELECT TOP (1000000)
CONCAT('user', n, '@example.com'),
CONCAT('user', n, '@example.com')
FROM (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS n
FROM sys.all_objects AS a CROSS JOIN sys.all_objects AS b) AS x;
CREATE INDEX IX_EmailSql ON dbo.CollationDemo (EmailSql);
CREATE INDEX IX_EmailWin ON dbo.CollationDemo (EmailWin);
Now query both columns with an NVARCHAR parameter, the way your application does, with the actual execution plan on:
SET STATISTICS IO ON;
DECLARE @e nvarchar(100) = N'[email protected]';
SELECT Id FROM dbo.CollationDemo WHERE EmailSql = @e; -- Index Scan
SELECT Id FROM dbo.CollationDemo WHERE EmailWin = @e; -- Index Seek (with a warning)
Compare the logical reads in the Messages tab: thousands for the first query, a handful for the second. Same data, same index definition, same query.
Check your own servers
First, the defaults:
SELECT SERVERPROPERTY('Collation') AS instance_collation,
DATABASEPROPERTYEX(DB_NAME(), 'Collation') AS database_collation;
Then the columns at risk: indexed VARCHAR or CHAR columns with a SQL collation.
SELECT s.name AS schema_name, t.name AS table_name,
c.name AS column_name, c.collation_name
FROM sys.columns AS c
JOIN sys.tables AS t ON t.object_id = c.object_id
JOIN sys.schemas AS s ON s.schema_id = t.schema_id
JOIN sys.types AS ty ON ty.user_type_id = c.user_type_id
WHERE ty.name IN ('varchar', 'char')
AND c.collation_name LIKE 'SQL[_]%'
AND EXISTS (SELECT 1 FROM sys.index_columns AS ic
WHERE ic.object_id = c.object_id AND ic.column_id = c.column_id)
ORDER BY s.name, t.name, c.name;
And finally the queries that are actually paying the price. This one searches the plan cache, so run it off-peak on busy servers:
SELECT TOP (20)
qs.execution_count,
qs.total_logical_reads / qs.execution_count AS avg_logical_reads,
SUBSTRING(st.text, 1, 200) AS query_text
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) AS qp
WHERE CAST(qp.query_plan AS nvarchar(max)) LIKE N'%CONVERT_IMPLICIT%'
ORDER BY qs.total_logical_reads DESC;
The fix: send the right type
You do not need to change the column or the collation. Changing an instance or database collation is a big project, and converting columns to NVARCHAR doubles their size. The cheap fix is in the application: send the parameter as VARCHAR, with the same length as the column.
ADO.NET
cmd.Parameters.Add("@Email", SqlDbType.VarChar, 100).Value = email;
Entity Framework Core
modelBuilder.Entity<Customer>()
.Property(c => c.Email)
.IsUnicode(false)
.HasMaxLength(100);
Dapper
var p = new { Email = new DbString { Value = email, IsAnsi = true, Length = 100 } };
Setting the length matters too. Without it, every different string length can produce a different cached plan.
The opposite case is harmless: an NVARCHAR column compared with a VARCHAR parameter converts the parameter, not the column, and the seek stays.
In short
- VARCHAR column plus NVARCHAR parameter means an implicit conversion on the column.
- With a Windows collation you still get a seek. With a SQL collation you get a scan.
- Check the collation of your indexed VARCHAR columns, then fix the parameter types in the application.
It is one of the most common findings in the SQL Server health checks I run, and often the cheapest big win: one line of code, and CPU and reads drop on the hottest queries.
