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 OPTIMIZE FOR

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. Normally that is a good thing, but in some cases can actually hurt performance. SQL MVP Plamen Ratchev shows how OPTIMIZE FOR can fix this problem very cleanly.

Duration:
3 mins 9 secs
Skill Level:
200
Rating:
4.06 out of 5
Publish Date:
November 06, 2008
Solve Parameter Sniffing By Using OPTIMIZE FOR 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
suvankar on 4/23/2009
simply great

neil on 4/23/2009
Hidden Gem

Denise Yu on 4/23/2009
.

D151BB6B9F on 4/23/2009
The way the presenter ROLLS his R's is so heavy as to be very distracting and hard to understand. His English is good except for that. He seems like a bright guy, and paying attention to his R's shouldn't be that big a deal, so why doesn't he do it? Also, your question page won't scroll down in Firefox.

Matt Wade on 4/23/2009
Works only for a singular case, does not address multiple parameters with multiple values per run, in those cases you have to use recompile.

Jed Perlowin on 4/23/2009
Very interesting, but I would like to have seen a more in depth explination of why this helps. I'm not sure I understood the reason for the advantage of using the OPTIMIZE FOR clause.

mark on 4/23/2009
should have shown what we do today to fix the problem. that is put the param in a temp var then use that in the where clause

Plamen Ratchev on 4/23/2009
Matt/Jeff, here is an article that I wrote which explains in greater detail parameter sniffing and different solutions: http://pratchev.blogspot.com/2007/08/parameter-sniffing.html. The OPTIMIZE FOR query hint has limited scope but it can be useful tool in case optimization has to be done for a known value (or in case of SQL Server 2008 for UNKNOWN which will use statistical data).

Plamen Ratchev on 4/23/2009
Mark, there is a video that demonstrates using local variables to solve parameter sniffing: http://www.jumpstarttv.com/solve-parameter-sniffing-by-using-local-variables_531.aspx. But that is just one method, which may not work in future versions of SQL Server because SQL Server supports recompilation at statement level and is capable of sniffing local variables.

bc on 4/23/2009
would be good to demo the bad plan vs the good plan

borjonx on 4/23/2009
good speaker

Mike Mastro on 4/24/2009
great hint...

Jack Kress on 4/24/2009
Great information - pramater sniffing has been a problem for us in some SQL 2000 back ended applications that are moving to 2005, so this is valuable.

Jacob on 4/24/2009
It would have been nice to show the differences in the execution plans with different parameters. The author does mention different parameters could cause different execution plans but it's nice to see the differences and how Optimze For will force an execution plan.

JohnK on 4/27/2009
The obvious next question is what happens if the chosen optimize for value ID gets deleted from the table? BOL isn't much help. (In sql 2000, we resolved an occurrence of this by reassigning the input parameter variable to a local variable in the sproc.)

Plamen Ratchev on 4/28/2009
John, the value that is used in OPTIMIZE FOR is simply a hint for the optimizer to generate a plan. As long as other values have similar distribution they can all benefit from the plan. Even if the value is deleted the plan can be efficient for the other values. Using local variables is a technique that actually disables parameter sniffing (because the optimizer does not currently sniff local variables) and the generated plan is not optimized for the input parameter values.

Jonathan Winer on 4/30/2009
A bit hard to understand with the accent unfortunately. Also, during the beginning of the presentation, it would have been nice to see the different query plans resulting from parameter sniffing as he was explaining the issue.

Prasanna Prabhu on 5/1/2009
Nice Demo, very precise. If you could have demoed the same with multiple parameters, that would have given a picture about HOW TO use/define multiple parameters with "OPTION" part of the syntax (2nd last line of your code)

Plamen Ratchev on 5/2/2009
Prasanna, the syntax of OPTIMIZE FOR is: OPTIMIZE FOR ( @variable_name { UNKNOWN | = literal_constant } [ , ...n ] ). As you can see you simply need to list all variable values separated by a comma, like OPTION ( OPTIMIZE FOR (@var1 = 'abc', @var2 = ‘def’) )

Sinshith.S on 6/23/2009
Very good!!!!!!!!!!!1

The correct answer for the definition of parameter sniffing is poorly worded

Gökhan Gülbiz on 9/19/2011
May be more useful with showing metrics.

Cary on 9/19/2011
Elegantly explained. I also was not aware of the "optimize for" option. Thanks.

Alfred Kristinsson on 9/19/2011
Not really useful without functioning example

Margie on 9/19/2011
I am pretty new to SQL Server and really didn't follow what he was tring to say here.

Jacqueline Mornan on 9/19/2011
Could hardly understand what he was saying

Clyde Turner on 9/20/2011
Presenter was slightly hard to understand. Had to watch the video 5 or 6 times to get a full understanding.

Kevin on 9/23/2011
did not see the benefits demonstrated

Dave on 9/25/2011
Would have been nice to see the sp in action to see the benefits.

Maurice Ivory on 2/22/2012
I got it!!



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