Module 12: Optimizing Query Performance. Overview Introducing the Query Optimizer Tuning Performance Using SQL Utilities Using an Index to Cover a Query.

Slides:



Advertisements
Similar presentations
Understanding SQL Server Query Execution Plans
Advertisements

Module 3: Creating and Tuning Indexes. Planning Indexes Creating Indexes Optimizing Indexes.
Module 13: Performance Tuning. Overview Performance tuning methodologies Instance level Database level Application level Overview of tools and techniques.
EXECUTION PLANS By Nimesh Shah, Amit Bhawnani. Outline  What is execution plan  How are execution plans created  How to get an execution plan  Graphical.
Module 14: Analyzing Queries. Overview Queries That Use the AND Operator the OR Operator Join Operations.
Query Execution Professor: Dr T.Y. Lin Prepared by, Mudra Patel Class id: 113.
Query Execution Professor: Dr T.Y. Lin Prepared by, Mudra Patel Class id: 113.
Module 13: Optimizing Query Performance. Overview Introduction to the Query Optimizer Obtaining Execution Plan Information Using an Index to Cover a Query.
Virtual techdays INDIA │ 9-11 February 2011 SQL 2008 Query Tuning Praveen Srivatsa │ Principal SME – StudyDesk91 │ Director, AsthraSoft Consulting │ Microsoft.
Indexes Rose-Hulman Institute of Technology Curt Clifton.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 11 Database Performance Tuning and Query Optimization.
Query Execution Professor: Dr T.Y. Lin Prepared by, Mudra Patel Class id: 113.
Module 7: Creating and Maintaining Indexes. Overview Creating Indexes Creating Index Options Maintaining Indexes Introduction to Statistics Querying the.
8-1 Outline  Overview of Physical Database Design  File Structures  Query Optimization  Index Selection  Additional Choices in Physical Database Design.
Chapter 8 Physical Database Design. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Overview of Physical Database.
Access Path Selection in a Relation Database Management System (summarized in section 2)
Executing Explain Plans and Explaining Execution Plans Craig Martin 01/20/2011.
Practical Database Design and Tuning. Outline  Practical Database Design and Tuning Physical Database Design in Relational Databases An Overview of Database.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
Module 8 Improving Performance through Nonclustered Indexes.
Physical Database Design & Performance. Optimizing for Query Performance For DBs with high retrieval traffic as compared to maintenance traffic, optimizing.
Physical Database Design Chapter 6. Physical Design and implementation 1.Translate global logical data model for target DBMS  1.1Design base relations.
Denny Cherry Manager of Information Systems MVP, MCSA, MCDBA, MCTS, MCITP.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Module 4 Designing Databases for Optimal Performance.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
Module 5 Planning for SQL Server® 2008 R2 Indexing.
Module 8: Implementing Stored Procedures. Overview Implementing Stored Procedures Creating Parameterized Stored Procedures Working With Execution Plans.
Module 4 Designing and Implementing Views. Module Overview Introduction to Views Creating and Managing Views Performance Considerations for Views.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team Feb, SQL Server 2000.
Query Execution Section 15.1 Shweta Athalye CS257: Database Systems ID: 118 Section 1.
Query Optimizer Execution Plan Cost Model Joe Chang
Indexes and Views Unit 7.
SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.
Query Processing – Query Trees. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying.
Chapter 8 Physical Database Design. Outline Overview of Physical Database Design Inputs of Physical Database Design File Structures Query Optimization.
Chapter 5 Index and Clustering
Session 1 Module 1: Introduction to Data Integrity
1 Copyright © 2005, Oracle. All rights reserved. Following a Tuning Methodology.
Creating Indexes on Tables An index provides quick access to data in a table, based on the values in specified columns. A table can have more than one.
IMS 4212: Database Implementation 1 Dr. Lawrence West, Management Dept., University of Central Florida Physical Database Implementation—Topics.
1 Chapter 9 Tuning Table Access. 2 Overview Improve performance of access to single table Explain access methods – Full Table Scan – Index – Partition-level.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Module 14: Managing Transactions and Locks. Overview Introducing Transactions and Locks Managing Transactions Understanding SQL Server Locking Architecture.
Oracle9i Developer: PL/SQL Programming Chapter 11 Performance Tuning.
Dave LinkedIn
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.
Module 6: Creating and Maintaining Indexes. Overview Creating Indexes Understanding Index Creation Options Maintaining Indexes Introducing Statistics.
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Views / Session 3/ 1 of 40 Session 3 Module 5: Implementing Views Module 6: Managing Views.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Indexes By Adrienne Watt.
Module 4: Creating and Tuning Indexes
Database Performance Tuning and Query Optimization
Chapter 15 QUERY EXECUTION.
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Selected Topics: External Sorting, Join Algorithms, …
Dave Bland LinkedIn SQL Server Execution Plans: How to use them to find performance bottlenecks Dave Bland LinkedIn
Query Processing CSD305 Advanced Databases.
Overview of Query Evaluation
Chapter 11 Database Performance Tuning and Query Optimization
Diving into Query Execution Plans
Query Optimization Techniques
All about Indexes Gail Shaw.
Presentation transcript:

