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.
How a little code can help with support.. Chris Barba – Developer at Cimarex Energy Blog:
Designing with Procedures 1. Designing a Program with Procedures If the code for your program is going to be less than one page, normally don’t bother;
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.
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.
Improve query performance with the new SQL Server 2016 query store!! Michelle Gutzait Principal Consultant at
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?
You Inherited a Database Now What?
Are You There, DBA? It’s Me, The App Developer.
Stored Procedures – Facts and Myths
Query Tuning without Production Data
Solving the Hard Problems
Finding more space for your tight environment
Parameter Sniffing in SQL Server Stored Procedures
Query Tuning without Production Data
Isolation Levels Understanding Transaction Temper Tantrums
Introduction to Execution Plans
Weird Stuff I Saw While ... Supporting a Java Team
Decoding the Cardinality Estimator to Speed Up Queries
Third Party Tools for SQL Server
EXEC and sp_executesql An Ad Hoc Rally
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
Purpose, Pitfalls and Performance Implications
Batches, Transactions, & Errors
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
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.
Understanding Azure SQL DB Service Tiers
SQL Server Performance Tuning Nowadays
SQL Server Agent The Life Preserver for the Drowning DBA Lance Tidwell.
How to Use Parameters Like a Pro
Targeting Wait Statistics with Extended Events
Parameter Sniffing: the Good, the Bad, and the Ugly
Batches, Transactions, & Errors
You Inherited a Database Now What?
Introduction to Execution Plans
When I Use NOLOCK AND OTHER HINTS
Parameter Sniffing on SQL Server
Parameter Sniffing: the Good, the Bad, and the Ugly
Summit Nashville /3/2019 1:48 AM
Score a (row) goal and beat a query optimizer
Query Tuning Fundamentals
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
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. 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

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

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

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

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

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

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

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

DEMO: OPTIMIZE FOR <value> 13 | 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. 14 | Parameter Sniffing the Good and the Bad

DEMO: OPTION Recompile 15 | 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. 16 | Parameter Sniffing the Good and the Bad

DEMO: Plan Guide 17 | 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. 18 | 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. 20 | Parameter Sniffing the Good and the Bad

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). http://usergroup.tv/videos/query-store-a-new-sql-query-tuning-feature 21 | 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