SqlShare.com Logo
 
Skip Navigation Links
Home
Video List
Events
Classes
About Us
Login / Register
Subscribe RSS Feed 

Takes Query Plan Analysis and Performance Monitoring to a New Level

Solve Parameter Sniffing By Using Local Variables

Parameter sniffing is a process that occurs when executing a stored procedure for the first time that allows SQL Server to build an effective query plan. This video shows you how to limit the performance effects of parameter sniffing by utilizing local variables.

Duration:
2 mins 30 secs
Skill Level:
100
Rating:
4.13 out of 5
Publish Date:
November 06, 2008
Solve Parameter Sniffing By Using Local Variables You must be logged in to view this video.  
Bookmark and Share
 
1=Poor, 3=Good, 5=Excellent

About the Author

Image of Plamen Ratchev
Plamen Ratchev has over two decades experience in the software architecture and development field. He has enjoyed building solutions for UNIX, DOS and Windows platforms, with main focus on SQL Server since version 4.21. Plamen is founder of Tangra, specializing in relational database applications analysis, implementation, and tuning. His particular interest is in design patterns, performance a...

References

There are no downloads or recommended reading links for this video

Comments
selva on 12/17/2008
it is good

Carsten Hynne on 7/8/2009
Again: Looking at the execution plans would make it much clearer.

William on 7/8/2009
Excellent lesson thanks. How do I store my favorites to my account?

Mark Nilsen on 7/8/2009
When the new local variable is set the passed in parameter, the passed in parameter is named incorrectly.

Bala on 7/8/2009
ok

Rick Wadsworth on 7/8/2009
seemed to stop before explaining the point?!

Rick Wadsworth on 7/8/2009
seemed to stop before explaining the point?!

Hansjörg on 7/8/2009
Show the bad plan?

Jed Perlowin on 7/8/2009
Very interesting, but I would like to know a little bit more -- maybe should I always be doing this, or when should I do this...

Joe Celko on 7/8/2009
Nice and clean, even with the typo

nilanaksha on 7/8/2009
Good one. Want to see some more of this kind..

There is no explanation why, what is happenning now with exec plan.

02F3ADB9C5 on 7/9/2009
He spelled customerid wrong. He wrote cutomerid

Plamen Ratchev on 7/9/2009
Jed, as noted parameter sniffing in general is a good thing and you should not worry about it unless you have performance problems. In this example using local variables in effect disables parameter sniffing, which will generate execution plan based on statistics, and that may not be best. You can read more details and different approaches on my blog: http://pratchev.blogspot.com/2007/08/parameter-sniffing.html.

Plamen Ratchev on 7/9/2009
Carsten, Hansjörg, Aleksandar, here are the two possible execution plans: Index/Table scan: StmtText -------------------------------- |--Clustered Index Scan(OBJECT:([dbo].[Orders].[PK_Orders]), WHERE:([dbo].[Orders].[CustomerID]=[@customerid])) Index seek: StmtText -------------------------------- |--Nested Loops(Inner Join, OUTER REFERENCES:([dbo].[Orders].[OrderID])) |--Index Seek(OBJECT:([dbo].[Orders].[CustomerID]), SEEK:([dbo].[Orders].[CustomerID]=[@customerid]) ORDERED FORWARD) |--Clustered Index Seek(OBJECT:([dbo].[Orders].[PK_Orders]), SEEK:([dbo].[Orders].[OrderID]=[dbo].[Orders].[OrderID]) LOOKUP ORDERED FORWARD) If you execute the stored procedure first for very selective customer with only a few orders, the index seek plan is generated. All subsequent stored procedures use the index seek plan, even if it is for large set of orders which can benefit from the table scan plan. Or the opposite, if the large set of orders is executed first then the table scan plan is generated and then used even for a small set of orders that can benefit from the index seek plan.

Kamen Angelov on 7/11/2009
Will be good to append something about Level method

Kamen Angelov on 7/11/2009
Very useful!

Travis on 7/16/2009
Nice little twist to parameter sniffing. Good to know..

Caleb Bell on 7/16/2009
It would be good to give more examples of when and why you'd want to do this, as well as the performance impact of doing so.

Plamen Ratchev on 7/17/2009
Caleb, you have to use these types of techniques only when you encounter parameter sniffing. The performance impact is that this method in effect disables parameter sniffing and the plans are generated based on statistical data, which may not be the best in all cases.

josh on 7/24/2009
no proof that this technique works

Plamen Ratchev on 7/25/2009
Josh, the proof is that SQL Server does not sniff local variables and therefore cannot create optimized plans for those variables, resulting in plan based on statistics. Read this article by Ken Henderson for more details: http://blogs.msdn.com/khen1234/archive/2005/06/02/424228.aspx.

Henk Schreij on 8/20/2009
you made a typo: forgot the s :-)

If, as stated, the query plan is determined upon the first invocation, the query plan results should be the same whether a parameter or local var is used. If not, then recompilation is required.

Plamen Ratchev on 1/20/2010
Arturo, when a local variable is used SQL Server cannot use the parameter values to generate plan and the plan is based on the statistics. When the parameters are used directly the plan is generated based on the parameter values. This is why the plan can be different.

Arun on 3/4/2010
Good one!!!

khurram on 4/7/2010
Back in 2005, I figured it out but could not name this scenario. Thank you for providing name 'parameter sniffing' for it.

hitesh on 6/24/2010
good

Neil Abram on 6/24/2010
This was good to a point. It does not state what is good about parameter sniffing. Does this local variable technique force an execution plan to be created every time?

Team FAB on 6/24/2010
I didn't really understand why making that change would improve things, but if I ever see code like that I'll know it might have been done for a reason.

Ed Svastits on 6/24/2010
It would have been excellent if you would have shown us the execution plans and the profiler traces.

Plamen Ratchev on 6/24/2010
Neil, using local variables does not force plan recompilation. It simply disables parameter sniffing and generates plan based on statistics (not on the passed parameter value). See here for more details and examples: http://pratchev.blogspot.com/2007/08/parameter-sniffing.html

Plamen Ratchev on 6/24/2010
Team FAB, this technique will improve performance only if there is performance issue due to parameter sniffing (that means if there was plan created that is inefficient for some parameter values). In essence using local variables disables parameter sniffing and the optimizer generates plan based on statistics (not on the passed parameter value).

William Willyerd on 6/24/2010
Had never even thought of this method of variable usage!

245BE142DD on 6/24/2010
Uncorrected typo in the demo, but the point and method is clear. Good job.

Stephen Dyckes on 6/24/2010
Great to learn something so simple that can have a huge impact.

Russell Tye on 6/24/2010
Very interesting approach.

Denise Tsubota on 6/24/2010
Very informative and will be very helpful to me

mark on 6/24/2010
in sq

Gil on 6/24/2010
There was a typo (@cutomerid instead of @customerid)

B on 6/24/2010
Cannot understand all his words due to accent.

26168E2B7B on 6/24/2010
This is somewhat misleading. Just because parameter sniffing occurs does not mean statistics are not being used. Isn't the real issue whether or not it makes sense to reuse the cached query plan?

Plamen Ratchev on 6/24/2010
26168E2B7B, the cached query plan will be reused with parameter sniffing and when you use local variables to disable parameter sniffing and generate plan based on statistics. But the plans will differ (most likely) and one can be very inefficient.

Cosmin Tornea on 6/25/2010
cool

Shashank Banerjea on 6/25/2010
I wish the author had provided examples by executing the stored procedure before and after the changes.

Charlie Bruno on 6/26/2010
I learned something new. Thank you.

sat on 6/30/2010
Have you tested the performance difference using and without using local variables . Can you demonstrate?

Plamen Ratchev on 7/1/2010
Sat, testing performance differences matters only if you do have performance problems due to parameter sniffing. Yes, I have tested and resolved many cases where local variables helped to improve performance. The answer is in the query plans generated when using local variables and without local variables. This is what makes the difference.

Christian Bahnsen on 7/1/2010
Great tip, as usual. Welcome back, haven't seen any posts from you lately.

Mark Jones on 12/14/2010
Further explantion and query plan examples would have been helpful

Suzanne Artzberger on 3/21/2011
rated sometime before. viewed again on 3-21-11

Acton on 8/26/2011
Would have liked to have seen the query plan before the change and the query plan after the change.

Praveen on 12/13/2011
more explanation...wit some examples

Praveen on 12/13/2011
more explanation...wit some examples

Praveen on 12/13/2011
more explanation...wit some examples

Praveen on 12/13/2011
more explanation...wit some examples

Praveen on 12/13/2011
more explanation...wit some examples

Paul on 12/13/2011
Great tip! Simple change with potentially huge performance impact. Thank you.

Spelled @customerid wrong in line 8.

Fabiano on 12/13/2011
Something bad to do... there is another alternatives to solve this problem and this is the worst...

Dennis on 12/13/2011
a good start about local variables

Ryan on 12/13/2011
awesome tip. Would have been nice to actually see the plans to visualize the effect.

Gil on 12/13/2011
There was a typo ("@cutomerid").

Tim Garcia on 12/14/2011
I still don't understand what this topic was about. Very confusing. Doesn't even seem to make sense.

Plamen Ratchev on 12/15/2011
Fabiano, this method is one of the best known solutions for SQL Server 2000. For newer versions there are other related videos: http://www.sqlshare.com/solve-parameter-sniffing-by-using-optimize-for-unknow_532.aspx, http://www.sqlshare.com/solve-parameter-sniffing-by-using-optimize-for_533.aspx, http://www.sqlshare.com/solve-parameter-sniffing-by-using-option-recompile_534.aspx, http://www.sqlshare.com/solve-parameter-sniffing-by-using-recompile_536.aspx

Mike on 12/29/2011
SQL2008 has also introduced 'OPTION (OPTIMIZE FOR UNKNOWN)' which is another option.

John O'Sullivan on 1/1/2012
interesting tip



Must Be Logged In
 

How Do I Become a Video Author? |  Newsletter History

Copyright © Fourdeuce, Inc., 2005-2009. All Rights Reserved | Privacy Policy | Terms & Conditions