Module 12: Optimizing Query Performance

Overview Introducing the Query Optimizer Tuning Performance Using SQL Utilities Using an Index to Cover a Query Overriding the Query Optimizer Understanding Indexing Strategies and Guidelines

Lesson: Introducing the Query Optimizer Function of the Query Optimizer Cost-Based Optimization The Query Optimization Process Phases of Query Optimization Execution Plan Cache Management The Query Governor

Function of the Query Optimizer Determines the Most Efficient Execution Plan  Determines whether indexes exist and evaluating their usefulness  Determines which indexes or columns can be used  Determines how to process joins  Uses cost-based evaluation of alternatives  Creates column statistics Uses Additional Information Produces an Execution Plan

Cost-Based Optimization Limits the Number of Optimization Plans  Cost is estimated in terms of I/O and CPU cost Determines Query Processing Time  Use of physical operators and sequence of operations  Use of parallel and serial processing

Results Set The Query Optimization Process Standardization Process Compilation Data Access Routines Query Optimization Transact-SQL Parsing Process

Phases of Query Optimization Query Analysis  Identifies the search and join criteria of the query Index Selection  Determines whether an index or indexes exist  Assesses the usefulness of the index or indexes Join Selection  Evaluates which join strategy to use

Execution Plan Cache Management Memory Storage  One copy for all serial executions  Another copy for all parallel executions Execution Context  An existing execution plan is reused, if one exists  A new execution plan is generated, if one does not exist Recompilation  Changes in database cause execution plan to be inefficient or invalid

The Query Governor How Do You Prevent Long-Running Queries?  Use the query governor to specify an upper limit Connection Limits  Use the sp_configure stored procedure  Execute the SET QUERY_GOVERNOR_COST_LIMIT statement  Specify 0 to turn off the query governor sp_configure query governor cost limit value SET QUERY_GOVERNOR_COST_LIMIT value

Lesson: Tuning Performance Using SQL Utilities The STATISTICS Statement The SHOWPLAN_ALL and SHOWPLAN_TEXT Statements The Graphical Execution Plan Analysis of Graphical Execution Plan Output Potential Problem Areas in Execution Plans

StatementOutput Sample STATISTICS TIME SQL Server Execution Times: CPU time = 0 ms, elapsed time = 2 ms. STATISTICS PROFILE Rows Executes StmtText StmtId… SELECT * FROM [charge] 16 WHERE STATISTICS IO Table 'member'. Scan count 1, logical reads 23, physical reads 0, read-ahead reads 0. The STATISTICS Statement

The SHOWPLAN_ALL and SHOWPLAN_TEXT Statements Structure of the SHOWPLAN Statement Output  Returns information as a set of rows  Forms a hierarchical tree  Represents steps taken  Shows estimated values of how a query was optimized Details of the Execution Steps SHOWPLAN_ALL Returns Additional Information

The Graphical Execution Plan Steps Are Units of Work to Process a Query Sequence of Steps Is the Order in Which the Steps Are Processed Logical Operators Describe Relational Algebraic Operation Used to Process a Statement Physical Operators Describe Physical Implementation Algorithm Used to Process a Statement

Analysis of Graphical Execution Plan Output Query Plan SELECT Cost: 0% Bookmark Lookup Cost: 8% Hash Match Root… Cost 28% Member.corp_no Cost 9% Member.fname Cost: 10% Filter Cost: 0% Sequence of Steps

Potential Problem Areas in Execution Plans Problem AreasSolution Table Scans Add indexes to columns in the WHERE clause Ensure the WHERE clause is selective Bookmark Lookup Operator Adding or modifying a nonclustered index to cover all columns Hash Join Operations Adding an index to the Probe Input Sort Operations Adding an index on the column in an ORDER BY or GROUP BY clause

Lesson: Using an Index to Cover a Query Introduction to Indexes That Cover a Query Single-Page Navigation Partial-Scan Navigation Full-Scan Navigation Factors Affecting an Index’s Ability to Cover a Query Methods to Verify That an Index Is Used to Cover a Query Guidelines for Creating Indexes That Cover a Query

Introduction to Indexes That Cover a Query Only Nonclustered Indexes Cover Queries Indexes Must Contain All Columns Referenced in the Query No Data Page Access Is Required Indexed Views Can Pre-Aggregate Data Indexes That Cover Queries Retrieve Data Quickly

Index Pages Leaf Level (Key Value) Non-Leaf Level Single-Page Navigation Akers Barr Barreto Borba Budai Kim … … … … … … … … Lang Martin Morris Eric … … … … … … … … … Data Pages …… Akers Lang … … … … Kim Eric … … … … Akers … … Ganio … … Kim … … Jon … … Lang … … … … … … Eric … … … … … … SELECT lastname, firstname FROM member WHERE lastname = 'Hall' Ganio Hall Hartono Jones Jon Don Tommy Brannon David HallDon

