What is parameter sniffing and how can it affect execution plans?

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?

more ▼

asked Oct 14, 2009 at 08:31 AM in Default

avatar image

Matt Whitfield ♦♦
29.5k 62 66 88

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Oct 14, 2009 at 08:49 AM

avatar image

Grant Fritchey ♦♦
137k 20 47 81

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Oct 14, 2009 at 08:31 AM

Seen: 2838 times

Last Updated: Apr 21, 2010 at 05:38 PM

Copyright 2018 Redgate Software. Privacy Policy