sql server performance tuning(i)
DESCRIPTION
SQL Server Performance Tuning(I). -- SQL Server 2000. Haijun Yang AREC SQL Support Team mailto:[email protected] Feb, 2001. Agenda. Introduction to Optimizing Queries Index Strategies Query Plan Analysis. Overview. Introduction to the Query Optimizer - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/1.jpg)
Microsoft AREC TAM Microsoft AREC TAM InternshipInternship
SQL Server Performance SQL Server Performance Tuning(I)Tuning(I)
Haijun YangAREC SQL Support Team
mailto:[email protected]
Feb, 2001
-- SQL Server 2000
![Page 2: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/2.jpg)
2
AgendaAgenda
Introduction to Optimizing Queries Index Strategies Query Plan Analysis
![Page 3: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/3.jpg)
3
OverviewOverview
Introduction to the Query Optimizer Obtaining Query Plan Information Indexing Fundamentals Introduction to Statistics
![Page 4: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/4.jpg)
4
Function of the Query OptimizerFunction 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
![Page 5: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/5.jpg)
5
How the Query Optimizer Uses How the Query Optimizer Uses Cost-Based OptimizationCost-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
![Page 6: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/6.jpg)
6
How the Query Optimizer WorksHow the Query Optimizer Works
Parsing ProcessParsing Process
Standardization ProcessStandardization Process
Query OptimizationQuery Optimization
CompilationCompilation
Database Access RoutinesDatabase Access Routines
Transact-SQLTransact-SQL
ResultsSet
ResultsSet
![Page 7: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/7.jpg)
7
Query Optimization PhasesQuery 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
![Page 8: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/8.jpg)
8
Caching Query PlanCaching 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
![Page 9: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/9.jpg)
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
![Page 10: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/10.jpg)
10
Querying the sysindexes TableQuerying 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
![Page 11: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/11.jpg)
11
Viewing SHOWPLAN_ALL and Viewing SHOWPLAN_ALL and SHOWPLAN_TEXT OutputSHOWPLAN_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
![Page 12: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/12.jpg)
12
• Viewing Graphical Viewing Graphical ShowplanShowplan
Elements of Graphical Showplan Reading Graphical Query Plan Output
![Page 13: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/13.jpg)
13
Elements of Graphical ShowplanElements 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
![Page 14: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/14.jpg)
14
Reading Graphical Query Plan OutputReading Graphical Query Plan Output
Query PlanQuery Plan
SELECTCost: 0%
Bookmark LookupCost: 8%
Hash Match Root…Cost 28%
Member.corp_noCost 9%
Member.fnameCost: 10%
FilterCost: 0%
Sequence of StepsSequence of StepsSequence of StepsSequence of Steps
Index SeekScanning a particular range of rows from a non-clustered index.
Index SeekScanning 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 SeekIndex Seek
41424
0.007060.000605
1.00.007675(6%)
0.00767
Argument:OBJECT: ([credit].[dbo].[member].[fname]), SEEK: ([member],[firstname] >=‘Rb’ AND [member],[firstname] <‘T’) ORDERED
![Page 15: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/15.jpg)
15
• Indexing FundamentalsIndexing Fundamentals
Understanding the Data Limiting a Search Determining Selectivity Determining Density Determining Distribution of Data
![Page 16: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/16.jpg)
16
Determining SelectivityDetermining Selectivity
High selectivityHigh selectivitymember_nomember_nomember_nomember_no
11
22
..
..
last_namelast_namelast_namelast_name first_namefirst_namefirst_namefirst_name
RandallRandall
FloodFloodJoshuaJoshua
KathieKathie
..
1000010000 AndersonAnderson BillBill
SELECT *FROM memberWHERE member_no > 8999
SELECT *FROM memberWHERE member_no > 8999
100010000 = 10%
Number of rows meeting criteriaTotal number of rows in table =
Low selectivityLow selectivitymember_nomember_nomember_nomember_no
11
22
..
..
last_namelast_namelast_namelast_name first_namefirst_namefirst_namefirst_name
RandallRandall
FloodFloodJoshuaJoshua
KathieKathie
..
1000010000 AndersonAnderson BillBill
SELECT *FROM memberWHERE member_no < 9001
SELECT *FROM memberWHERE member_no < 9001
900010000 = 90%
Number of rows meeting criteriaTotal number of rows in table =
![Page 17: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/17.jpg)
17
Determining DensityDetermining Density
last_namelast_namelast_namelast_name first_namefirst_namefirst_namefirst_name
RandallRandall
..
..
..
JoshuaJoshua
RandallRandall CynthiaCynthia
RandallRandall TristanTristan
..
..
..
OtaOta LaniLani
..
..
..
SELECT *FROM memberWHERE last_name = ‘Ota’
SELECT *FROM memberWHERE last_name = ‘Ota’
Low DensityLow Density
SELECT *FROM memberWHERE last_name = ‘Randall’
SELECT *FROM memberWHERE last_name = ‘Randall’
High DensityHigh Density
![Page 18: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/18.jpg)
18
Determining Distribution of DataDetermining Distribution of Data
Standard Distribution of ValuesStandard Distribution of Values
F - JA - E K - O P - U V - ZLast Name
Number ofLast Names
Even Distribution of ValuesEven Distribution of Values
Last Name
Number ofLast Names
C - FA - B G - K L - N O - Z
![Page 19: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/19.jpg)
19
Updating StatisticsUpdating 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
![Page 20: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/20.jpg)
20
Viewing StatisticsViewing 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
![Page 21: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/21.jpg)
21
OverviewOverview
Accessing Data Using an Index to Cover a Query Using Index Tuning Tools to Improve Query
Performance Indexing Strategies
![Page 22: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/22.jpg)
22
• Accessing DataAccessing Data
Table Scans and Indexes Index Architecture and Navigation Using Row Identifiers to Access Data
![Page 23: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/23.jpg)
23
Table Scans and IndexesTable Scans and Indexes
Indexes Use Key Values to Locate Data Indexes Use Key Values to Locate Data
Table Scans Access Every PageTable Scans Access Every Page
Data PagesData Pages
Data PagesData Pages
……
Data PagesData Pages
……
Index PagesIndex Pages
![Page 24: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/24.jpg)
24
Index Architecture and NavigationIndex Architecture and Navigation
Clustered IndexClustered Index Nonclustered IndexNonclustered IndexIndex PagesIndex Pages
Non-LeafLevel
Leaf Level(Key Value)Leaf Level
(Key Value)
Data PagesData Pages LeafLevel
Index PagesIndex Pages
Non-LeafLevel
Data PagesData Pages
![Page 25: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/25.jpg)
25
Using Row Identifiers to Access DataUsing 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
![Page 26: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/26.jpg)
26
• Using an Index to Cover a QueryUsing 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
![Page 27: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/27.jpg)
27
Locating Data by Using Indexes Locating Data by Using Indexes That Cover a QueryThat Cover a Query
Example of Single Page Navigation Example of Partial Scan Navigation Example of Full Scan Navigation
![Page 28: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/28.jpg)
28
Example of Single Page NavigationExample of Single Page Navigation
Index PagesIndex Pages
Non-LeafLevel
Non-LeafLevel
Leaf Level(Key Value)Leaf Level
(Key Value)
SELECT lastname, firstnameFROM memberWHERE lastname = 'Hall'
SELECT lastname, firstnameFROM memberWHERE lastname = 'Hall'
AkhtarAkhtarBarrBarrBarrBarrBormBormBuhlBuhl
…………………………
GanioGanioHallHallHartHart
JonesJonesJonesJones
…………………………
Hall …MorganMorganNashNashNayNayOtaOta
RuddRudd
…………………………
MartinMartinSmithSmith
……
AkhtarAkhtarGanioGanio
……
AkhtarAkhtar……
MartinMartin
ChaiChaiConConConConCoxCoxDaleDale
…………………………
DunnDunnDunnDunnFineFineFortFortFunkFunk
…………………………
JordanJordanKimKimKimKimKochKochKochKoch
…………………………
LangLangMartinMartinMartinMartinMartinMartinMorisMoris
…………………………
SmithSmithSmithSmithSmithSmithSmithSmithSmithSmith
…………………………
…
Data PagesData Pages
![Page 29: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/29.jpg)
29
Example of Partial Scan NavigationExample of Partial Scan Navigation
Index PagesIndex Pages
Non-LeafLevel
Non-LeafLevel
Leaf Level(Key Value)Leaf Level
(Key Value)AkhtarAkhtarBarrBarrBarrBarrBormBormBuhlBuhl
…………………………
GanioGanioHallHallHartHart
JonesJonesJonesJones
…………………………
MorganMorganNashNashNayNayOtaOta
RuddRudd
…………………………
MartinMartinSmithSmith
……
AkhtarAkhtarGanioGanio
……
AkhtarAkhtar……
MartinMartin
ChaiChaiConConConConCoxCoxDaleDale
…………………………
DunnDunnDunnDunnFineFineFortFortFunkFunk
…………………………
JordanJordanKimKimKimKimKochKochKochKoch
…………………………
LangLangMartinMartinMartinMartinMartinMartinMorisMoris
…………………………
SmithSmithSmithSmithSmithSmithSmithSmithSmithSmith
…………………………
Data PagesData Pages
USE creditSELECT lastname, firstname FROM memberWHERE lastname BETWEEN 'Funk' AND 'Lang'
USE creditSELECT lastname, firstname FROM memberWHERE lastname BETWEEN 'Funk' AND 'Lang'
![Page 30: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/30.jpg)
30
Example of Full Scan NavigationExample of Full Scan Navigation
Index PagesIndex Pages
Non-LeafLevel
Non-LeafLevel
Leaf Level(Key Value)Leaf Level
(Key Value)AkhtarAkhtarBarrBarrBarrBarrBormBormBuhlBuhl
…………………………
GanioGanioHallHallHartHart
JonesJonesJonesJones
…………………………
MorganMorganNashNashNayNayOtaOta
RuddRudd
…………………………
MartinMartinSmithSmith
……
AkhtarAkhtarGanioGanio
……
AkhtarAkhtar……
MartinMartin
ChaiChaiConConConConCoxCoxDaleDale
…………………………
DunnDunnDunnDunnFineFineFortFortFunkFunk
…………………………
JordanJordanKimKimKimKimKochKochKochKoch
…………………………
LangLangMartinMartinMartinMartinMartinMartinMorisMoris
…………………………
SmithSmithSmithSmithSmithSmithSmithSmithSmithSmith
…………………………
…
Data PagesData Pages
USE creditSELECT lastname, firstnameFROM member
USE creditSELECT lastname, firstnameFROM member
![Page 31: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/31.jpg)
31
Identifying Whether an Index Can Be Identifying Whether an Index Can Be Used to Cover a QueryUsed 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
![Page 32: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/32.jpg)
32
Determining Whether an Index Is Used Determining Whether an Index Is Used to Cover a Queryto Cover a Query
Observing the Query Plan Output Displays the phrase “Scanning a
nonclustered index entirely or only a range” Comparing I/O
Nonclustered indexTotal number of levels in the non–leaf levelTotal number of pages that make up the leaf levelTotal number of rows per leaf-level pageTotal number of rows per data page
Total Number of pages that make up the table
![Page 33: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/33.jpg)
33
Guidelines to Creating Indexes That Guidelines to Creating Indexes That Can Cover a QueryCan Cover a Query
Adding Columns to Indexes Limiting Index Key Size Maintaining Row-to-Key Size Ratio
![Page 34: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/34.jpg)
34
Demo: Analyzing How Queries Are Covered By Different Types of Indexes
![Page 35: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/35.jpg)
35
Using Index Tuning Tools to Improve Using Index Tuning Tools to Improve Query PerformanceQuery 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
![Page 36: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/36.jpg)
36
Access MethodAccess MethodAccess MethodAccess Method Page I/OPage I/OPage I/OPage I/O
Table scanTable scan 10,41710,417
Clustered index on the charge_amt columnClustered index on the charge_amt column 1042 1042
Nonclustered index on the charge_amt columnNonclustered index on the charge_amt column 100,273100,273
Composite index on charge_amt, charge_nocolumns
Composite index on charge_amt, charge_nocolumns 273 273
Evaluating I/O for Queries That Access a Evaluating I/O for Queries That Access a Range of DataRange of Data
SELECT charge_noFROM chargeWHERE charge_amt BETWEEN 20 AND 30
SELECT charge_noFROM chargeWHERE charge_amt BETWEEN 20 AND 30
![Page 37: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/37.jpg)
37
Demo : Analyzing Queries That Use the AND andOR Operators
![Page 38: SQL Server Performance Tuning(I)](https://reader036.vdocuments.net/reader036/viewer/2022081421/5681515d550346895dbf81dd/html5/thumbnails/38.jpg)
Microsoft AREC TAM Microsoft AREC TAM InternshipInternship
gogoWHERE DOWHERE DO YOU WANT TOYOU WANT TO
??TODAYTODAYMicrosoftMicrosoft