Sponsored by: Professional Association for SQL Server Bad plan! Sit! Gail Shaw.

Slides:



Advertisements
Similar presentations
SQL Server performance tuning basics
Advertisements

SQL Performance 2011/12 Joe Chang, SolidQ
Understanding Parameter Sniffing Benjamin Nevarez Blog: benjaminnevarez.com 1.
Virtual techdays INDIA │ 9-11 February 2011 SQL 2008 Query Tuning Praveen Srivatsa │ Principal SME – StudyDesk91 │ Director, AsthraSoft Consulting │ Microsoft.
Executing Explain Plans and Explaining Execution Plans Craig Martin 01/20/2011.
Troubleshooting SQL Server Enterprise Geodatabase Performance Issues
How a little code can help with support.. Chris Barba – Developer at Cimarex Energy Blog:
Denny Cherry Manager of Information Systems MVP, MCSA, MCDBA, MCTS, MCITP.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Srik Raghavan Principal Lead Program Manager Kevin Cox Principal Program Manager SESSION CODE: DAT206.
© IBM Corporation 2005 Informix User Forum 2005 John F. Miller III Explaining SQLEXPLAIN ®
How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira.
Maciej Pilecki | Project Botticelli Ltd.. SELECT Bio FROM Speakers WHERE FullName=‘Maciej Pilecki’;  Microsoft Certified Trainer since 2001  SQL Server.
Stored Procedure Optimization Preventing SP Time Out Delay Deadlocking More DiskReads By: Nix.
Gail Shaw XpertEase DAT 305 Topics Background Information Query Hints Plan Cache Metadata Plan Guides Plan Freezing Monitoring Plan Guide Use.
Meta Data Cardinality Explored CSSQLUG User Group - June 2009.
1 Chapter 8 Execution Plan Management. 2 Overview of Execution Plan Management Review techniques to – override optimizer – Improve optimizer’s decisions.
Module 9: Using Advanced Techniques. Considerations for Querying Data Working with Data Types Cursors and Set-Based Queries Dynamic SQL Maintaining Query.
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.
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
Execution Plans Detail From Zero to Hero İsmail Adar.
Why Should I Care About … The Plan Cache? Tuning When Stakeholders Won’t Say Where It Hurts.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP SQL SERVER Database Administration.
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Improve query performance with the new SQL Server 2016 query store!! Michelle Gutzait Principal Consultant at
This document is provided for informational purposes only and Microsoft makes no warranties, either express or implied, in this document. Information.
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.
Basic Outputs to Troubleshooting Queries.
SQL Server Performance Tuning
Joe Sack, Principal Program Manager, Microsoft
Parameter Sniffing in SQL Server Stored Procedures
Cleveland SQL Saturday Catch-All or Sometimes Queries
Query Optimization Techniques
Are You There, DBA? It’s Me, The App Developer.
Stored Procedures – Facts and Myths
Query Tuning without Production Data
Query Tuning without Production Data
Query Tuning without Production Data
Reading execution plans successfully
Reading Execution Plans Successfully
Optimizing Microsoft SQL Server 2008 Applications Using Table Valued Parameters, XML, and MERGE
Introduction to Execution Plans
Now where does THAT estimate come from?
Cardinality Estimator 2014/2016
Statistics What are the chances
Query Optimization Techniques
Statistics: What are they and How do I use them
When query plans go wrong
Reading Execution Plans Successfully
Hugo Kornelis Now where does THAT estimate come from? The nuts and bolts of cardinality estimation.
When I Use NOLOCK AND OTHER HINTS
Ascending Key Problem in SQL Server Large Tables
Statistics for beginners – In-Memory OLTP
Parameter Sniffing: the Good, the Bad, and the Ugly
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
“Magic numbers”, local variable and performance
Diving into Query Execution Plans
Query Profiling Options in SQL Server
Introduction to Execution Plans
Query Optimization Techniques
Introduction to Execution Plans
SSRS – Thinking Outside the Report
Analyzing Execution Plans
Presentation transcript:

Sponsored by: Professional Association for SQL Server Bad plan! Sit! Gail Shaw

Agenda What exactly is a bad execution plan? Symptoms Possible causes Options for fixing

What is a bad execution plan One that uses the wrong index? One that performs badly? One that uses the wrong joins? One that does table or index scans? One that performs erratically?

Symptoms Works fine today, bad tomorrow and nothing changed. Works fine for me, bad for my colleague Works fine one minute and bad the next. Works fine for some parameters, bad for others

Possible causes Parameter sniffing Stale statistics Particular query patterns Differing set options

Parameter sniffing Usually a good thing Allows better row estimations, hence better execution plan Sometimes has unwanted side effects Often a problem with data skew

Demo

Stale statistics Especially for larger tables Especially for indexes where data is added at the end

Demo

Query patterns Catch all queries Multiple execution paths Modifying parameter values

Example – Catch-All Query SELECT ProductID, ReferenceOrderID, TransactionType, Quantity, TransactionDate, ActualCost FROM Production.TransactionHistory WHERE (ProductID IS NULL) AND (ReferenceOrderID Is NULL) AND (TransactionType Is NULL) AND (Quantity is null)

Example – Multiple Execution Paths CREATE PROCEDURE MultipleExecPaths char(1) = NULL ) AS IS NULL SELECT max(transactionDate) from Production.TransactionHistory ELSE SELECT max(transactionDate) from Production.TransactionHistory WHERE TransactionType GO

Example – modifying Parameters CREATE PROCEDURE RecentOrders DATETIME = NULL ) AS IS NULL = '1900/01/01' SELECT OrderDate, DestinationCountry, SUM(ItemPrice) AS totalPrice, SUM(QuantityPurchased) AS totalPurchased FROM dbo.BookOrders AS bo INNER JOIN dbo.OrderDetails AS od ON bo.OrderID = od.OrderID WHERE OrderDate GROUP BY OrderDate, DestinationCountry

Tracking bad plans Symptoms Querying the plan cache Profiler events Extended events

Tracking via Symptoms Profiler or the query stats DMVs Queries that have massive ranges in IO, CPU and duration Can then be examined in Management Studio Must be run on a near-identical copy of the DB to be useful

Tracking via Plan Cache Often not practical The plans in the cache have no run-time information No actual row counts The plans will look good for the estimated row counts that are included

Tracking via Profiler There are two events that return the actual execution plan –Showplan Statistics Profile –Showplan XML Statistics Profile

Tracking via Extended Events Not a practical option at present There is no extended event that provides the execution plan with run-time information events-action-to-collect-actual-execution-plan

Fixing Parameter sniffing Local variables Recompile Optimise for hint

Fixing stale statistics Manual stats updates –Database-wide if there is time –Specific if only some tables exhibit the problem. Do not turn auto-update off without having a plan in place to replace it.

Fixing bad query patterns Don’t use them If you do need to, understand the effects Test to ensure that the effects are not detrimental

Last resort Query hints Plan guides Make sure you know exactly what the effects are before using one

The very last resort Plan forcing Does not disable the optimiser Plan must be a valid one

Resources Performance-related articles on my blog – server/performance/ server/performance/ Grant Fritchey –

Thank you to our sponsor Professional Association for SQL Server

May 11-13, Orlando, FL Oct 11-14, Seattle, WA Save 25%: Register by April 12 th Register by March31st: save 40% and have the chance to win a cruise to Alaska! “24HR11” code gets you $100 off