Query Tuning without Production Data

Slides:



Advertisements
Similar presentations
Understanding SQL Server Query Execution Plans
Advertisements

SQL Server performance tuning basics
SQL Performance 2011/12 Joe Chang, SolidQ
CS 540 Database Management Systems
Dos and don’ts of Columnstore indexes The basis of xVelocity in-memory technology What’s it all about The compression methods (RLE / Dictionary encoding)
Virtual techdays INDIA │ 9-11 February 2011 SQL 2008 Query Tuning Praveen Srivatsa │ Principal SME – StudyDesk91 │ Director, AsthraSoft Consulting │ Microsoft.
SQL Server Query Optimizer Cost Formulas Joe Chang
Convergence /20/2017 © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
DBSQL 14-1 Copyright © Genetic Computer School 2009 Chapter 14 Microsoft SQL Server.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
1 Chapter 7 Optimizing the Optimizer. 2 The Oracle Optimizer is… About query optimization Is a sophisticated set of algorithms Choosing the fastest approach.
Primary Key, Cluster Key & Identity Loop, Hash & Merge Joins Joe Chang
Query Optimizer Execution Plan Cost Model Joe Chang
Query Processing – Implementing Set Operations and Joins Chap. 19.
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.
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.
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.
Architecting Availability Groups An analysis of Microsoft SQL Server Always-On Availability Group architectures 1.
SQL Server Statistics and its relationship with Query Optimizer
Joe Sack, Principal Program Manager, Microsoft
ASE Optdiag Features including dynamic_histogram
Query Optimization Techniques
Execution Planning for Success
Stored Procedures – Facts and Myths
Database Management System
Query Tuning without Production Data
Query Tuning without Production Data
Reading execution plans successfully
Four Rules For Columnstore Query Performance
Introduction to Query Optimization
Introduction to Execution Plans
Evaluation of Relational Operations: Other Operations
The Key to the Database Engine
SQL Server 2017 has more cool features than just running on Linux
Now where does THAT estimate come from?
Cardinality Estimator 2014/2016
Physical Join Operators
Statistics What are the chances
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Execution Plans Demystified
Deep Dive into Adaptive Query Processing
Statistics: What are they and How do I use them
Steve Hood SimpleSQLServer.com
SQL Server 2016 Execution Plan Analysis Liviu Ieran
Hugo Kornelis Now where does THAT estimate come from? The nuts and bolts of cardinality estimation.
SQL Server Query Plans Journeyman and Beyond
In Memory OLTP Not Just for OLTP.
Introduction to reading execution plans
Statistics for beginners – In-Memory OLTP
Parameter Sniffing: the Good, the Bad, and the Ugly
Dave Bland LinkedIn SQL Server Execution Plans: How to use them to find performance bottlenecks Dave Bland LinkedIn
Four Rules For Columnstore Query Performance
Introduction to Execution Plans
Parameter Sniffing: the Good, the Bad, and the Ugly
CS222P: Principles of Data Management Notes #13 Set operations, Aggregation, Query Plans Instructor: Chen Li.
Query Tuning Fundamentals
Evaluation of Relational Operations: Other Techniques
Diving into Query Execution Plans
In Memory OLTP Not Just for OLTP.
Introduction to Execution Plans
Reading execution plans successfully
Introduction to Execution Plans
Presentation transcript:

Query Tuning without Production Data Focusing on execution plan patterns and faking out the optimizer

Derik Hammer @sqlhammer derik@sqlhammer.com www.sqlhammer.com Operational Database Administrator Spent a year pretending to be a .NET developer then back to being a DBA Specialize in High-Availability, Disaster Recovery, and Automation / Continuous Integration Member of the #sqlfamily Chapter leader of FairfieldPASS in Stamford, CT. BS in Computer Information Systems with a focus in Database Management Querying Microsoft SQL Server 2012 Databases (70-461) Administering Microsoft SQL Server 2012 Databases (70-462)

Materials Slide deck and demo material available at: This deck http://www.sqlhammer.com/presentation-query-tuning-without-production- data/ All presentations http://www.sqlhammer.com/community/ This material has already been posted. When I update the material, the most recent updates will be available. This slide will be shown again at the end of the session if you want to write it down.

** 3 min

Why do we tune our queries? To meet or exceed user expectations To efficiently use system resources Expectations: 1. As a user I want to click SAVE and wait no longer than 4 seconds before receiving confirmation. 2. There are 6 commands which make up the SAVE operation. 3. The sum of the elapsed time of all 6 commands must take less than 4 seconds, under the worst scenarios. Resources: CPU / Memory / Storage sub-system I/O

Goals Answer these questions How do I tune a query without production quantity data? How do I tune a query without production quality hardware? Demonstrate How to setup your development database Query anti-patterns to look for when tuning

