title: ADO.NET: Safe to specify -1 for SqlParameter.Size for all VarChar parameters?
body: <p>We have an existing body of C# code that calls parameterized ad-hoc SQL Server queries in many places. We never specify SqlParameter.Size, and it's documented that in this case, the SqlParameter class infers the size from the parameter value. We just recently became aware of the SQL Server plan cache pollution issues this creates, where a separate plan is cached for each distinct combination of parameter sizes.</p>
  
  <p>Luckily, whenever we create a SqlParameter, we do so via a single utility method, so we have the opportunity to add a few lines to that method and make this problem go away. We are thinking about adding the following:</p>
  
  <pre><code>if((sqlDbType == SqlDbType.VarChar) || (sqlDbType == SqlDbType.NVarChar))
      m_sqlParam.Size = -1;
  </code></pre>
  
  <p>In other words, every time we pass a varchar parameter, pass it as a varchar(max). Based on some quick testing, this works fine, and we can see (via SQL Profiler and sys.dm_exec_cached_plans) that there is now a single plan in the cache for each ad-hoc query, and the type of the string parameter(s) is now varchar(max).</p>
  
  <p>This seems like such an easy solution that there must be some hidden, performance-destroying downside. Is anyone aware of one?</p>
  
  <p>(Please note that we only need to support SQL Server 2008 and later.)</p>
  
  <h2>Update (Jan. 16)</h2>
  
  <p><strong>Yes, there is a hidden, performance-destroying downside!</strong> </p>
  
  <p>Many thanks to Martin Smith, whose answer (see below) pointed me to the right way to analyze this. I tested with our application's Users table, which has an Email column defined as nvarchar(100), and has a non-clustered index (IX_Users_Email) on the Email column. I modified Martin's example query as follows:</p>
  
  <pre><code>declare @a nvarchar(max) = cast('a' as nvarchar(max))
  --declare @a nvarchar(100) = cast('a' as nvarchar(100))
  --declare @a nvarchar(4000) = cast('a' as nvarchar(4000))
  
  select Email from Users where Email = @a
  </code></pre>
  
  <p>Depending which of the "declare" statements I un-comment, I get a VERY different query plan. The nvarchar(100) and nvarchar(4000) versions both give me an index <strong>seek</strong> on IX_Users_Email -- in fact, any length I specify gives me the same plan. The nvarchar(max) version, on the other hand, gives me an index <strong>scan</strong> on IX_Users_Email, followed by a Filter operator to apply the predicate.</p>
  
  <p>That's enough for me -- if there is any possibility of getting a scan rather than a seek, then this "cure" is worse than the disease.</p>
  
  <p><strong>New Proposal</strong></p>
  
  <p>I noticed that every time SQL Server parameterizes a query with a varchar parameter, the cached plan simply uses varchar(8000) (or nvarchar(4000)) for the parameter. I figure if it's good enough for SQL Server, it's good enough for me! Replacing the C# code in my original question (above) with:</p>
  
  <pre><code>if(sqlDbType == SqlDbType.VarChar)
      m_sqlParam.Size = 8000;
  else if(sqlDbType == SqlDbType.NVarChar)
      m_sqlParam.Size = 4000;
  </code></pre>
  
  <p>This seems to solve the plan cache pollution problem without the same impact on the query plans as using a Size of -1. However, I have not done a lot of testing with this, and I am very interested to hear anyone's input on this revised approach.</p>
  
  <h2>Update (Sep. 24)</h2>
  
  <p>We had to modify the prior version (New Proposal, above) to handle the case where the parameter value is longer than the maximum. At that point, you have no choice but to make it a varchar(max):</p>
  
  <pre><code>if((sqlDbType == SqlDbType.VarChar) || (sqlDbType == SqlDbType.NVarChar))
  {
      m_sqlParam.Size = (sqlDbType == SqlDbType.VarChar) ? 8000 : 4000;
  
      if((value != null) &amp;&amp; !(value is DBNull) &amp;&amp; (value.ToString().Length &gt; m_sqlParam.Size))
          m_sqlParam.Size = -1;
  }
  </code></pre>
  
  <p>We've been using this version for about six months with no problems.</p>