NHacker Next
  • new
  • past
  • show
  • ask
  • show
  • jobs
  • submit
C# strings silently kill your SQL Server indexes in Dapper (consultwithgriff.com)
wvenable 52 minutes ago [-]
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.

beart 45 minutes ago [-]
I agree with your first point. I've seen this same issue crop up in several other ORMs.

As to your second point. VARCHAR uses N + 2 bytes where as NVARCHAR uses N*2 + 2 bytes for storage (at least on SQL Server). The vast majority of character fields in databases I've worked with do not need to store unicode values.

wvenable 39 minutes ago [-]
> The vast majority of character fields in databases I've worked with do not need to store unicode values.

This has not been my experience at all. Exactly the opposite, in fact. ASCII is dead.

SigmundA 16 minutes ago [-]
Vast majority of text fields I see are coded values that are perfectly fine using ascii, but I deal mostly with English language systems.

Text fields that users can type into directly especially multiline tend to need unicode but they are far fewer.

SigmundA 24 minutes ago [-]
To complicate matters SQL Server can do Nvarchar compression, but they should have just done UTF-8 long ago:

https://learn.microsoft.com/en-us/sql/relational-databases/d...

Also UTF-8 is actually just a varchar collation so you don't use nvarchar with that, lol?

_3u10 40 minutes ago [-]
Generally if it stores user input it needs to support Unicode. That said UTF-8 is probably a way better choice than UTF-16/UCS-2
SigmundA 26 minutes ago [-]
UTF-8 is a relatively new thing in MSSQL and had lots of issues initially, I agree it's better and should have been implemented in the product long ago.

I have avoided it and have not followed if the issues are fully resolved, I would hope they are.

applfanboysbgon 8 minutes ago [-]
I think this is a rather pertinent showcase of the danger of outsourcing your thinking to LLMs. This article strongly indicates to me that it is LLM-written, and it's likely the LLM diagnosed the issue as being a C# issue. When you don't understand the systems you're building with, all you can do is take the plausible-sounding generated text about what went wrong for granted, and then I suppose regurgitate it on your LLM-generated portfolio website in an ostensible show of your profound architectural knowledge.
SigmundA 19 minutes ago [-]
Yes I have run into this regardless of client language and I consider it a defect in the optimizer.
wvenable 14 minutes ago [-]
I wouldn't consider it a defect in the optimizer; it's doing exactly what it's told to do. It cannot convert an nvarchar to varchar -- that's a narrowing conversion. All it can do is convert the other way and lose the ability to use the index. If you think that there is no danger converting an nvarchar that contains only ASCII to varchar then I have about 70+ different collations that say otherwise.
enord 30 minutes ago [-]
This is due to utf-16, an unforgivable abomination.
smithkl42 20 minutes ago [-]
Been bit by that before: it's not just an issue with Dapper, it can also hit you with Entity Framework.
adzm 22 minutes ago [-]
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
jiggawatts 44 minutes ago [-]
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.

valiant55 9 minutes ago [-]
There's a data type precedence that it uses to determine which value should be casted[0]. Nvarchar is higher precedence, therefore the varchar value is "lifted" to an nvarchar value first. This wouldn't be an issue if the types were reversed.

0: https://learn.microsoft.com/en-us/sql/t-sql/data-types/data-...

wvenable 34 minutes ago [-]
It's the optimizer caching the query plan as a parameterized query. It's not re-planning the index lookup on every execution.
SigmundA 29 minutes ago [-]
The parameter type is part of the cache identity, nvarchar and varchar would have two cache entries with possibly different plans.
23 minutes ago [-]
Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact
Rendered at 00:16:20 GMT+0000 (Coordinated Universal Time) with Vercel.