Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So I had a similar issue a couple of weeks ago, which was explained to me by one of our DBAs thusly (paraphrased and dumbed down to my level of course):</p> <p>When a SQL Server stored procedure is called, the server creates and caches an execution plan per stored procedure <em>per <code>object_id</code></em>. Sometimes a bad execution plan can be created by SQL Server depending on the parameter value that is passed in (in our case it was null for a nullable parameter). When this happens, the quick fix is to run <code>sp_recompile 'Schema.Procedure'</code> in SQL Server Management Studio (or whatever DB management tool you might be using). All this does is clear the plan cache for that stored procedure. If the next callee of the proc passes in the "bad" parameter value again you'll be stuck in the same situation <strong>so the real fix is to</strong> give the query a hint using the <code>OPTIMIZE FOR</code> syntax (see <a href="http://msdn.microsoft.com/en-gb/library/ms181714.aspx">http://msdn.microsoft.com/en-gb/library/ms181714.aspx</a>).</p> <p>In short if you add <code>OPTION (OPTIMIZE FOR (@myParameter = 'Some value that gives you a GOOD execution plan'))</code> after your <code>WHERE</code> and/or <code>ORDER BY</code> clauses it should fix the issue.</p> <p>Also, if you're wondering why when executing the <em>same</em> SQL in SSMS you always get fast results it's because SSMS has a default setting of ON for an option called ARITHABORT (<code>SET ARITHABORT ON</code>) which all other apps have set to OFF by default, but how it works and its implications are beyond my experience and I haven't bothered to read up on it. I have been told that I shouldn't use it though. I'm sure a real DBA can do a better job of explaining why.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload