An nvarchar(max) column cannot be a key column of a B-tree index (clustered or non-clustered); it can only sit in INCLUDE or a full-text index, so a lookup on it scans. Values that do not fit in the row (over 8,000 bytes) move to LOB pages, so reading them costs extra I/O, and online index operations were historically restricted. The optimiser also estimates row size from the declared type, assuming a max column is about half-full of a large value, so sorts and hash joins get oversized memory grants that waste memory and reduce concurrency. EF Core maps an unconfigured string to nvarchar(max), so the fix is to give strings realistic lengths with HasMaxLength, a convention, or [MaxLength], and keep max for genuine documents.
"Three things go wrong. First, indexing: SQL Server will not let an
nvarchar(max)column be an index key, so aWHERE Email = @eon it scans. You can put it inINCLUDE, or use full-text search, but you cannot seek on it.Second, storage: once a value exceeds 8,000 bytes it moves off-row to LOB pages, and reading it means extra page reads. Short values stay in-row, so the damage is mostly from genuinely big values and from scans.
Third, and the one people miss, is the optimiser. It sizes memory grants from estimated row size, and for a
maxcolumn it assumes a large average width. ASELECT ... ORDER BYover a few hundred thousand rows then asks for far more memory than it needs; under load queries queue onRESOURCE_SEMAPHORE.In EF Core the cause is usually an unconfigured
string, which maps tonvarchar(max). I set lengths withHasMaxLengthor a model-wide convention, and I keepmaxonly for documents or free text."
nvarchar(256) |
nvarchar(max) |
|
|---|---|---|
| Index key | Yes (within the 1,700-byte non-clustered limit) | No; INCLUDE or full-text only |
| Storage | In-row | In-row up to 8,000 bytes, then LOB pages |
| Memory grant estimate | Based on ~half the declared size | Large default assumption |
| Good for | Names, emails, codes | Documents, notes, JSON |
sys.columns where max_length = -1 lists every max column.sys.dm_exec_query_memory_grants shows queries waiting for a grant.A customer search sorted on an unconfigured Name column requested [~1 GB] grants per execution; after HasMaxLength(200) and an index on Name, the grant fell to [a few MB] and p95 dropped from [1.8 s] to [120 ms].
Checks the link between EF configuration and SQL Server behaviour.
Last updated 23 Nov 2026.