SQL Server - Bài
Trang 1Planning and Creating Index
Vu Tuyet Trinh
trinhvt-fit@mail.hut.edu.vn
Hanoi University of Technology
1
Trang 2 Introduction to Indexes
Understanding Index Architecture
Retrieving Stored Data with SQL Server
Defining Indexes
Creating Indexes
Maintaining Index
Trang 3Index
Rows are stored in data pages
Heaps are a collection of data pages for a table
Scanning all data pages in a table
Using an index that points to data on a page
Data Pages
Akhtar Funk Smith Martin
Smith Ota Jones
Martin Phua Jones Smith
Ganio Jones Hall
Trang 4Creating 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, or image data
types
Trang 5Index Architecture
Using Clustered Indexes
Using Nonclustered Indexes
Accessing a Range of Data with Clustering Keys
Trang 6How 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
Trang 7Maintaining 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 new
rows being inserted
Connecting data pages
Reclaiming space for new rows in the heap when a row is deleted
Trang 8Using Clustered Indexes
Each table can have only one clustered index
The physical row order of the table and the order of rows
in the index are the same
Key value uniqueness is maintained explicitly or implicitly
Trang 9Using 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
Trang 10Accessing a Range of Data in a Heap
Data Pages
Akhtar Funk Smith Martin
Rudd White Barr
Smith Ota Jones
Martin Phua Jones Smith
Ganio Jones Hall
Index Pages
Non-leaf Level
Non-leaf Level
Page 12 - Root
Leaf Level (Key Value)
Leaf Level (Key Value)
Akhtar
Martin
Akhtar Barr Con Funk Funk
470601 470503 470401 470602 470402
Martin Martin Ota Phua Rudd
470801 470604 470702 470802 470501
Martin Smith
Smith Smith Smith White White
470603 470804 470701 470403 470502
Akhtar Ganio
Ganio Hall Jones Jones Jones
470901 470904 470902 470803 470703
SELECT lastname, firstanme FROM member
WHERE lastname BETWEEN 'Martin' AND 'Rudd'
SELECT lastname, firstanme FROM member
WHERE lastname BETWEEN 'Martin' AND 'Rudd'
Data Pages
Akhtar Funk Smith Martin
Rudd White Barr
Smith Ota Jones
Martin Phua Jones Smith
Ganio Jones Hall
Index Pages
Non-Leaf Level
Non-Leaf Level
Page 12 - Root
Leaf Level (Key Value)
Leaf Level (Key Value)
Akhtar
Martin
Akhtar Barr Con Funk Funk
470601 470503 470401 470602 470402
Martin Martin Ota Phua Rudd
470801 470604 470702 470802 470501
Martin Smith
Smith Smith Smith White White
470603 470804 470701 470403 470502
Akhtar Ganio
Ganio Hall Jones Jones Jones
470901 470904 470902 470803 470703
Martin
Martin
Martin 470801 Martin 470604 Ota 470702 Phua 470802 Rudd 470501
Martin 470801
Phua 470802
Ota 470702 Martin 470604
Rudd 470501
Trang 11Page 37
1234 2234
Page 28
5678 7678
5678 5778 5878 5978 6078
Barr Smith 8721 Ota
Jones 5788 Rudd
Page 71
7678 7778 7878 7978 8078
Ganio Martin 7206 Phua
Smith 8772 Hall
Hall
Page 51
2234 2334 2434 2534 2634
White Akhtar Jones Con Jones6002
Leaf Level (Key Value)
Akhtar Barr Con Funk Funk
Ganio Hall Jones Jones Jones
5788
6002
Akhtar Ganio
Page 141
1234 7778 5878 7878 6078
Martin Martin Ota Phua Rudd
7206
1434 5778 7978 2234 1634
Smith Smith Smith White White
8721 8772
9355
Page 145
1334 1434 1534 1634
Martin Funk Smith Funk4705 White9355
Martin
SELECT lastname, firstanme FROM member
WHERE member_no BETWEEN 5678 AND 6078
SELECT lastname, firstanme FROM member
WHERE member_no BETWEEN 5678 AND 6078
Smith
5678
Page 37
1234 2234
Page 28
5678 7678
5678 5778 5878 5978 6078
Barr Smith 8721 Ota
Jones 5788 Rudd
Page 71
7678 7778 7878 7978 8078
Ganio Martin 7206 Phua
Smith 8772 Hall
Hall
Page 51
2234 2334 2434 2534 2634
White Akhtar Jones Con Jones6002
Leaf Level (Key Value)
1234
2334 5678 2534 1334 1534
Akhtar Barr Con Funk Funk
Ganio Hall Jones Jones Jones
5788
6002
Akhtar Ganio
Page 141
1234 7778 5878 7878 6078
Martin Martin Ota Phua Rudd
7206
1434 5778 7978 2234 1634
Smith Smith Smith White White
8721 8772
9355
Page 145
1334 1434 1534 1634
Martin Funk Smith Funk4705 White9355 6078 Rudd
5678 5778 5878 5978
Barr Smith 8721 Ota
Jones 5788
5678 5678
Martin Akhtar
Martin Smith
Trang 12Managing Indexes
Creating Indexes
Obtaining Information on Existing Indexes
Trang 14Creating Unique Indexes
USE library CREATE UNIQUE INDEX title_ident
ON title (title_no)
USE library CREATE UNIQUE INDEX title_ident
ON title (title_no)
12 Le Petit Prince Antoine de Saint-Exupery ~ ~ ~
Duplicate key values are not allowed
when a new row is added to the table
Duplicate key values are not allowed
when a new row is added to the table
title
10 11 12
10 11 12
The Night-Born Lemon
Walking
The Night-Born Lemon
Walking
Jack London Motojirou Henry David Thoreau
Jack London Motojirou Henry David Thoreau
Trang 15Creating Composite Indexes
USE library
CREATE UNIQUE INDEX loan_ident
ON loan (isbn, copy_no)
USE library
CREATE UNIQUE INDEX loan_ident
ON loan (isbn, copy_no)
342 342 343
5 10 4
5 10 4
35 35 35
35 35 35
3744 5278 3445
3744 5278 3445
out_date
1998-01-06 1998-01-04 1998-01-04
1998-01-06 1998-01-04 1998-01-04
Trang 16Creating Index Option
Trang 17 Specifies how much to fill the page
Impacts leaf-level pages
Data Pages Full
Con Funk White Rudd
Akhtar Funk Smith Martin Smith
Martin Phua Jones Smith Ganio
Rudd White Barr
470501 470502 470503
Akhtar Funk Smith
470601 470402 470603
Martin Smith Ota
470604 470701 470702
Martin Phua Jones
Smith Ganio White
470804 470901 470902
Trang 18Using the PAD_INDEX Option
Specifies percentage to which to fill index page
Impact Non-Leaf-Level Index pages
Trang 19Obtaining Information on Existing Indexes
procedure
USE libraryEXEC sp_helpindex member
Trang 20CREATE CLUSTERED INDEX loan_ident
ON loan (isbn, copy_no)
WITH DROP_EXISTING, FILLFACTOR=65
CREATE CLUSTERED INDEX loan_ident
ON 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
Trang 22Data Fragmentation
SQL Server reorganizes index pages when data is modified
Reorganization causes index pages to split
Dropping and recreating an index and specifying a fillactor value
Rebuild an index and specify a fillfactor value
Data fragmentation can be good for OLTP environment
Data fragmentation can be bad for OLAP Services environment
Trang 23Index Tuning Wizard
Capturing a workload
Analyzing a workload
Providing an indexing recommendation
Trang 24DBCC SHOWCONTIG Statement
whether a table or index is heavily fragmented
whether data and index pages are full
If tables have been heavily modified
If tables contain imported data
If tables seem to cause poor query performance
Trang 25 Automatic for indexed columns
Can be created for other columns
USE library CREATE STATISTICS STATS_outdate
Trang 26 Using the UPDATE STATISTICS Statement
Obtaining statistic information
Disabling the automatic updating of statistics
Trang 27Index Analysis
Using the SHOWPLAN statement
Indicating whether an index is used
Displaying summary or detailed information about a query
Indicating the amount of I/O used to return the result set
Displaying information on the logical and physical I/O for a specific query
Trang 28 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'
USE library
SELECT title_no, title, author
FROM title WITH (INDEX(author_ind)) WHERE author = 'Jane Austen'
Optimizer Hints
Trang 29Performance 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
Trang 30• Choose the appropriate columns to index
• Use the FILLFACTOR and PAD_INDEX options to
optimize performance
• Use the DROP_EXISTING option to rebuild indexes
• Avoid optimizer hints
Trang 31Microsoft