Module 12: Optimizing Query Performance. Overview Introducing the Query Optimizer Tuning Performance...
-
Upload
zoe-martin -
Category
Documents
-
view
234 -
download
1
Transcript of Module 12: Optimizing Query Performance. Overview Introducing the Query Optimizer Tuning 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 evaluatingtheir 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
Results Set
The Query Optimization Process
Standardization ProcessStandardization Process
CompilationCompilation
Data Access RoutinesData Access Routines
Query OptimizationQuery Optimization
Transact-SQLTransact-SQL Parsing ProcessParsing 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 valuesp_configure query governor cost limit value
SET QUERY_GOVERNOR_COST_LIMIT valueSET 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
Statement Output Sample
STATISTICS TIME
SQL Server Execution Times:CPU time = 0 ms, elapsed time = 2 ms.
STATISTICS PROFILE
Rows Executes StmtText StmtId…
-----------------------------------------------
47 1 SELECT * FROM [charge] 16
WHERE (([charge_amt]>=@1)
.
.
.
STATISTICS IOTable '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 StepsAre 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 Areas Solution
Table ScansAdd indexes to columns in theWHERE 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 LevelNon-Leaf Level
Single-Page Navigation
AkersAkersBarrBarr
BarretoBarretoBorbaBorbaBudaiBudai
KimKim……………………
LangLangMartinMartinMartinMartinMartinMartinMorrisMorris
EricEric……………………
…
Data Pages
… …
AkersAkersLangLang
…………
KimKimEricEric…………
AkersAkers……
GanioGanio……
KimKim……
JonJon……
LangLang………………
EricEric………………
SELECT lastname, firstnameFROM memberWHERE lastname = 'Hall'
GanioGanioHallHallHartonoHartonoJonesJonesJonesJones
JonJonDonDonTommyTommyBrannonBrannon
DavidDavid
Hall Don
Index Pages
Non-Leaf LevelNon-Leaf Level
Leaf Level (Key Value)
Partial-Scan Navigation
AkersAkersBarrBarr
BarretoBarretoBorbaBorbaBudaiBudai
…………………………
GanioGanioHallHall
HartonoHartonoJonesJonesJonesJones
…………………………
MoyerMoyerNashNashNayNay
OwenOwenRussoRusso
…………………………
ChaiChaiConConCoxCoxCoxCox
DannerDanner
…………………………
DuboisDuboisDuffyDuffyFineFineFortFortFunkFunk
…………………………
JordanJordanKimKimKimKimKochKochKochKoch
…………………………
LangLangMartinMartinMartinMartinMartinMartinMorrisMorris
…………………………
SmithSmithSmithSmithSmithSmithSmithSmithSmithSmith
…………………………
AkersAkersChaiChai
DuboisDuboisGanioGanio
……………………
JordanJordanLangLang
MoyerMoyerSmithSmith
……………………
Data Pages
AkersAkers
JordanJordan
……
……
……
……
USE creditSELECT lastname, firstname FROM memberWHERE lastname BETWEEN 'Funk' AND 'Lang'
Index Pages
Leaf Level (Key Value)
Non-Leaf LevelNon-Leaf Level
Data Pages
Full-Scan Navigation
AkersAkersBarrBarr
BarretoBarretoBorbaBorbaBudaiBudai
…………………………
GanioGanioHallHall
HartonoHartonoJonesJonesJonesJones
…………………………
MoyerMoyerNashNashNayNay
OwenOwenRussoRusso
…………………………
MartinMartinSmithSmith
……
AkersAkersGanioGanio
……
AkersAkers……
MartinMartin
ChaiChaiConConCoxCoxCoxCox
DannerDanner
…………………………
DuboisDuboisDuffyDuffyFineFineFortFortFunkFunk
…………………………
JordanJordanKimKimKimKimKochKochKochKoch
…………………………
LangLangMartinMartinMartinMartinMartinMartinMorrisMorris
…………………………
SmithSmithSmithSmithSmithSmithSmithSmithSmithSmith
…………………………
…
USE creditSELECT lastname, firstnameFROM 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 indexTotal 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
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_noFROM chargeWHERE charge_amt BETWEEN 20 AND 30
SELECT charge_noFROM chargeWHERE 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 2Example 2
Indexing Strategy for Multiple Queries
USE creditSELECT charge_no, charge_dt, charge_amtFROM chargeWHERE statement_no = 19000 AND member_no = 3852
USE creditSELECT charge_no, charge_dt, charge_amtFROM chargeWHERE statement_no = 19000 AND member_no = 3852
USE creditSELECT member_no, charge_no, charge_amtFROM chargeWHERE charge_dt BETWEEN '07/30/1999'AND '07/31/1999' AND member_no = 9331
USE creditSELECT member_no, charge_no, charge_amtFROM chargeWHERE charge_dt BETWEEN '07/30/1999'AND '07/31/1999' AND member_no = 9331
Example 1Example 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 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 Have a Thorough Understanding of the Data and How Queries Gain Access to Data Create Indexes That Cover the Most Frequently Used QueriesCreate Indexes That Cover the Most Frequently Used QueriesEstablish Indexing Strategies for Individual and Multiple QueriesEstablish Indexing Strategies for Individual and Multiple Queries
Avoid Overriding the Query Optimizer 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 Coveredby 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