HeadlinesBriefing favicon HeadlinesBriefing.com

Dapper String Parameters Silent SQL Performance Killer

Hacker News •
×

A developer recently discovered a silent performance killer in production: a simple Dapper query was causing CPU spikes to 90%. The culprit turned out to be a two-character type mismatch between C# strings and SQL Server varchar columns, completely invisible in the code yet devastating to performance.

When C# strings pass through Dapper's anonymous objects, they map to nvarchar(4000) by default. For varchar columns, SQL Server performs CONVERT_IMPLICIT on every row, making index seeks impossible. Instead of microseconds per execution, queries took milliseconds, with hundreds of thousands of executions daily creating a massive CPU burden.

The fix requires explicit parameter typing with DbType.AnsiString for varchar columns. This simple change transforms index scans into seeks, reducing logical reads from tens of thousands to single digits. Performance improves dramatically with no schema changes needed—just proper parameter matching between C# and SQL Server data types.