title: Stored Procedure slow from Entity Framework because of date parameter
body: <p>I have discovered an interesting issue where a Stored Procedure executes very slowly from Entity Framework. I have already solved the problem, but I would like to hear if anyone can tell me why the solution works.</p>
  
  <p><strong>Issue</strong></p>
  
  <p>I have a Stored Procedure <strong>GetLoginCount</strong> that receives a <strong>@date</strong> parameter of type <strong>DATETIME</strong>. When I execute this Stored Procedure directly on the database it executes within a second. When executing through my application via <strong>Entity Framework</strong>, it takes around 45 seconds.</p>
  
  <p>I have tried using WITH RECOMPILE on the Stored Procedure and cleared execution plans on the server, to ensure it hadn't cached some slow version of the execution plan that didn't use the correct index.</p>
  
  <p><strong>Fast forward after 2 days of experiments</strong>, I found that if I simply put the following in the beginning of my Stored Procedure: <code>DECLARE @date1 DATETIME = @date</code> and use @date1 instead, the Stored Procedure executes in 1 second, also from Entity Framework.</p>
  
  <p><strong>WHY?</strong></p>
  
  <p>I have solved my problem and that's all good and fine, but I need to know <strong>why</strong> this specific solution works.</p>