📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

Why is nvarchar(max) a problem for indexing and performance in SQL Server?

Junior Theory Beginner

🎯 Key points

  • nvarchar(max) cannot be a B-tree index key; only INCLUDE or full-text
  • Values over 8,000 bytes go off-row to LOB pages: extra I/O
  • Optimiser assumes a large average size for max columns → inflated memory grants
  • Bigger grants mean RESOURCE_SEMAPHORE waits under load
  • EF Core maps unconfigured string to nvarchar(max)
  • Fix: HasMaxLength, [MaxLength] or a ConfigureConventions default
  • Keep max for real documents; consider varchar with a UTF-8 collation or JSON where suitable

⏱ 30-second answer

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.

🗣 Full answer (senior)

How I would answer this in an interview

"Three things go wrong. First, indexing: SQL Server will not let an nvarchar(max) column be an index key, so a WHERE Email = @e on it scans. You can put it in INCLUDE, 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 max column it assumes a large average width. A SELECT ... ORDER BY over a few hundred thousand rows then asks for far more memory than it needs; under load queries queue on RESOURCE_SEMAPHORE.

In EF Core the cause is usually an unconfigured string, which maps to nvarchar(max). I set lengths with HasMaxLength or a model-wide convention, and I keep max only for documents or free text."


Comparison

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

How to find it

  • sys.columns where max_length = -1 lists every max column.
  • Query Store or the actual plan shows Excessive Grant warnings and granted vs used memory.
  • sys.dm_exec_query_memory_grants shows queries waiting for a grant.

Production example

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].

🔍 What the interviewer is checking

Checks the link between EF configuration and SQL Server behaviour.

↩ Usually asked after

More in EF Core Fundamentals

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me