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

📚 Interview question

Why does AddWithValue cause plan cache bloat as well as implicit conversions, and how do you detect it?

Covers how SqlParameterCollection.AddWithValue infers nvarchar with the value's length, why that creates one cached plan per length and implicit conversions on varchar columns, and how to detect and fix it.

Senior Debugging Advanced ado-net implicit-conversion parameterisation plan-cache sql-server

🎯 Key points

  • AddWithValue infers nvarchar(n) with n = each value's length: nvarchar(5) for 'Smith', nvarchar(6) for 'Nguyen'
  • Different parameter declarations = different statement text = a separate compiled plan per length
  • Cost: extra compile CPU, plan cache memory taken from the buffer pool, and plans evicted sooner
  • Detect: many rows per query_hash with different plan_handle in sys.dm_exec_query_stats, or many query ids per query text in Query Store
  • Same API also causes CONVERT_IMPLICIT scans on varchar columns
  • Fix: Parameters.Add with an explicit SqlDbType and fixed Size; Dapper and EF Core already use fixed sizes

⏱ 30-second answer

AddWithValue infers the parameter type from the .NET value: a string becomes nvarchar(n) where n is that value's length, so 'Smith' is sent as nvarchar(5) and 'Nguyen' as nvarchar(6). The parameter declaration is part of the statement text, so SQL Server compiles and caches a separate plan per length. On a busy system that means extra compile CPU, plan cache memory taken from the buffer pool, and useful plans evicted sooner. If the column is varchar, the same call also causes a CONVERT_IMPLICIT scan.

Detect it as many rows sharing one query_hash with different plan_handles in sys.dm_exec_query_stats, or many query ids for one query in Query Store. Fix with Parameters.Add("@p", SqlDbType.VarChar, 20); Dapper and EF Core already send fixed sizes.

🔍 What the interviewer is checking

Checks understanding of parameter size and plan reuse.

↩ Usually asked after

More in Indexes

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me