38
Microsoft AREC TAM Microsoft AREC TAM Internship Internship SQL Server Performance SQL Server Performance Tuning(I) Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft. com Feb, 2001 -- SQL Server 2000

Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:[email protected] Feb, 2001 -- SQL Server 2000

Embed Size (px)

Citation preview

Page 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

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: 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

AgendaAgenda

Introduction to Optimizing Queries Index Strategies Query Plan Analysis

Page 3: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

3

OverviewOverview

Introduction to the Query Optimizer Obtaining Query Plan Information Indexing Fundamentals Introduction to Statistics

Page 4: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

12

• Viewing Graphical Viewing Graphical ShowplanShowplan

Elements of Graphical Showplan Reading Graphical Query Plan Output

Page 13: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

15

• Indexing FundamentalsIndexing Fundamentals

Understanding the Data Limiting a Search Determining Selectivity Determining Density Determining Distribution of Data

Page 16: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

21

OverviewOverview

Accessing Data Using an Index to Cover a Query Using Index Tuning Tools to Improve Query

Performance Indexing Strategies

Page 22: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

22

• Accessing DataAccessing Data

Table Scans and Indexes Index Architecture and Navigation Using Row Identifiers to Access Data

Page 23: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

34

Demo: Analyzing How Queries Are Covered By Different Types of Indexes

Page 35: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

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: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

37

Demo : Analyzing Queries That Use the AND andOR Operators

Page 38: Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team mailto:hjyang@microsoft.com Feb, 2001 -- SQL Server 2000

Microsoft AREC TAM Microsoft AREC TAM InternshipInternship

gogoWHERE DOWHERE DO YOU WANT TOYOU WANT TO

??TODAYTODAYMicrosoftMicrosoft