You are on page 1of 31

Planning and Creating Index

Vu Tuyet Trinh
trinhvt-fit@mail.hut.edu.vn

Hanoi University of Technology


1

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

Rudd White Barr


... ...

Akhtar Funk Smith Martin ...

Smith Ota Jones


... ...

Martin Phua Jones Smith ...

Ganio Jones Hall


... ...

... ... ... ... ...

Microsoft

Creating Index or not ?

Creating index

Speeds up data access Enforces uniqueness of rows Primary and foreign keys Frequently searched in ranges Frequently accessed in sorted order

Donot creating index


Consumes disk space Incurs overhead

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

How SQL Server Uses Indexes


1.
2.

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

3.2 If the index is not user full database


Scanning each data page in table

Microsoft

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 new rows being inserted Connecting data pages

Reclaiming space for new rows in the heap when a row is deleted

Microsoft

Using 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

Microsoft

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

Microsoft

Accessing a Range of Data in a Heap


Index Pages
Page 37 Akhtar lastname, SELECT Ganio FROM ... member Akhtar ... Martin Page 12 - Root

Non-Leaf Non-leaf Level

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

Page 28 Martin firstanme Smith ...

Leaf Level (Key Value)

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

Ganio ... 470901 Jones ... 470902 Hall ... 470904


... ... ... ... ... ...

Accessing a Range of Data in Clustering Key


Index Pages
Page 37 Page 28 1234 5678 2234 7678 ... ... SELECT lastname, firstanme Page 41 FROM member Page 51 Page 61 Page 71 1234 Martin White 5678 Barr 7678 Ganio 1234 WHERE 2234 member_no 1334 Funk 2334 Akhtar 5778 Smith 8721 7778 Martin 7206 BETWEEN 6078 1434 Smith 2434 5678 Jones AND 5878 Ota 7878 Phua 1534 Funk4705 2534 Con 5978 Jones 5788 7978 Smith 8772 1634 White9355 2634 Jones6002 6078 Rudd 8078 Hall Akhtar ... Martin Page 140 - Root

1234 ... 5678 Page 12 - Root

Non-Leaf Non-leaf Level

Leaf Level (Key Value)

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

Using the CREATE INDEX Statement

USE library CREATE CLUSTERED INDEX cl_lastname ON library..member (lastname)

Microsoft

Creating Unique Indexes


USE library CREATE UNIQUE INDEX title_ident ON title (title_no)

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

~~~

Creating Composite Indexes


USE library CREATE UNIQUE INDEX loan_ident ON loan (isbn, copy_no)

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 1 Composite Key

Column 2

Microsoft

Creating Index Option

Using the FILLFACTOR option Using the PAD_INDEX option

Microsoft

Using the FILLFACTOR Option

Specifies how much to fill the page Impacts leaf-level pages


Con Funk White Rudd White Barr ... 470401 ... 470402 ... 470403 ... 470501 ... 470502 ... 470503 Akhtar Funk Smith Martin Smith Ota ... 470601 ... 470602 ... 470603 ... 470604 ... 470701 ... 470702 Martin Phua Jones Smith Ganio Jones ... 470801 ... 470802 ... 470803 ... 470804 ... 470901 ... 470902

Data Pages Full

Data Pages 50% Fillfactor


Con ... 470401 Rudd ... 470501 Akhtar ... 470601 Martin ... 470604 Martin ... 470801 Smith ... 470804 Funk ... 470402 White ... 470502 Funk ... 470402 Smith ... 470701 Phua ... 470802 Ganio ... 470901 White ... 470403 Barr ... 470503 Smith ... 470603 Ota ... 470702 Jones ... 470803 White ... 470902

Microsoft

Using the PAD_INDEX Option

Specifies percentage to which to fill index page Must use with FILLFACTOR option
Impact Non-Leaf-Level Index pages

Microsoft

Obtaining Information on Existing Indexes

Using the sp_helpindex system stored procedure


Using the sp_help tablename system stored procedure USE library EXEC sp_helpindex member

Microsoft

DROP_EXISTING Option

Rebuilds an index

Can change index characteristics Reorganizes leaf pages Recalculates index statistics
Type Index columns Options

Changing index characteristics


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

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

Index Tuning Wizard

Capturing a workload Analyzing a workload Providing an indexing recommendation Implementing an indexing recommendation

Microsoft

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

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

USE library CREATE STATISTICS STATS_outdate ON loanhist (out_date)


Microsoft

Updating Statistics

Using the UPDATE STATISTICS Statement


USE library UPDATE STATISTICS loan loan_ident

Obtaining statistic information Disabling the automatic updating of statistics

Microsoft

Index Analysis

Using the SHOWPLAN statement


Indicating whether an index is used Displaying summary or detailed information about a query

Using the STATISTICS IO Statement


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

Using graphical SHOWPLAN

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

You might also like