Professional Documents
Culture Documents
Vu Tuyet Trinh
trinhvt-fit@mail.hut.edu.vn
Outline
Introduction to Indexes Understanding Index Architecture Retrieving Stored Data with SQL Server Defining Indexes Creating Indexes Maintaining Index
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 4 Con Funk White ... ... Page 5 ... ... ... ... ... Page 6 ... ... ... ... ... Page 7 ... ... ... ... ... Page 8 ... ... ... ... ... Page 9 ... ... ... ... ...
Microsoft
Creating index
Speeds up data access Enforces uniqueness of rows 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
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
Microsoft
3.
Determining if index exists on an appropriate column for the query If an index does not exist Scanning each data page of the table. 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
Microsoft
Maintaining Heaps
Reclaiming space for new rows in the heap when a row is deleted
Microsoft
Microsoft
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
Microsoft
Page 41 Page 51 Page 61 Page 71 BETWEEN 'Martin' AND 'Rudd' Akhtar 470601 Ganio 470901 Martin Martin 470801 470801 Smith 470603 Barr 470503 Hall 470904 Martin Martin 470604 470604 Smith 470804 Con 470401 Jones 470902 Ota Ota 470702 470702 Smith 470701 Funk 470602 Jones 470803 Phua 470802 470802 White 470403 Funk 470402 Jones 470703 Rudd 470501 470501 White 470502
WHERE lastname
Data Pages
Page 4 Con Funk White ... ... Microsoft Page 5 ... 470401 ... 470402 ... 470403 ... ... ... ... Page 6 Page 7 Page 8 Page 9
Rudd ... 470501 Akhtar ... 470601 White ... 470502 Funk ... 470602 Barr ... 470503 Smith ... 470603 ... ... ... Martin ... 470604 ... ... ... ... ... ...
Martin ... 470801 Smith ... 470701 Martin Phua ... 470802 Ota ... 470702 Phua Jones ... 470703 Jones ... 470803 ... ... ... Smith ... 470804 ... ... ... ... ... ...
Clustered Index
Page 141 Akhtar Ganio ... 2334 Akhtar... 5678 Barr Barr ... 2534 Con ... 1334 Funk ... 1534 Funk ... 4705 ... ... ... ... Page 100
Page 145 Martin Smith ... 1434 Smith ... 5778 Smith ... 8721 7978 Smith ... 8772 2234 White ... 1634 White ... 9355 ... ... ... ... Page 130
Microsoft
7678 Ganio ... 8078 Hall ... 2434 Jones ... 5978 Jones ... 5788 2634 Jones ... 6002 ... ... ... ... Page 110
1234 Martin ... 7778 Martin ... 7206 5878 Ota Ota ... 7878 Phua ... 6078 Rudd ... ... ... ... ... Page 120
Managing Indexes
Creating Indexes Creating Unique Indexes Creating Composite Indexes Obtaining Information on Existing Indexes
Microsoft
Creating Index
Microsoft
title
title_no 10 11 12 title The Night-Born Lemon Walking author synopsis Jack London ~~~ Motojirou ~~~ Henry David Thoreau ~ ~ ~
Duplicate key values are not allowed when a new row is added to the table
12
Microsoft
Le Petit Prince
Antoine de Saint-Exupery
~~~
loan
isbn 342 342 343 copy_no 5 10 4 title_no 35 35 35 member_no 3744 5278 3445 out_date 1998-01-06 1998-01-04 1998-01-04
Column 2
Microsoft
Microsoft
Microsoft
Specifies percentage to which to fill index page Must use with FILLFACTOR option
Impact Non-Leaf-Level Index pages
Microsoft
Microsoft
DROP_EXISTING Option
Rebuilds an index
Can change index characteristics Reorganizes leaf pages Recalculates index statistics
Type Index columns Options
CREATE CLUSTERED INDEX loan_ident ON loan (isbn, copy_no) WITH DROP_EXISTING, FILLFACTOR=65
Microsoft
Maintaining Indexes
Data fragmentation Index tuning wizard DBCC SHOWCONTIG Statement DROP_EXISTING Option
Statistical information Index analysis
Microsoft
Data Fragmentation
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 fillactor value Rebuild an index and specify a fillfactor value
Business environment
Data fragmentation can be good for OLTP environment Data fragmentation can be bad for OLAP Services environment
Microsoft
Capturing a workload Analyzing a workload Providing an indexing recommendation Implementing an indexing recommendation
Microsoft
Microsoft
Statistical Information
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
Updating Statistics
Microsoft
Index Analysis
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
Microsoft
Optimizer Hints
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'
Microsoft
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
Microsoft
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
Microsoft
Microsoft