Query Tuning without Production Data

Slides:



Advertisements
Similar presentations
SQL Server performance tuning basics
Advertisements

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.
Query Optimization 3 Cost Estimation R&G, Chapters 12, 13, 14 Lecture 15.
SQL Server Query Optimizer Cost Formulas Joe Chang
Denny Cherry Manager of Information Systems MVP, MCSA, MCDBA, MCTS, MCITP.
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
Parallel Execution Plans Joe Chang
Query Optimizer Execution Plan Cost Model Joe Chang
Query Optimization CMPE 226 Database Systems By, Arjun Gangisetty
Computing & Information Sciences Kansas State University Wednesday, 08 Nov 2006CIS 560: Database System Concepts Lecture 32 of 42 Monday, 06 November 2006.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Copyright Sammamish Software Services All rights reserved. 1 Prog 140  SQL Server Performance Monitoring and Tuning.
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.
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
Joe Sack, Principal Program Manager, Microsoft
Query Optimization Techniques
Execution Planning for Success
Database Management System
Query Tuning without Production Data
Query Tuning without Production Data
Four Rules For Columnstore Query Performance
Introduction to Query Optimization
Introduction to Execution Plans
Evaluation of Relational Operations: Other Operations
Introduction to Database Systems
The Key to the Database Engine
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
Query Optimization CS 157B Ch. 14 Mien Siao.
In Memory OLTP Not Just for OLTP.
Introduction to reading execution plans
Statistics for beginners – In-Memory OLTP
Lecture 2- Query Processing (continued)
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
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
Evaluation of Relational Operations: Other Techniques
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 Database Administrator (Traditional/Operational/Production) Specialize in High-Availability, Disaster Recovery, and Automation User group leader of FairfieldPASS in Stamford, CT.

** 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

Limitations of this method 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.

Setting up the development environment

Demonstration Generate Scripts Vs. DBCC CLONEDATABASE Use your 2014 SP2 or 2016 SP1 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 db size of files. Modify ALTER DATABASE [AdventureWorks2014_clone] SET READ_WRITE Set to ALTER DATABASE [AdventureWorks2014_clone] SET READ_ONLY Instead of step 9, you can disable auto update stats. Modify ALTER DATABASE [AdventureWorks2014_clone] SET AUTO_UPDATE_STATISTICS ON Set to ALTER DATABASE [AdventureWorks2014_clone] SET AUTO_UPDATE_STATISTICS OF

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

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