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. 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?
Heavily Skewed data Parameter Sniffing the Good and the Bad

7 When can it be a bad thing?
Outlying values gets “sniffed” Parameter Sniffing the Good and the Bad

8 When can it be a bad thing?
Bad statistics (sort of) Parameter Sniffing the Good and the Bad

9 When can it be a bad thing?
Multipurpose Stored Procedures Parameter Sniffing the Good and the Bad

10 What are signs of bad parameter sniffing ?
Phantom Problems. Parameter Sniffing the Good and the Bad

11 What are signs of bad parameter sniffing ?
Random performance spikes. Parameter Sniffing the Good and the Bad

12 What are signs of bad parameter sniffing ?
X-Event: inaccurate_cardinality_estimate (use sparingly and do not run continuously). 12 | Parameter Sniffing the Good and the Bad

13 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 NEW: Query Store!!! 13 | Parameter Sniffing the Good and the Bad

14 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 14 | Parameter Sniffing the Good and the Bad

15 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. 15 | Parameter Sniffing the Good and the Bad

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

17 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. 17 | Parameter Sniffing the Good and the Bad

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

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

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

21 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. 21 | Parameter Sniffing the Good and the Bad

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

23 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. 23 | Parameter Sniffing the Good and the Bad

24 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

25 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. 25 | Parameter Sniffing the Good and the Bad

26 Query Store: SQL Server 2016!!!!
Query Store is an upcoming feature that is in SQL Server 2016. Already on Microsoft SQL Azure Database (MASD). Conor Cunningham - Intro to Query Store: 26 | Parameter Sniffing the Good and the Bad

27 The Ugly!!! Demo of a horribly written stored procedure and what bad parameter sniffing can do. Parameter Sniffing the Good and the Bad

28 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