Download - Session11 Index
-
7/29/2019 Session11 Index
1/31
Planning and Creating Index
Vu Tuyet [email protected]
Hanoi University of Technology
1
-
7/29/2019 Session11 Index
2/31
Microsoft
Microsoft
Outline
Introduction to Indexes
Understanding Index Architecture
Retrieving Stored Data with SQL Server Defining Indexes
Creating Indexes
Maintaining Index
2
-
7/29/2019 Session11 Index
3/31
Microsoft
Microsoft
Index
Data storage Rows are stored in data pages Heaps are a collection of data pages for a table
Data access Scanning all data pages in a table Using an index that points to data on a page
Data Pages
Page 7 Page 8 Page 9
Akhtar
Funk
Smith
Martin
...
Page 4 Page 5 Page 6
...
...
...
...
...
Con
Funk
White
...
...
Rudd
White
Barr...
...
Smith
Ota
Jones...
...
Martin
Phua
Jones
Smith
...
Ganio
Jones
Hall...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
-
7/29/2019 Session11 Index
4/31
Microsoft
Microsoft
Creating Index or not ?
Creating index
Speeds up data access
Enforces uniqueness of rows
Donot creating index
Consumes disk space Incurs overhead
Primary and foreign keys
Frequently searched in ranges
Frequently accessed in sorted order
Seldom referenced in queries
Containing few unique values
Defined with bit, text, orimage data
types
-
7/29/2019 Session11 Index
5/31
Microsoft
Microsoft
Index Architecture
How SQL Server Uses Indexes
Maintaining Heaps
Using Clustered Indexes Using Nonclustered Indexes
Accessing a Range of Data in a Heap
Accessing a Range of Data with Clustering Keys
-
7/29/2019 Session11 Index
6/31
Microsoft
Microsoft
How SQL Server Uses Indexes
1. Determining if index exists on an appropriate column for the query
2. If an index does not exist
Scanning each data page of the table.
3. If an index exists,
Determining whether that index is useful for retrieving the
desired data.
3.1 If the index is useful,
SQL Server then uses it to access the rows in the table
3.2 If the index is not user full database
Scanning each data page in table
-
7/29/2019 Session11 Index
7/31
MicrosoftMicrosoft
Maintaining Heaps
Using index allocation map pages
Containing information on where the extents of a heap
are stored
Navigating through the heap and find available space for newrows being inserted
Connecting data pages
Reclaiming space for new rows in the heap when a row isdeleted
-
7/29/2019 Session11 Index
8/31
MicrosoftMicrosoft
Using Clustered Indexes Each table can have only one clustered index
The physical row order of the table and the order of rowsin the index are the same
Key value uniqueness is maintained explicitly or implicitly
-
7/29/2019 Session11 Index
9/31
MicrosoftMicrosoft
Using Non-clustered Indexes
Non-clustered indexes are the SQL server default
Existing non-clustered indexes are automatically rebuilt
when:
An existing clustered index is dropped
A clustered index is created
The DROP_EXISTING option is used to change which columns
define the clustered index
-
7/29/2019 Session11 Index
10/31
MicrosoftMicrosoft
Accessing a Range of Data in a Heap
Data Pages
Page 7 Page 8 Page 9
Akhtar
Funk
Smith
Martin
...
...
...
...
...
...
470601
470602
470603
470604
...
Page 4 Page 5 Page 6
Con
Funk
White
...
...
...
...
...
...
...
470401
470402
470403
...
...
Rudd
White
Barr...
...
...
...
...
...
...
470501
470502
470503...
...
Smith
Ota
Jones...
...
...
...
...
...
...
470701
470702
470703...
...
Martin
Phua
Jones
Smith
...
...
...
...
...
...
470801
470802
470803
470804
...
Ganio
Jones
Hall...
...
...
...
...
...
...
470901
470902
470904...
...
Index Pages
Non-leafLevel
Page 12 - RootPage 37 Page 28
Leaf Level
(Key Value)
Page 41 Page 51 Page 61 Page 71
Akhtar...Martin
AkhtarBarrConFunkFunk
470601470503470401470602470402
MartinMartin
OtaPhuaRudd
470801470604470702470802470501
MartinSmith...
SmithSmithSmithWhiteWhite
470603470804470701470403470502
AkhtarGanio...
GanioHall
JonesJonesJones
470901470904470902470803470703
SELECT lastname, firstanme
FROM member
WHERE lastname
BETWEEN 'Martin' AND 'Rudd'
Data Pages
Page 7 Page 8 Page 9
Akhtar
Funk
Smith
Martin
...
...
...
...
...
...
470601
470602
470603
470604
...
Page 4 Page 5 Page 6
Con
Funk
White
...
...
...
...
...
...
...
470401
470402
470403
...
...
Rudd
White
Barr...
...
...
...
...
...
...
470501
470502
470503...
...
Smith
Ota
Jones...
...
...
...
...
...
...
470701
470702
470703...
...
Martin
Phua
Jones
Smith
...
...
...
...
...
...
470801
470802
470803
470804
...
Ganio
Jones
Hall...
...
...
...
...
...
...
470901
470902
470904...
...
Index Pages
Non-LeafLevel
Page 12 - RootPage 37 Page 28
Leaf Level
(Key Value)
Page 41 Page 51 Page 61 Page 71
Akhtar...Martin
AkhtarBarrConFunkFunk
470601470503470401470602470402
MartinMartin
OtaPhuaRudd
470801470604470702470802470501
MartinSmith...
SmithSmithSmithWhiteWhite
470603470804470701470403470502
AkhtarGanio...
GanioHall
JonesJonesJones
470901470904470902470803470703
Martin
Martin
Martin 470801Martin 470604Ota 470702Phua 470802Rudd 470501
Martin ... 470801Phua ... 470802Ota ... 470702
Martin ...470604
Rudd ... 470501
-
7/29/2019 Session11 Index
11/31
MicrosoftMicrosoft
Accessing a Range of Data in Clustering Key
1234
Index
Pages
Non-leaf
LevelPage 12 - Root1234...
5678Page 3712342234...
Page 2856787678...
Page 41 Page 61
56785778
587859786078
BarrSmith 8721
OtaJones 5788Rudd
Page 71
76787778
787879788078
GanioMartin 7206
PhuaSmith 8772Hall
Page 51
22342334
243425342634
WhiteAkhtar
JonesConJones6002
Leaf Level
(Key Value)
Clustered
IndexAkhtar...Martin
Page 140 - Root
Page 100 Page 110 Page 120 Page 130
1234
23345678253413341534
...
AkhtarBarrConFunkFunk
...
...
...
...
...
...
...
4705
...
76788078243459782634
...
GanioHallJonesJonesJones
...
...
...
...
... 5788
...
...
6002
...
AkhtarGanio...
Page 141
12347778587878786078
...
MartinMartinOtaPhuaRudd
...
...
... 7206
...
...
...
... ...
14345778797822341634
...
SmithSmithSmithWhiteWhite
...
...
... 8721
... 8772
...
...
...
9355
...
Page 145
1334
143415341634
MartinFunk
SmithFunk4705White9355
Martin
SELECT lastname, firstanme
FROM member
WHERE member_no
BETWEEN 5678 AND 6078
Smith...
1234
Index
Pages
Non-Leaf
LevelPage 12 - Root1234...
5678Page 3712342234...
Page 2856787678...
Page 41 Page 61
56785778
587859786078
BarrSmith 8721
OtaJones 5788Rudd
Page 71
76787778
787879788078
GanioMartin 7206
PhuaSmith 8772Hall
Page 51
22342334
243425342634
WhiteAkhtar
JonesConJones6002
Leaf Level
(Key Value)
Clustered
IndexAkhtar...Martin
Page 140 - Root
MartinSmith...
Page 100 Page 110 Page 120 Page 130
1234
23345678253413341534
...
AkhtarBarrConFunkFunk
...
...
...
...
...
...
...
4705
...
76788078243459782634
...
GanioHallJonesJonesJones
...
...
...
...
... 5788
...
...
6002
...
AkhtarGanio...
Page 141
12347778587878786078
...
MartinMartinOtaPhuaRudd
...
...
... 7206
...
...
...
... ...
14345778797822341634
...
SmithSmithSmithWhiteWhite
...
...
... 8721
... 8772
...
...
...
9355
...
Page 145
1334
143415341634
MartinFunk
SmithFunk4705White9355 6078 Rudd
56785778
58785978
BarrSmith 8721
OtaJones 5788
5678
5678
Martin
Akhtar
MartinSmith
6078 Rudd ...
5878 Ota ...5778 Smith ... 87215678 Barr ...
5978 Jones ... 5788
AkhtarGanio...
-
7/29/2019 Session11 Index
12/31
MicrosoftMicrosoft
Managing Indexes
Creating Indexes
Creating Unique Indexes
Creating Composite Indexes Obtaining Information on Existing Indexes
-
7/29/2019 Session11 Index
13/31
MicrosoftMicrosoft
Creating Index
Using the CREATE INDEX Statement
USE library
CREATE CLUSTERED INDEX cl_lastnameON library..member (lastname)
-
7/29/2019 Session11 Index
14/31
MicrosoftMicrosoft
Creating Unique Indexes
USE library
CREATE UNIQUE INDEX title_identON title (title_no)
12 Le Petit Prince Antoine de Saint-Exupery ~ ~ ~
Duplicate key values are not allowedwhen a new row is added to the table
title
title_no title author synopsis
1011
12
The Night-BornLemon
Walking
Jack LondonMotojirou
Henry David Thoreau
~ ~ ~~ ~ ~
~ ~ ~12 Walking Henry David Thoreau ~ ~ ~
-
7/29/2019 Session11 Index
15/31
MicrosoftMicrosoft
Creating Composite IndexesUSE library
CREATE UNIQUE INDEX loan_identON loan (isbn, copy_no)
Composite Key
Column 1 Column 2
loan
isbn copy_no title_no member_no
342
342
343
5
10
4
35
35
35
3744
5278
3445
out_date
1998-01-06
1998-01-04
1998-01-04
-
7/29/2019 Session11 Index
16/31
MicrosoftMicrosoft
Creating Index Option
Using the FILLFACTOR option
Using the PAD_INDEX option
-
7/29/2019 Session11 Index
17/31
MicrosoftMicrosoft
Using the FILLFACTOR Option
Specifies how much to fill the page
Impacts leaf-level pages
Data Pages Full
Con
Funk
WhiteRudd
...
...
...
...
470401
470402
470403470501
White ... 470502Barr ... 470503
Akhtar
Funk
SmithMartin
Smith
...
...
...
...
...
470601
470602
470603470604
470701Ota ... 470702
Martin
Phua
JonesSmith
Ganio
...
...
...
...
...
470801
470802
470803470804
470901Jones ... 470902
Data Pages
50% Fillfactor
ConFunkWhite
...
...
...
470401470402470403
RuddWhiteBarr
...
...
...
470501470502470503
AkhtarFunkSmith
...
...
...
470601470402470603
MartinSmithOta
...
...
...
470604470701470702
MartinPhuaJones
...
...
...
470801470802470803
SmithGanioWhite
...
...
...
470804470901470902
-
7/29/2019 Session11 Index
18/31
MicrosoftMicrosoft
Using the PAD_INDEX Option Specifies percentage to which to fill index page
Must use with FILLFACTOR option
Impact Non-Leaf-Level Index pages
-
7/29/2019 Session11 Index
19/31
MicrosoftMicrosoft
Obtaining Information on Existing Indexes
Using the sp_helpindex system stored procedure
Using the sp_help tablename system storedprocedure
USE library
EXEC sp_helpindex member
-
7/29/2019 Session11 Index
20/31
MicrosoftMicrosoft
CREATE CLUSTERED INDEX loan_identON loan (isbn, copy_no)
WITH DROP_EXISTING, FILLFACTOR=65
DROP_EXISTING Option
Rebuilds an index Can change index characteristics Reorganizes leaf pages Recalculates index statistics
Changing index characteristics Type Index columns Options
-
7/29/2019 Session11 Index
21/31
MicrosoftMicrosoft
Maintaining Indexes
Data fragmentation
Index tuning wizard
DBCC SHOWCONTIGStatement
DROP_EXISTING Option
Statistical information
Index analysis
-
7/29/2019 Session11 Index
22/31
MicrosoftMicrosoft
Data Fragmentation
How fragmentation occurs SQL Server reorganizes index pages when data is modified Reorganization causes index pages to split
Managing fragmentation Dropping and recreating an index and specifying a fillactorvalue Rebuild an index and specify a fillfactorvalue
Business environment
Data fragmentation can be good for OLTP environment Data fragmentation can be bad for OLAP Services environment
-
7/29/2019 Session11 Index
23/31
MicrosoftMicrosoft
Index Tuning Wizard Capturing a workload
Analyzing a workload
Providing an indexing recommendation Implementing an indexing recommendation
-
7/29/2019 Session11 Index
24/31
MicrosoftMicrosoft
DBCC SHOWCONTIG Statement DBCC SHOWCONTIG determines
whether a table or index is heavily fragmented
whether data and index pages are full
Executing DBCC SHOWCONTIG
If tables have been heavily modified
If tables contain imported data
If tables seem to cause poor query performance
-
7/29/2019 Session11 Index
25/31
MicrosoftMicrosoft
Describes distribution of data in a column for use by
query optimizer
Automatically maintained
Statistics creation
Automatic for indexed columns
Can be created for other columns
USE libraryCREATE STATISTICS STATS_outdateON loanhist (out_date)
Statistical Information
-
7/29/2019 Session11 Index
26/31
MicrosoftMicrosoft
Using the UPDATE STATISTICSStatement
Obtaining statistic information
Disabling the automatic updating of statistics
USE library
UPDATE STATISTICS loan loan_ident
Updating Statistics
-
7/29/2019 Session11 Index
27/31
MicrosoftMicrosoft
Index Analysis
Using the SHOWPLAN statement Indicating whether an index is used
Displaying summary or detailed information about a query
Using the STATISTICS IOStatement Indicating the amount of I/O used to return the result set
Displaying information on the logical and physical I/O for aspecific query
Using graphical SHOWPLAN
-
7/29/2019 Session11 Index
28/31
MicrosoftMicrosoft
Used in development environment
Specifying how to access data
Overriding the query optimizer
USE library
SELECT title_no, title, author
FROM title WITH (INDEX(author_ind))WHERE author = 'Jane Austen'
Optimizer Hints
-
7/29/2019 Session11 Index
29/31
MicrosoftMicrosoft
Performance Considerations Creating indexes on foreign keys
Creating clustered indexes before nonclustered indexes
Creating composite indexes Creating multiple indexes for a table frequently read
Use the Index Tuning Wizard
-
7/29/2019 Session11 Index
30/31
MicrosoftMicrosoft
Ramarks
Choose the appropriate columns to index
Use the FILLFACTOR and PAD_INDEX options to
optimize performance
Use the DROP_EXISTING option to rebuild indexes
Execute DBCC SHOWCONTIG to measure fragmentation
Avoid optimizer hints
-
7/29/2019 Session11 Index
31/31
MicrosoftMicrosoft