|
Site Seeder Question: I have heard about 'parameter sniffing' and heard that it can result in sub-optimal execution plans sometimes - what is parameter sniffing and why can it affect how a stored procedure or other compiled object executes?
(comments are locked)
|
|
Parameter sniffing is a process where SQL Server examines the values of parameters passed to stored procedures and uses those values along with the available statistics from an index or table to help determine an execution plan. When the statistics are up to date and an accurate representation of the types of data that will be passed to the query, parameter sniffing helps optimize queries better. Without parameter sniffing, average values from the statistics are used in place of specific values from the parameter. In some cases when parameter sniffing occurs, either the value of the parameter passed in, or the statistics themselves, is not representative of the values that will normally be passed. This results in a sub-optimal execution plan. In general parameter sniffing should be considered a plus to the performance of the system, but in those cases where it's not, various mechanisms can be used to avoid it. One of the simplest is to use local variables. You can also use the OPTIMIZE FOR query hint or the WITH RECOMPILE query hint. In extreme cases you can force specific execution plans onto procedures. Brilliant answer - vote up :)
Oct 14 '09 at 09:42 AM
Matt Whitfield ♦♦
Thanks. I have had the chance to answer this question once or twice before.
Oct 14 '09 at 11:52 AM
Grant Fritchey ♦♦
OPTIMIZE FOR UNKNOWN -- kinda like my life mantra
Apr 21 '10 at 09:15 PM
Scot Hauder
Yep. That was a nice addition to the toolbox, but I still shy from using query hints unless I've got a gun to my head.
Apr 21 '10 at 10:58 PM
Grant Fritchey ♦♦
(comments are locked)
|

