SQL Server Query Tuning



Similar documents
Understanding SQL Server Execution Plans. Klaus Aschenbrenner Independent SQL Server Consultant SQLpassion.at

Execution Plans: The Secret to Query Tuning Success. MagicPASS January 2015

Understanding Query Processing and Query Plans in SQL Server. Craig Freedman Software Design Engineer Microsoft SQL Server

Oracle Database 11g: SQL Tuning Workshop Release 2

Oracle Database 11g: SQL Tuning Workshop

FHE DEFINITIVE GUIDE. ^phihri^^lv JEFFREY GARBUS. Joe Celko. Alvin Chang. PLAMEN ratchev JONES & BARTLETT LEARN IN G. y ti rvrrtuttnrr i t i r

Monitoring, Tuning, and Configuration

MOC 20461C: Querying Microsoft SQL Server. Course Overview

The Database is Slow

Introduction. Part I: Finding Bottlenecks when Something s Wrong. Chapter 1: Performance Tuning 3

Oracle Database 12c: Introduction to SQL Ed 1.1

Query Performance Tuning: Start to Finish. Grant Fritchey

SQL Query Evaluation. Winter Lecture 23

How To Improve Performance In A Database

SQL Server 2012 Optimization, Performance Tuning and Troubleshooting

MS SQL Performance (Tuning) Best Practices:

SQL Server Query Tuning

Comp 5311 Database Management Systems. 16. Review 2 (Physical Level)

W I S E. SQL Server 2008/2008 R2 Advanced DBA Performance & WISE LTD.

Advanced Oracle SQL Tuning

SQL Server 200x Optimizing Stored Procedure Performance

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Oracle EXAM - 1Z Oracle Database 11g Release 2: SQL Tuning. Buy Full Product.

Optimizing Performance. Training Division New Delhi

PERFORMANCE TUNING FOR PEOPLESOFT APPLICATIONS

1Z0-117 Oracle Database 11g Release 2: SQL Tuning. Oracle

SQL Query Performance Tuning: Tips and Best Practices

Useful Business Analytics SQL operators and more Ajaykumar Gupte IBM

Oracle Database: SQL and PL/SQL Fundamentals

SQL Server Performance Tuning and Optimization

Oracle Database: SQL and PL/SQL Fundamentals NEW

Inside the SQL Server Query Optimizer

Performance rule violations usually result in increased CPU or I/O, time to fix the mistake, and ultimately, a cost to the business unit.

In this session, we use the table ZZTELE with approx. 115,000 records for the examples. The primary key is defined on the columns NAME,VORNAME,STR

Microsoft SQL Server: MS Performance Tuning and Optimization Digital

Introduction to Querying & Reporting with SQL Server

The Sins of SQL Programming that send the DB to Upgrade Purgatory Abel Macias. 1 Copyright 2011, Oracle and/or its affiliates. All rights reserved.

Best Practices for DB2 on z/os Performance

LearnFromGuru Polish your knowledge

Querying Microsoft SQL Server 2012

AV-005: Administering and Implementing a Data Warehouse with SQL Server 2014

DBMS / Business Intelligence, SQL Server

Oracle Database: SQL and PL/SQL Fundamentals

SQL Server Performance Tuning and Optimization. Plamen Ratchev Tangra, Inc.

Course ID#: W 35 Hrs. Course Content

Course 10774A: Querying Microsoft SQL Server 2012

Course 10774A: Querying Microsoft SQL Server 2012 Length: 5 Days Published: May 25, 2012 Language(s): English Audience(s): IT Professionals

David Dye. Extract, Transform, Load

In-memory Tables Technology overview and solutions

SQL Server 2012 Query. Performance Tuning. Grant Fritchey. Apress*

D B M G Data Base and Data Mining Group of Politecnico di Torino

DB Audit Expert 3.1. Performance Auditing Add-on Version 1.1 for Microsoft SQL Server 2000 & 2005

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

Topics Advanced PL/SQL, Integration with PROIV SuperLayer and use within Glovia

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Microsoft SQL Server performance tuning for Microsoft Dynamics NAV

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio

SQL Performance for a Big Data 22 Billion row data warehouse