How will we get there? Make the optimizer think its row counts and data skew matches production. Make the optimizer think that the hardware matches production. Tune based on compiled execution plan instead of elapsed time / actual IO work load

Setting up the development environment

The plan Create a database which matches in schema only, no data. An existing development database with non- production data can be used as well. Copy statistics from production into development. Either disable automatic statistics updates or set the database to read only mode.

Demonstration Generate Scripts Vs. DBCC CLONEDATABASE Use your 2014 SP2 instance. DBCC CLONEDATABASE ('AdventureWorks2014','AdventureWorks2014_clone') OR Documented since 2005 > https://support.microsoft.com/en-us/kb/914288 Right-click db Tasks Generate scripts Script entire db and all db objs Advanced Enable ANSI Padding Cont. scripting on err Include system constraint names Script bindings Script collation Script for whatever edt. you are using. Scripts stats to include stats and histograms Schema only Triggers Next > Next > Finish Open script. Modify ALTER DATABASE [dba_stackexchange_empty] SET READ_WRITE Set to ALTER DATABASE [dba_stackexchange_empty] SET READ_ONLY Instead of step 9, you can disable auto update stats. Modify ALTER DATABASE [dba_stackexchange_empty] SET AUTO_UPDATE_STATISTICS ON Set to ALTER DATABASE [dba_stackexchange_empty] SET AUTO_UPDATE_STATISTICS OFF

Fake hardware with DBCC OPTIMIZER_WHATIF Hardcode values for the optimizer to work from. Can modify: Effective core count Physical memory Platform (32-bit vs. 64-bit) Session scoped. Undocumented DBCC command.

DBCC OPTIMIZER_WHATIF Demonstration DBCC OPTIMIZER_WHATIF Walk through comments and code in 1-OPTIMIZER_WHATIF

Execution Plan Tuning and Anti-Patterns

What is an execution plan? Pre-compiled plan of execution. Can be viewed graphically. Can be estimated or actual. Is based on schema and statistics. For our demos the estimated plan will work because the actual results would be based on an unrealistic work load. Tells much truth and many lies.

Process more efficiently. Goals of query tuning Consume less data. Process less data. Process more efficiently. Consume less data Indexes Selective filters Process less data Less iterations over the data. Fewer nested loops No table spooling Fewer rewinds Fewer sorts Process more efficiently Avoid blocking operators Hash match instead of a large sort followed by a merge join Proper memory grants to avoid table spills

Sorts ORDER BY TOP N Sort MERGE JOIN Expensive operator Needs to fit entire sort in memory grant or else it will spill to tempdb Blocking operation

Blocking Operator: Sort Avoiding sorts satisfies the process less data and process data more efficiently tuning goals.

Demonstration Sorts Walk through comments and code in 2-Sorts.

Residual Predicates Hidden index scans. Varying degrees of deception. Confuses the meaning of a covering index. Can increase storage I/O by orders of magnitude. Can be inside: Index seeks MERGE JOINs HASH MATCH joins

Demonstration Residual Predicates Walk through comments and code in 3-ResidualPredicates.sql.

Compute Scalar Used to evaluate expressions and scalar values. A scalar value is a single value like an integer or float rather than a data structure like a tuple. Optimizer almost always shows them as near 0 costs. Can prevent an execution plan from going parallel. Inline Table-Valued Functions are the exception. Most are inexpensive and inconsequential. Some are rather expensive.

Demonstration Compute Scalar Walk through comments and code in 4-Compute-Scalar.sql.

Nested Loops Also Known As…

Nested Loops Look out for expensive operations on the inner loop. RBAR: Row by agonizing row. Look out for expensive operations on the inner loop. SORTs Scans Residual Predicates Can be caused by skewed data and parameter sniffing. Parameter sniffing can be tested by loading data related to a couple of entities with different data sizes. Can be caused by bad cardinality estimates. Multi-statement table-valued functions. Table variables. Great for small data sets, bad for large sets. SQL Server is really smart but it also tries to be really fast. What do we get when an intelligent person is rushed? You get mistakes. If you want to load some data you can take a couple of entities with different data skew.

Demonstration Nested Loops Run through the comments on 5-Nested-Loops.sql

What did we learn? No data needed Fake it till you make it, with hardware Learn about execution plans Learn about parameter sniffing

Limitations Cannot definitively validate cardinality estimates. Cannot tune for concurrency because a simulated work load is not possible. Any queries executed would complete instantly because there is no data. Cannot trace work loads which are invisible to the optimizer. Table valued user-defined functions. Scalar user-defined functions Remote queries.

Materials Slide deck and demo material available at: This deck http://www.sqlhammer.com/presentation-query-tuning- without-production-data/ All presentations http://www.sqlhammer.com/community/ This material has already been posted. When I update the material, the most recent updates will be available. My Contact Information: @SQLHammer derik@sqlhammer.com www.sqlhammer.com