SQL Parameter Sniffing and what to do about it
Here was an interesting problem we ran into yesterday. We have about 50 people using a web site that I help build. All but one of them was logging in with no problems. But any time our “special” user logged in (lets call him John Doe) he would get a timeout.
That one left us scratching our heads. Luckily we have top notch security in place, so we were able to log in as him as well (without him knowing, and without knowing his password).
Hint: use forms authentication and write your own membership provider on a mirrored production site that no one outside of IT knows about.
When we log in as him we also get a timeout. OK, time to fire up SQL Server Query Profiler and see what is going on…which doesn’t take long. We see the query, a stored procedure really, taking +30 seconds to run. When we log in as a different user it takes less than one second. In fact we can log in as any other user and it takes less than one second.
Next step, dissect the query. We pull the query out of the stored proc (it isn’t that complicated of a query) and run it from Management Studio with John Doe’s info. ~1 second.
My coworker did some web searching and stumbled on the answer (Google to the rescue, giving us answers even when we really don’t know the question): SQL Parameter Sniffing.
The rundown is that when SQL Server compiles the query and creates the execution plan, if you pass parameters from the stored procedure call directly into the query, SQL Server will use the values passed in to “influence” the query plan. That can be a bad thing. Here is the Microsoft definition:
“Parameter sniffing” refers to a process whereby SQL Server’s execution environment “sniffs” the current parameter values during compilation or recompilation, and passes it along to the query optimizer so that they can be used to generate potentially faster query execution plans. The word “current” refers to the parameter values present in the statement call that caused a compilation or a recompilation.
Luckily the fix is easy. Take your stored procedure that looks like this (generalizing a query here:
And change it to this:
Look on lines 5,6, and 10 for changes.
What did I really do? I once again proved that any programming problem can be solved with another layer of indirection. Really.
For some strange reason, when you pass the parameters (@UserName in this case) to a local variable (@myUserName) SQL Server will no longer use the value of the parameter (@UserName) to influence the query plan.
If you don’t believe me (and really, you shouldn’t ever believe the word of just one person), do a Google search on SQL Parameter Sniffing.
We tried it, tested it, did a performance check on it, it worked. But this now goes onto the top of my “Strange fixes for SQL Server” stack.
- Parameter Sniffing & Stored Procedures Execution Plan, SQL Garbage Collector
- How to Disable Parameter Sniffing in SQL Server 2005, Glenn Berry
- Tips, Tricks, and Advice from the SQL Server Query Optimization Team