Developing Microsoft SQL Server Databases 20464C; 5 Days

Chapter 13: Query Processing. Basic Steps in Query Processing

MS Designing and Optimizing Database Solutions with Microsoft SQL Server 2008

Querying Microsoft SQL Server

Oracle SQL. Course Summary. Duration. Objectives

Querying Microsoft SQL Server 20461C; 5 days

Performance Tuning for the Teradata Database

IBM DB2: LUW Performance Tuning and Monitoring for Single and Multiple Partition DBs

Performance Tuning and Optimizing SQL Databases 2016

Programa de Actualización Profesional ACTI Oracle Database 11g: SQL Tuning Workshop

SQL Server Execution Plans Second Edition

20464C: Developing Microsoft SQL Server Databases

Oracle Database 12c: SQL Tuning for Developers. Sobre o curso. Destinatários. Oracle - Linguagens. Nível: Avançado Duração: 18h

Querying Microsoft SQL Server Course M Day(s) 30:00 Hours

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Physical Design. Phases of database design. Physical design: Inputs.

SAP Business Objects Business Intelligence platform Document Version: 4.1 Support Package Data Federation Administration Tool Guide

<Insert Picture Here> Extending Hyperion BI with the Oracle BI Server

SQL Server Execution Plans Second Edition

DB2 for i5/os: Tuning for Performance

Developing Microsoft SQL Server Databases

Course 20464: Developing Microsoft SQL Server Databases

Query tuning by eliminating throwaway

SQL Server Execution Plans

Oracle Database 10g: Introduction to SQL

Course 20461C: Querying Microsoft SQL Server Duration: 35 hours

Course -Oracle 10g SQL (Exam Code IZ0-047) Session number Module Topics 1 Retrieving Data Using the SQL SELECT Statement

DB2 for Linux, UNIX, and Windows Performance Tuning and Monitoring Workshop

Oracle 10g PL/SQL Training

Microsoft SQL Database Administrator Certification

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Efficient Data Access and Data Integration Using Information Objects Mica J. Block

Seminar 5. MS SQL Server - Performance Tuning -

SQL Server 2008 Designing, Optimizing, and Maintaining a Database Session 1

Oracle Database: SQL and PL/SQL Fundamentals NEW

EZManage V4.0 Release Notes. Document revision 1.08 ( )

Querying Microsoft SQL Server 2012

Transcription:

SQL Server Query Tuning Klaus Aschenbrenner Independent SQL Server Consultant SQLpassion.at Twitter: @Aschenbrenner

About me Independent SQL Server Consultant International Speaker, Author Pro SQL Server 2008 Service Broker SQLpassion.at Twitter: @Aschenbrenner

SQL Server Performance & Troubleshooting Workshop 19. 21. Juni in Bern Gurten Agenda SQL Server Indexing SQL Server Locking & Blocking SQL Server Performance Monitoring & Troubleshooting Weitere Informationen http:///events.html

Agenda Basics of Query Execution Execution Plan Overview Index Tuning

Agenda Basics of Query Execution Execution Plan Overview Index Tuning

Execution of a query ad SQL Query Execution Break SQL into logical units such as keywords, expressions, operators, and identifiers. Produce sequence tree SQL SQL Execute Parse SQL Statement Normalize Execution Tree Optimize SQL Query Cache Execution Plan Allocate Memory Execute Return Data Data Binding: verify that the tables and columns exist and load the metadata for the tables and columns. Perform implicit data conversions (typecasting). Replace views with definitions. Perform simple syntax-based optimizations. Perform Trivial Optimization. Perform more syntactical transformations. If needed, perform full cost-based optimization. Execution Plan is produced here

Query Optimization Execution Plans and cost-based optimizations Optimization phases Indexes and distribution statistics Join Selection

Execution Plan Strategy determined by the Optimizer to access/manipulate data Can be influenced by the developer query hints Key decisions are made Which indexes to use? How to perform JOIN operations? How to order and group data? In what order tables should be processed? Can be cached plans reused?

Agenda Basics of Query Execution Execution Plan Overview Plan Cache Plan Caching Parameter Sniffing Recompilations