Index Pages Non-Leaf Level Leaf Level (Key Value) Partial-Scan Navigation Akers Barr Barreto Borba Budai … … … … … … … … … … Ganio Hall Hartono Jones … … … … … … … … … … Moyer Nash Nay Owen Russo … … … … … … … … … … Chai Con Cox Danner … … … … … … … … … … Dubois Duffy Fine Fort Funk … … … … … … … … … … Jordan Kim Koch … … … … … … … … … … Lang Martin Morris … … … … … … … … … … Smith … … … … … … … … … … Akers Chai Dubois Ganio … … … … … … … … Jordan Lang Moyer Smith … … … … … … … … Data Pages Akers Jordan … … … … … … … … USE credit SELECT lastname, firstname FROM member WHERE lastname BETWEEN 'Funk' AND 'Lang'

Index Pages Leaf Level (Key Value) Non-Leaf Level Data Pages Full-Scan Navigation Akers Barr Barreto Borba Budai … … … … … … … … … … Ganio Hall Hartono Jones … … … … … … … … … … Moyer Nash Nay Owen Russo … … … … … … … … … … Martin Smith … … Akers Ganio … … Akers … … Martin Chai Con Cox Danner … … … … … … … … … … Dubois Duffy Fine Fort Funk … … … … … … … … … … Jordan Kim Koch … … … … … … … … … … Lang Martin Morris … … … … … … … … … … Smith … … … … … … … … … … … USE credit SELECT lastname, firstname FROM member

Factors Affecting an Index’s Ability to Cover a Query All Necessary Data Must Be in the Index A Composite Index Is Useful Even if the First Column Is Not Referenced A WHERE Clause Is Not Necessary A Nonclustered Index Can Be Used if It Requires Less I/O Than a Clustered Index Containing a Column Referenced in the WHERE Clause Indexes Can Be Joined to Cover a Query

Methods to Verify That an Index Is Used to Cover a Query Execution Plan Output  Displays the phrase “Scanning a non-clustered index entirely or only a range”  No Bookmark Lookup operator I/O Comparison  Nonclustered index Total number of levels in the non–leaf level Total number of pages that make up the leaf level Total number of rows per leaf-level page Total number of rows per data page  Total number of pages that make up the table

Guidelines for Creating Indexes That Cover a Query Add Columns to Indexes Minimize Index Key Size Maintain Row-to-Key Size Ratio

Lesson: Overriding the Query Optimizer Hints and SET FORCEPLAN Statement Guidelines for Maintaining Overridden Queries Alternatives to Overriding the Query Optimizer

Hints and SET FORCEPLAN Statement Table Hints Join Hints Query Hints SET FORCEPLAN Statement

Guidelines for Maintaining Overridden Queries Verify That Performance Improves Document Reasons for Using Optimizer Hints Retest Queries Regularly

Alternatives to Overriding the Query Optimizer Limit Optimizer Hints Explore Other Alternatives Before Overriding the Query Optimizer by:  Updating statistics  Recompiling stored procedures  Reviewing the queries or search arguments  Evaluating the possibility of building different indexes

Lesson: Understanding Indexing Strategies and Guidelines I/O Consideration and Evaluation Indexing Strategy for Multiple Queries Guidelines for Creating Indexes Best Practices

I/O Consideration and Evaluation SELECT charge_no FROM charge WHERE charge_amt BETWEEN 20 AND 30 Access method Approx Page I/O Table scan 10,417 Clustered index on the charge_amt column 1,042 Nonclustered index on the charge_amt column 100,273 Composite index on charge_amt, charge_no columns 273

Example 2 Indexing Strategy for Multiple Queries USE credit SELECT charge_no, charge_dt, charge_amt FROM charge WHERE statement_no = AND member_no = 3852 USE credit SELECT charge_no, charge_dt, charge_amt FROM charge WHERE statement_no = AND member_no = 3852 USE credit SELECT member_no, charge_no, charge_amt FROM charge WHERE charge_dt BETWEEN '07/30/1999' AND '07/31/1999' AND member_no = 9331 USE credit SELECT member_no, charge_no, charge_amt FROM charge WHERE charge_dt BETWEEN '07/30/1999' AND '07/31/1999' AND member_no = 9331 Example 1

Guidelines for Creating Indexes Determine the Priorities of All of the Queries Determine the Selectivity for Each Portion of the WHERE Clause of Each Query Determine Whether to Create an Index Identify the Columns That Should Be Indexed Determine the Best Column Order of Composite Indexes Determine What Other Indexes Are Necessary Test the Performance of the Queries

Use the Query Governor to Prevent Long-Running Queries from Consuming System Resources Have a Thorough Understanding of the Data and How Queries Gain Access to Data Create Indexes That Cover the Most Frequently Used Queries Establish Indexing Strategies for Individual and Multiple Queries Avoid Overriding the Query Optimizer Best Practices

Lab A: Optimizing Query Performance Exercise 1: Using the Graphical Execution Plan to Determine How a Query Is Resolved Exercise 2: Comparing I/O for Queries That Are Covered or Not Covered by Indexes Exercise 3: Comparing I/O for Queries That Retrieve a Range of Data If Time Permits: Using Optimizer Hints to Force the Use of an Index or Join

Course Evaluation