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

Slides:



Advertisements
Similar presentations
Understanding Parameter Sniffing Benjamin Nevarez Blog: benjaminnevarez.com 1.
Advertisements

Sponsored by: Professional Association for SQL Server Bad plan! Sit! Gail Shaw.
Tempdb Parasites Jason Hall-Dir. of Client SQL Sentry Blog-jasonhall.blogs.sqlsentry.net.
How a little code can help with support.. Chris Barba – Developer at Cimarex Energy Blog:
ADAPTING YOUR ETL SOLUTION TO USE SSIS 2012 Presentation by Devin Knight
How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira.
DAT410 SQL Server 2005 Optimizing Procedural Code Kimberly L. Tripp President/Founder, SQLskills.com.
Meta Data Cardinality Explored CSSQLUG User Group - June 2009.
How to kill SQL Server Performance Håkan Winther.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP,MCP. SQL SERVER Database Administration.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP SQL SERVER Database Administration.
High Performance Functions SQLBits VI. Going backwards is faster than going forwards.
Session Name Pelin ATICI SQL Premier Field Engineer.
SQL Server Magic Buttons! What are Trace Flags and why should I care? Steinar Andersen, SQL Service Nordic AB Thanks to Thomas Kejser for peer-reviewing.
SQL Server Statistics and its relationship with Query Optimizer
Data Virtualization Demoette… Packaged Query Single Select Option
Parameter Sniffing in SQL Server Stored Procedures
What Is The SSIS Catalog and Why Do I Care?
Query Optimization Techniques
Are You There, DBA? It’s Me, The App Developer.
Stored Procedures – Facts and Myths
Query Tuning without Production Data
PowerShell is Happening FOR REAL THIS TIME!!!
Solving the Hard Problems
Parameter Sniffing in SQL Server Stored Procedures
Query Tuning without Production Data
Query Tuning without Production Data
Isolation Levels Understanding Transaction Temper Tantrums
Introduction to Execution Plans
Weird Stuff I Saw While ... Supporting a Java Team
Discovering SSRS 2016 in Azure: Dataset to Deployment
Decoding the Cardinality Estimator to Speed Up Queries
Third Party Tools for SQL Server
EXEC and sp_executesql An Ad Hoc Rally
The Ins and Outs of Indexes
Now where does THAT estimate come from?
Cardinality Estimator 2014/2016
5 WAYS TO BYPASS *OR ENSURE* SQL SERVER SECURITY MATT MARTIN
What’s new in SQL Server 2016 Availability Groups
Query Optimization Techniques
Batches, Transactions, & Errors
Plan cache performance tuning
Statistics: What are they and How do I use them
When query plans go wrong
Hugo Kornelis Now where does THAT estimate come from? The nuts and bolts of cardinality estimation.
SQL Server Agent The Life Preserver for the Drowning DBA Lance Tidwell.
Transact SQL Performance Tips
How to Use Parameters Like a Pro
Targeting Wait Statistics with Extended Events
Batches, Transactions, & Errors
Four Rules For Columnstore Query Performance
Introduction to Execution Plans
Parameter Sniffing: the Good,the Bad, and the Ugly
When I Use NOLOCK AND OTHER HINTS
Parameter Sniffing on SQL Server
Parameter Sniffing: the Good, the Bad, and the Ugly
Query Tuning Fundamentals
Sioux Falls, SD | Hosted by (605) SQL
The Ins and Outs of Indexes
SQL Server Query Design and Optimization Recommendations
Introduction to Execution Plans
Query Optimization Techniques
The Complete Guide to Temporary Tables and Table Variables
Isolation Levels Understanding Transaction Temper Tantrums
Introduction to Execution Plans
Why should I care about SQL, if I have ORM?
The Complete Guide to Temporary Tables and Table Variables
Vendor Software Lessons From Consulting Vendor Software.
The Ins and Outs of Indexes
XML? What’s this doing in my database? Adam Koehler
Presentation transcript:

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

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

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

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

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

When can it be a bad thing? Heavily Skewed data Parameter Sniffing the Good and the Bad

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 http://www.sqlpass.org/summit/2014/PASStv.aspx?watch=p-6ii2NiUI0 Parameter Sniffing the Good and the Bad

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

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: http://usergroup.tv/videos/query-store-a-new-sql-query-tuning-feature 26 | Parameter Sniffing the Good and the Bad

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

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