Why understanding Execution Plans? Insight into query execution/processing strategy Tune performance problems at the source Hardware is not every time the problem High CPU/IO consumption -> poorly tuned Execution Plans Understanding Execution Plans is a prerequisite to performance tuning!

Execution Plan Types 1/2 Estimated Execution Plan Created without ever running the query Uses statistics for estimation Good for long running query tuning Actual Execution Plan Created when the actual query runs Uses the real data They can be different Statistics out of date Estimated Execution Plan not valid any more

Execution Plan Types 2/2 Textual Execution Plan Depricated in further versions of SQL Server XML based Execution Plan Very good for further analysis Can be queried Graphic Execution Plan Uses internally the XML based Execution Plan

Reading Execution Plans SHOWPLAN permission needed Query execution is serial A series of sequential steps/operators Executed one after one Execution Plan displays these steps/ operators This is one step in the execution plan

Execution Plan Sample 4 2 3 1 Note: plan starts at [SalesOrderHeader] even though [Customer] is actually named first in query expression Right angle blue arrow in table access method icon represents full scan (bad) Stepped blue arrow in table access method icon represents index seek, but could be either a single row or a range of rows

Operator Properties Each operator has several properties with additional information

Common Properties Physical Operation Logical Operation Estimated I/O Cost Estimated CPU Cost Estimated Operator Cost Estimated Subtree Cost Estimated Number of Rows Estimated Row Size

Common Operators Data Retrieval Operators Table Scan (reads a whole table) Index Scan (reads a whole index) Index Seek (seeks into a index) Join Operators Nested Loop (outer loop, inner loop) Merge Join (needs sorted input) Hash Match (uses a hash table internally) Aggregation Operators Stream Aggregate Hash Aggregate

Nested Loop Join Start Fetch next row from input #1 Quit True No more rows? False Fetch matching rows from input #2

Nested Loop For each Row operator Takes output from one step and executes another operation for each output row Outer Loop, Inner Loop Only join type that supports inequality predicates

Merge Join Start Fetch next row from input #1 Quit True No more rows? False Fetch next row from input #2 True False Rows match?

Merge Join Needs at least one equijoin predicate Used when joined columns are indexed (sorted) Otherwise (expensive) sorting is needed Plan may include explicit sort

Start Fetch next row from input #1 Fetch next row from input #2 Hash Join No more rows? True True No more rows? False Quit False Apply Hash Function Apply Hash Function Place row in hash bucked Probe bucked for matching rows

Hash Join Needs at least one equijoin predicate Hashes values of join columns from one side (smaller table) Based on Statistics Probes them with the join columns of the other side (larger table) Uses hash buckets Stop and Go for the Probe Phase Needs memory grants to build the hash table If they exceed, Hash Join is spilled to TempDb Performance decreases!

Stream Aggreate Data must be sorted on the aggregation columns Processes groups one at a time Does not block or use memory Efficient if sort order is provided by index Plan may include explicit sort

Hash Aggregate Data need not be sorted Builds a hash table of all groups Stop and Go Same as Hash Join Needs memory grants to build the hash table If they exceed, Hash Aggregate is spilled to TempDb Performance decreases! General better for larger input sets

Capturing Execution Plans SQL Server Management Studio Development SQL Profiler Production Event: Performance/Showplan XML Dump Execution Plan to file sys.dm_exec_cached_plans Production

Demo Working with Execution Plans

Agenda Basics of Query Execution Execution Plan Overview Index Tuning Search Arguments Covering Index Tipping Point Index Intersection Index Joins Filtered Indexes Indexed Views

General SET STATISTICS IO ON/OFF Returns the IO page count SET STATISTICS TIME ON/OFF Returns the elapsed time Index improves read performance Index degrades write performance Index maintenance But it can also improve performance, because the record to be updated/deleted must be found in the first step (SELECT)

Search Arguments Can be part of WHERE clause JOIN clause Has major impact on Index usage Breaks performance Wrong usage can lead to SCANs instead of SEEKs Recommendations No functions/expressions on Search Arguments

Demo Search Arguments

