Presentation is loading. Please wait.

Presentation is loading. Please wait.

Parameter Sniffing: the Good,the Bad, and the Ugly

Similar presentations


Presentation on theme: "Parameter Sniffing: the Good,the Bad, and the Ugly"— Presentation transcript:

1 Parameter Sniffing: the Good,the Bad, and the Ugly
Lance Tidwell

2 About Me SQL Server DBA – Data Services Engineer for TicketCity in Austin, TX Twitter: @Lance_LT Blog: lancetidwell.com 2 | Parameter Sniffing the Good and the Bad

3 Questions to be answered
What is parameter sniffing? Why is this a good thing? When can it be a bad thing? What are signs of bad parameter sniffing? What can I do to fix bad parameter sniffing? 3 | Parameter Sniffing the Good and the Bad

4 What is parameter sniffing?
When a parameterized query or stored procedure is compiled, the parameter values that are passed in are “sniffed” and used to create the best query plan for those specific values. 4 | Parameter Sniffing the Good and the Bad

5 Why is this a good thing? Creates the best query plan for a specific value instead of a using an average of the entire table. Builds reusable plans that can be used for different values of the parameters. More reusable plans means less recompilation time. In most cases parameter sniffing is a wonderful thing for you (until it is not). 5 | Parameter Sniffing the Good and the Bad

6 When can it be a bad thing?
Your data distribution is heavily skewed. When a plan gets recompiled and optimized for an outlying value. If your statistics are bad (can be a red herring). Multipurpose stored procedures. 6 | Parameter Sniffing the Good and the Bad

7 What are signs of bad parameter sniffing ?
Phantom Problems. Random performance spikes with no explanation. X-Event: inaccurate_cardinality_estimate (use sparingly and do not run continuously). 7 | Parameter Sniffing the Good and the Bad

8 Ways to fix bad parameter sniffing.
Local Variables OPTIMIZE FOR UNKNOWN (2008 at higher) OPTIMIZE FOR <value> OPTION Recompile Plan Guides Basic Querying Tuning Code with parameter sniffing in mind TraceFlag 4136 Future: Query Store!!! 8 | Parameter Sniffing the Good and the Bad

9 Local Variables Store parameters in local variables then pass the local variables to the query. Creates a generic plan based on averages of full tables. Using to fix bad parameter sniffing looks a bit silly. Better alternative OPTIMIZE FOR UNKNOWN 9 | Parameter Sniffing the Good and the Bad

10 OPTIMIZE FOR UNKNOWN Same results as local variables except easier to understand why it is in the code. Available on SQL Server 2008 and up. 10 | Parameter Sniffing the Good and the Bad

11 DEMO: Local Variables and OPTIMIZE FOR UNKNOWN
*DBCC FREEPROCCACHE is good for demos and testing on QA not so much for Prod. 11 | Parameter Sniffing the Good and the Bad

12 OPTIMIZE FOR <value>
Optimizes for a specific value. You can put in a value that gives you the best plan for the majority of executions. Don’t have to worry about outlying data giving a sub-optimal plan for the bulk of the calls. Data is always changing, so while a value might be good today, it might not be tomorrow or the next year. 12 | Parameter Sniffing the Good and the Bad

13 DEMO: OPTIMIZE FOR <value>
13 | Parameter Sniffing the Good and the Bad

14 OPTION Recompile Gets you the best specific plan for the parameter values that are passed in. Adds the overhead of recompilation for each call. 14 | Parameter Sniffing the Good and the Bad

15 DEMO: OPTION Recompile
15 | Parameter Sniffing the Good and the Bad

16 Plan Guides You can use the previous query hints without altering procedure. Good for fixing parameter sniffing in places where the procedures are untouchable. Hard to recognize they are being used unless you are looking for them. 16 | Parameter Sniffing the Good and the Bad

17 DEMO: Plan Guide 17 | Parameter Sniffing the Good and the Bad

18 Basic Query Tuning Make sure that some of the basic query tuning has been done before you get too crazy trying to fix bad parameter sniffing. Badly tuned queries can cause the effects of bad parameter sniffing to be amplified. 18 | Parameter Sniffing the Good and the Bad

19 Code with parameter sniffing in mind
Multi-Purpose Stored Procedures Kimberly Tripp’s PASS Summit 2014 session: DEALING WITH MULTIPURPOSE PROCS AND PSP THE RIGHT WAY! Pass TV Parameter Sniffing the Good and the Bad

20 TraceFlag 4136 This trace flag can turn off parameter sniffing across the entire SQL Server instance. Last resort. Very dangerous! Make sure you test across the entire instance thoroughly before deploying. 20 | Parameter Sniffing the Good and the Bad

21 Query Store Coming Soon!!!!
Query Store is an upcoming feature that is slated for the next version of SQL Server. Will probably first be seen on Microsoft SQL Azure Database (MASD). 21 | Parameter Sniffing the Good and the Bad

22 Thank You! Twitter: @Lance_LT
Blog : lancetidwell.com Parameter Sniffing the Good and the Bad


Download ppt "Parameter Sniffing: the Good,the Bad, and the Ugly"

Similar presentations


Ads by Google