logoalt Hacker News

C# strings silently kill your SQL Server indexes in Dapper

68 pointsby PretzelFischyesterday at 10:55 PM38 commentsview on HN

Comments

wvenableyesterday at 11:24 PM

This really doesn't have anything to do with C#. This is your classic nvarchar vs varchar issue (or unicode vs ASCII). The same thing happens if you mix collations.

I'm not sure why anyone would choose varchar for a column in 2026 unless if you have some sort of ancient backwards compatibility situation.

show 5 replies
diathtoday at 1:55 AM

It's weird that the article does not show any benchmarks but crappy descriptions like "milliseconds to microseconds" and "tens of thousands to single digits". This is the kind of vague performance description LLMs like to give when you ask them about performance differences between solutions and don't explicitly ask for a benchmark suite.

briHasstoday at 12:42 AM

I've found and fixed this bug before. There are 2 other ways to handle it

Dapper has a static configuration for things like TypeMappers, and you can change the default mapping for string to use varchar with: Dapper.SqlMapper.AddTypeMap(typeof(string),System.Data.DbType.AnsiString). I typically set that in the app startup, because I avoid NVARCHAR almost entirely (to save the extra byte per character, since I rarely need anything outside of ANSI.)

Or, one could use stored procedures. Assuming you take in a parameter that is the correct type for your indexed predicate, the conversion happens once when the SPROC is called, not done by the optimizer in the query.

I still have mixed feelings about overuse of SQL stored procedures, but this is a classic example of where on of their benefits is revealed: they are a defined interface for the database, where DB-specific types can be handled instead of polluting your code with specifics about your DB.

(This is also a problem for other type mismatches like DateTime/Date, numeric types, etc.)

show 1 reply
maciekkmrktoday at 1:53 AM

Interesting problem, but the AI prose makes me not want to read to the end.

jiggawattsyesterday at 11:32 PM

This feels like a bug in the SQL query optimizer rather than Dapper.

It ought to be smart enough to convert a constant parameter to the target column type in a predicate constraint and then check for the availability of a covering index.

show 3 replies
andrelaszlotoday at 12:42 AM

I thought, having just read the title, that maybe it's time to upgrade if you're still on Ubuntu 6.06.

enordyesterday at 11:46 PM

This is due to utf-16, an unforgivable abomination.

mvdtnztoday at 2:32 AM

This is a really interesting blog post - the kind of old school stuff the web used to be riddled with. I must say - would it have been that hard to just write this by hand? The AI adds nothing here but the same annoying old AI-isms that distract from the piece.

smithkl42yesterday at 11:56 PM

Been bit by that before: it's not just an issue with Dapper, it can also hit you with Entity Framework.

adzmyesterday at 11:54 PM

even better is Entity Framework and how it handles null strings by creating some strange predicates in SQL that end up being unable to seek into string indexes

ltbarcly3today at 2:47 AM

Life is too short to use SQL Server. I know people that use it will swear it's "not bad anymore" but yes it is.