Bookmark Lookup Occurs when query is not satisfied by a Covering Index Base table must be touched Index Page + Data Page access occurs 2 kinds of Bookmark Lookup Clustered Key Lookup (Clustered Table) RID Lookup (Heap Table) Deadlocks are possible Writer: X(CI) => X(NCI) Reader: S(NCI) => S(CI)

Covering Index Non-Clustered Index that satisfies a complete query Without touching the base table No access to the data page containing the data The following referred columns must be present in the Non-Clustered index SELECT WHERE Otherwise Traditional Bookmark Lookup

INCLUDE Property Includes columns in the leaf-pages of the index Columns are not included in the navigation hierarchy Navigation hierarchy is very small Benefits More elegant covering index More than 900 bytes per index possible More than 16 columns per index possible

Demo Covering Index

Tipping Point Defines whether to do a Bookmark Lookup or a Table/Clustered Index Scan Bookmark Lookups are only done when the Non- Clustered Index is selective enough Covering Index will not have a Tipping Point Lookups OK Bookmark Lookups... TP... Lookups too expensive Table/Clustered Index Scans 1/4 1/3 # of Pages

Tipping Point Sample 80.000 records per 4.000 bytes 12,5% - 16,7% records 80.000 records per 400 bytes 1,25% - 1,67% records 80.000 records per 40 bytes 0,125% - 0,167% records Doesn t depend on the number of records Depends on the size of the records!!!

Demo Tipping Point

Index Intersection SQL Server can use multiple indexes to execute a query Selecting small subsets of data Finally performing an intersection between those subsets Why No chance to modify existing indexes Already too much columns/bytes in the Non-Clustered Index Create multiple narrow indexes, instead of wide indexes Reuse through index intersection is possible!

Disadvantages Not the fastest option for performance More than 1 index must be accessed Join(s) necessary Hash Join needs (maybe) space in TempDb Not recommended for high priority queries

Demo Index Intersection

Filtered Index Specialized version of Non-Clustered Index WHERE clause Creates a highly selective set of keys Reduces the size (page count) of the Non-Clustered Index and therefore the query performance Decreased maintenance cost, because index is smaller E.g. Column with large amount of NULL values

Filtered Indexes - Prerequisites ANSI settings ON ANSI_NULLS ANSI_PADDING ANSI_WARNING ARITHABORT CONCAT_NULL_YIELDS_NULL QUOTED_IDENTIFIER ANSI settings OFF NUMERIC_ROUNDABORT

Demo Filtered Index

Indexed Views Views Stores only the SELECT statement (virtual table) Contains no data Data is retrieved by every call again and again Poor performance if you have to do a lot of calculations (like aggregations) Indexed Views Unique Clustered Index on a view The data in the view in materialized into the index Index is stored physically in the database file

Indexed Views - Restrictions First index must be a unique Clustered Index Non-Clustered Index after Clustered Index View definition must be deterministic No reference to other views Float columns can t be the Clustered Key View must be schema-bound

Indexed Views - Prerequisites ANSI settings ON ARITHABORT CONCAT_NULL_YIELDS_NULL QUOTED_IDENTIFIER ANSI_NULLS ANSI_PADDING ANSI_WARNING ANSI settings OFF NUMERIC_ROUNDABORT

Demo Indexed Views

Indexing for OR OR returns individual sets Ensures that rows that appears in multiple sets are only returned once IN is a simplified version of OR conditions Each column must be indexed Condition must be selective enough (regarding Tipping Point)

Demo Indexing for OR

Indexing for Aggregates 2 Types of Aggregates Stream Aggregate Needs sorted input on the aggregated columns Hash Aggregate Done otherwise, when input is not sorted Can be spilled out to TempDb Can lead to TempDb overhead/problems Aggregation on multiple columns All columns must be indexed through ONE index

Demo Indexing for Aggregates

Summary Basics of Query Execution Execution Plan Overview Index Tuning

SQL Server Performance & Troubleshooting Workshop 19. 21. Juni in Bern Gurten Agenda SQL Server Indexing SQL Server Locking & Blocking SQL Server Performance Monitoring & Troubleshooting Weitere Informationen http:///events.html