x

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

Matt Whitfield gravatar image

Matt Whitfield ♦♦
29.4k 61 65 87

(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

Grant Fritchey gravatar image

Grant Fritchey ♦♦
98.9k 19 21 74

Brilliant answer - vote up :)
Oct 14, 2009 at 09:42 AM Matt Whitfield ♦♦
Thanks. I have had the chance to answer this question once or twice before.
Oct 14, 2009 at 11:52 AM Grant Fritchey ♦♦
OPTIMIZE FOR UNKNOWN -- kinda like my life mantra
Apr 21, 2010 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, 2010 at 10:58 PM Grant Fritchey ♦♦
(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.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

SQL Server Central

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

Topics:

x986
x73
x40

asked: Oct 14, 2009 at 08:31 AM

Seen: 2494 times

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