Download presentation
Presentation is loading. Please wait.
Published byThomas Dalton Modified over 9 years ago
1
Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000
2
2 Agenda Introduction to Optimizing Queries Index Strategies Query Plan Analysis
3
3 Overview Introduction to the Query Optimizer Obtaining Query Plan Information Indexing Fundamentals Introduction to Statistics
4
4 Function of the Query Optimizer Determines the Most Efficient Query Plan Determining whether indexes exist and evaluating their usefulness Determining which indexes or columns can be used Determining how to process joins Using cost-based evaluation Creating column statistics Produces a Query Plan Uses Available Information
5
5 How the Query Optimizer Uses 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
6
6 How the Query Optimizer Works Parsing Process Standardization Process Query Optimization Compilation Database Access Routines Transact-SQL Results Set Results Set
7
7 Query Optimization Phases 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
8
8 Caching Query Plan Storing a Query Plan in Memory One copy for all serial executions Another copy for all parallel executions Using an Execution Context An existing query plan is reused, if one exists A new query plan is generated, if one does not exist Recompiling Query Plans Changes in database cause query plan to be inefficient or invalid
9
9 Obtaining Query Plan InformationObtaining Query Plan Information Querying the sysindexes Table Viewing STATISTIC Statements Output Viewing SHOWPLAN_ALL and SHOWPLAN_TEXT Output Viewing Graphical Showplan
10
10 Querying the sysindexes Table Stores Table and Index Information Type of index (indid) Space used (dpages, reserved, and used) Fill factor (OrigFillFactor) Stores Statistics for Each Index
11
11 Viewing SHOWPLAN_ALL and SHOWPLAN_TEXT Output Structure of the SHOWPLAN Statement Output Returns information as a set of rows Forms a hierarchical tree Represents steps taken by the query optimizer Shows estimated values of how a query was optimized, not the actual query plan Details of the Execution Steps Difference Between SHOWPLAN_TEXT and SHOWPLAN_ALL Output
12
12 Viewing Graphical Showplan Viewing Graphical Showplan Elements of Graphical Showplan Reading Graphical Query Plan Output
13
13 Elements of Graphical Showplan 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
14
14 Reading Graphical Query 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 Index Seek Scanning a particular range of rows from a non-clustered index. Index Seek Scanning a particular range of rows from a non-clustered index. Physical operation: Logical operation: Row count: Estimated row sizes: I/O cost: CPU cost: Number of executes: Cost: Subtree cost: Index Seek 414 24 0.00706 0.000605 1.0 0.007675(6%) 0.00767 Argument: OBJECT: ([credit].[dbo].[member].[fname]), SEEK: ([member],[firstname] >=‘Rb’ AND [member],[firstname] <‘T’) ORDERED
15
15 Indexing Fundamentals Indexing Fundamentals Understanding the Data Limiting a Search Determining Selectivity Determining Density Determining Distribution of Data
16
16 Determining Selectivity High selectivity member_nomember_no 1 1 2 2.... last_namelast_namefirst_namefirst_name Randall Flood Joshua Kathie.. 10000 Anderson Bill SELECT * FROM member WHERE member_no > 8999 1000 10000 = 10% Number of rows meeting criteria Total number of rows in table = Low selectivity member_nomember_no 1 1 2 2.... last_namelast_namefirst_namefirst_name Randall Flood Joshua Kathie.. 10000 Anderson Bill SELECT * FROM member WHERE member_no < 9001 9000 10000 = 90% Number of rows meeting criteria Total number of rows in table =
17
17 Determining Density last_namelast_namefirst_namefirst_name Randall...... Joshua Randall Cynthia Randall Tristan...... Ota Lani...... SELECT * FROM member WHERE last_name = ‘Ota’ Low Density SELECT * FROM member WHERE last_name = ‘Randall’ High Density
18
18 Determining Distribution of Data Standard Distribution of Values F - JA - EK - OP - UV - Z Last Name Number of Last Names Even Distribution of Values Last Name Number of Last Names C - FA - BG - K L - NO - Z
19
19 Updating Statistics Frequency of Updating Statistics Automatically Updating Statistics Manually Updating Statistics If you created an index before any data was put into the table If a table is truncated If you added many rows to a table that contained minimal or no data, and you plan to immediately query against that table
20
20 Viewing Statistics SQL Server Returns Information on: The Time When the Statistics Were Last Updated The Number of Rows Sampled to Produce the Histogram Density Information Average Key Length Contents of the statsblob Column DBCC SHOW_STATISTICS
21
21 Overview Accessing Data Using an Index to Cover a Query Using Index Tuning Tools to Improve Query Performance Indexing Strategies
22
22 Accessing Data Accessing Data Table Scans and Indexes Index Architecture and Navigation Using Row Identifiers to Access Data
23
23 Table Scans and Indexes Indexes Use Key Values to Locate Data Table Scans Access Every Page Data Pages … … … … Index Pages
24
24 Index Architecture and Navigation Clustered Index Nonclustered Index Index Pages Non-Leaf Level Leaf Level (Key Value) Data Pages Leaf Level Index Pages Non-Leaf Level Data Pages
25
25 Using Row Identifiers to Access Data Creation and Storage of a RID Consists of the file number, page number, and slot number, which identify the exact location of a row Is an internal value RID Is Used to Retrieve Rows Accessing the nonclustered index Performing a Bookmark Lookup operation
26
26 Using an Index to Cover a Query Using an Index to Cover a Query Introduction to Indexes That Cover a Query Locating Data by Using Indexes That Cover a Query Identifying Whether an Index Can Be Used to Cover a Query Determining Whether an Index Is Used to Cover a Query Guidelines to Creating Indexes That Can Cover a Query
27
27 Locating Data by Using Indexes That Cover a Query Example of Single Page Navigation Example of Partial Scan Navigation Example of Full Scan Navigation
28
28 Example of Single Page Navigation Index Pages Non-Leaf Level Non-Leaf Level Leaf Level (Key Value) SELECT lastname, firstname FROM member WHERE lastname = 'Hall' SELECT lastname, firstname FROM member WHERE lastname = 'Hall' Akhtar Barr Borm Buhl … … … … … … … … … … Ganio Hall Hart Jones … … … … … … … … … … Hall… Morgan Nash Nay Ota Rudd … … … … … … … … … … Martin Smith … … Akhtar Ganio … … Akhtar … … Martin Chai Con Cox Dale … … … … … … … … … … Dunn Fine Fort Funk … … … … … … … … … … Jordan Kim Koch … … … … … … … … … … Lang Martin Moris … … … … … … … … … … Smith … … … … … … … … … … … Data Pages
29
29 Example of Partial Scan Navigation Index Pages Non-Leaf Level Non-Leaf Level Leaf Level (Key Value) Akhtar Barr Borm Buhl … … … … … … … … … … Ganio Hall Hart Jones … … … … … … … … … … Morgan Nash Nay Ota Rudd … … … … … … … … … … Martin Smith … … Akhtar Ganio … … Akhtar … … Martin Chai Con Cox Dale … … … … … … … … … … Dunn Fine Fort Funk … … … … … … … … … … Jordan Kim Koch … … … … … … … … … … Lang Martin Moris … … … … … … … … … … Smith … … … … … … … … … … Data Pages USE credit SELECT lastname, firstname FROM member WHERE lastname BETWEEN 'Funk' AND 'Lang' USE credit SELECT lastname, firstname FROM member WHERE lastname BETWEEN 'Funk' AND 'Lang'
30
30 Example of Full Scan Navigation Index Pages Non-Leaf Level Non-Leaf Level Leaf Level (Key Value) Akhtar Barr Borm Buhl … … … … … … … … … … Ganio Hall Hart Jones … … … … … … … … … … Morgan Nash Nay Ota Rudd … … … … … … … … … … Martin Smith … … Akhtar Ganio … … Akhtar … … Martin Chai Con Cox Dale … … … … … … … … … … Dunn Fine Fort Funk … … … … … … … … … … Jordan Kim Koch … … … … … … … … … … Lang Martin Moris … … … … … … … … … … Smith … … … … … … … … … … … Data Pages USE credit SELECT lastname, firstname FROM member USE credit SELECT lastname, firstname FROM member
31
31 Identifying Whether an Index Can Be Used to Cover a Query All Data Can Be Found in the Index First Column of a Composite Index Is Not Referenced in the WHERE Clause A WHERE Clause Does Not Exist There Is a Clustered Index on a Column Referenced in the WHERE Clause, and Selectivity Requires More I/O to Use the Clustered Index Join Operations Exist
32
32 Determining Whether an Index Is Used to Cover a Query Observing the Query Plan Output Displays the phrase “Scanning a nonclustered index entirely or only a range” Comparing I/O 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
33
33 Guidelines to Creating Indexes That Can Cover a Query Adding Columns to Indexes Limiting Index Key Size Maintaining Row-to-Key Size Ratio
34
34 Demo: Analyzing How Queries Are Covered By Different Types of Indexes
35
35 Using Index Tuning Tools to Improve Query Performance Using the Index Tuning Wizard Recommends or verifies optimal index configuration Provides cost analysis reports Recommends ways to tune the database Specifies criteria when a workload is evaluated Using the Index Analysis Tool Recommends optimal set of indexes to support a given query or batch
36
36 Access Method Page I/O Table scan 10,417 Clustered index on the charge_amt column 1042 Nonclustered index on the charge_amt column 100,273 Composite index on charge_amt, charge_no columns 273 Evaluating I/O for Queries That Access a Range of Data SELECT charge_no FROM charge WHERE charge_amt BETWEEN 20 AND 30
37
37 Demo : Analyzing Queries That Use the AND and OR Operators
38
Microsoft AREC TAM Internship go WHERE DO YOU WANT TO ? TODAY Microsoft
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.