Module 6: Planning Indexes Overview Introduction to Indexes Index - - PowerPoint PPT Presentation

module 6 planning indexes overview
SMART_READER_LITE
LIVE PREVIEW

Module 6: Planning Indexes Overview Introduction to Indexes Index - - PowerPoint PPT Presentation

Module 6: Planning Indexes Overview Introduction to Indexes Index Architecture How SQL Server Retrieves Stored Data How SQL Server Maintains Index and Heap Structures Deciding Which Columns to Index Introduction to Indexes


slide-1
SLIDE 1

Module 6: Planning Indexes

slide-2
SLIDE 2

Overview

 Introduction to Indexes  Index Architecture  How SQL Server Retrieves Stored Data  How SQL Server Maintains Index and Heap Structures  Deciding Which Columns to Index

slide-3
SLIDE 3

Introduction to Indexes

 How SQL Server Stores and Accesses Data  Whether to Create Indexes

slide-4
SLIDE 4

How SQL Server Stores and Accesses Data

 How Data Is Stored

 Rows are stored in data pages  Heaps are a collection of data pages for a table

 How Data Is Accessed

 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

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

slide-5
SLIDE 5

Whether to Create Indexes

 Why to Create an Index

 Speeds up data access  Enforces uniqueness of rows

 Why Not to Create an Index

 Consumes disk space  Incurs overhead

slide-6
SLIDE 6

 Index Architecture

 SQL Server Index Architecture  Using Heaps  Using Clustered Indexes  Using Nonclustered Indexes

slide-7
SLIDE 7

Multimedia Presentation: SQL Server Index Architecture

slide-8
SLIDE 8

Using Heaps

SQL Server:

 Uses Index Allocation Map Pages That:

 Contain information on where the extents of a heap

are stored

 Navigate through the heap and find available space for

new rows being inserted

 Connect data pages

 Reclaims Space for New Rows in the Heap When a Row

Is Deleted

slide-9
SLIDE 9

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

  • r Implicitly
slide-10
SLIDE 10

Using Nonclustered Indexes

 Nonclustered Indexes Are the SQL Server Default  Existing Nonclustered Indexes Are Automatically

Rebuilt When:

 An existing clustered index is dropped  A new clustered index is created  The DROP_EXISTING option is used to change which

columns define the clustered index

slide-11
SLIDE 11

 How SQL Server Retrieves Stored Data

 How SQL Server Uses the sysindexes Table  Finding Rows Without Indexes  Finding Rows in a Heap with a Nonclustered Index  Finding Rows in a Clustered Index  Finding Rows in a Clustered Index with a Nonclustered

Index

slide-12
SLIDE 12

How SQL Server Uses the sysindexes Table

 Describes the Indexes  Location of IAM, First, and Root of Indexes  Number of Pages and Rows  Distribution of Data

indid indid Object Type Object Type

Heap 1 Clustered Index 2 to 250 Nonclustered Index 255 text, ntext, or image

slide-13
SLIDE 13

Finding Rows Without Indexes

Heap

Extent 127

id indid = 0 First IAM sysindexes IAM

Extent 128 Extent 129 01 02 03 … … Smith Ota Jones … ... … … … ... ... 01 02 03 04 … Akhtar Funk Smith Martin ... … … … ... ... 01 02 03 … … Rudd White Barr ... ... … … … ... ... 01 02 03 … … Con Funk White ... ... … … … ... ... 01 02 03 … … Smith Ota Jones … ... … … … ... ... 01 02 03 04 … Akhtar Funk Smith Martin ... … … … ... ... 01 02 03 … … Rudd White Barr ... ... … … … ... ... 01 02 03 04 05 Con Funk White Durkin Lang … … … ... ... 01 02 03 … … Smith Ota Jones … ... … … … ... ... 01 02 03 04 … Akhtar Funk Smith Martin ... … … … ... ... 01 02 03 … … Rudd White Barr ... ... … … … ... ... 01 02 03 … … Con Funk White ... ... … … … ... ... 01 02 03 … … Smith Ota Jones … ... … … … ... ... 01 02 03 04 … Akhtar Funk Smith Martin ... … … … ... ... 01 02 03 … … Rudd White Barr ... ... … … … ... ... 01 02 03 04 05 Dunn Randall Ota Slichter LaBrie … … … ... ... 01 02 03 … … Smith Ota Jones … ... … … … ... ... 01 02 03 04 … Akhtar Funk Smith Martin ... … … … ... ... 01 02 03 … … Rudd White Barr ... ... … … … ... ... 01 02 03 … … Con Funk White ... ... … … … ... ... 01 02 03 … … Smith Ota Jones … ... … … … ... ... 01 02 03 04 … Akhtar Funk Smith Martin ... … … … ... ... 01 02 03 … … Rudd White Barr ... ... … … … ... ... 01 02 03 … … Smith Ota Jones … ... … … … ... ... 01 02 03 04 … Akhtar Funk Smith Martin ... … … … ... ... 01 02 03 … … Rudd White Barr ... ... … … … ... ... 01 02 03 … … Con Funk White ... ... … … … ... ... 01 02 03 … … Smith Ota Jones … ... … … … ... ... 01 02 03 04 … Akhtar Funk Smith Martin ... … … … ... ... 01 02 03 … … Rudd White Barr ... ... … … … ... ... 01 02 03 … … Graff Bacon Koch ... ... … … … ... ... Extent 130 01 02 03 04 … Seattle Paris Tokyo Atlanta ... … … … ... ... … 127 1 128 1 129 0 130 1 …

Extent Bit Map

slide-14
SLIDE 14

Finding Rows in a Heap with a Nonclustered Index

Non-Leaf Level

Page 12 - Root Page 37 Page 28

Leaf Level

(Key Value) Page 41 Page 51 Page 61 Page 71 Akhtar ... Martin Akhtar Barr Con Funk Funk 4:706:01 4:705:03 4:704:01 4:706:02 4:704:02 Martin Smith ... Smith Smith Smith White White 4:706:03 4:708:04 4:707:01 4:704:03 4:705:02 Akhtar Ganio ... Ganio Hall Jones Jones Jones 4:709:01 4:709:04 4:709:02 4:708:03 4:707:03

Heap

Page 707 Page 708 Page 709 01 02 03 04 ... ... ... ... ... ... Akhtar Funk Smith Matey ... Page 704 Page 705 Page 706 01 02 03 ... ... ... ... ... ... ... Conn Funk White ... ... 01 02 03 ... ... ... ... ... ... ... Rudd White Barr ... ... 01 02 03 ... ... ... ... ... ... ... Smith Ota Jones ... ... 01 02 03 04 ... ... ... ... ... ... Martin Phua Jones Smith ... 01 02 03 ... ... ... ... ... ... ... Ganio Jones Hall ... ... Martin Matey Ota Phua Rudd 4:708:01 4:706:04 4:707:02 4:708:02 4:705:01

Non Clustered Index

File ID #4

id indid = 2 root sysindexes

SELECT lastname, firstname FROM member WHERE lastname BETWEEN 'Masters' AND 'Rudd'

Non-Leaf Level

Page 12 - Root Page 37 Page 28

Leaf Level

(Key Value) Page 41 Page 51 Page 61 Page 71 Akhtar ... Martin Akhtar Barr Con Funk Funk 4:706:01 4:705:03 4:704:01 4:706:02 4:704:02 Martin Smith ... Smith Smith Smith White White 4:706:03 4:708:04 4:707:01 4:704:03 4:705:02 Akhtar Ganio ... Ganio Hall Jones Jones Jones 4:709:01 4:709:04 4:709:02 4:708:03 4:707:03

Heap

Page 707 Page 708 Page 709 01 02 03 04 ... ... ... ... ... ... Akhtar Funk Smith Matey ... Page 704 Page 705 Page 706 01 02 03 ... ... ... ... ... ... ... Conn Funk White ... ... 01 02 03 ... ... ... ... ... ... ... Rudd White Barr ... ... 01 02 03 ... ... ... ... ... ... ... Smith Ota Jones ... ... 01 02 03 04 ... ... ... ... ... ... Martin Phua Jones Smith ... 01 02 03 ... ... ... ... ... ... ... Ganio Jones Hall ... ... Martin Matey Ota Phua Rudd 4:708:01 4:706:04 4:707:02 4:708:02 4:705:01

Non clustered Index

File ID #4 Martin Martin Martin Martin 04 04 ... ... Matey Matey Matey Matey 4:706:04 4:706:04 02 02 ... ... Phua Phua Phua Phua 4:708:02 4:708:02 01 01 ... ... Rudd Rudd Rudd Rudd 4:705:01 4:705:01 02 02 ... ... Ota Ota Ota Ota 4:707:02 4:707:02

slide-15
SLIDE 15

Finding Rows in a Clustered Index

Clustered Index

Page 140 - Root Page 100 Page 120 Page 130 Page 141 Page 145 Akhtar Barr Con Funk Funk ... 2334 5678 2534 1334 1534 ... ... ... ... ... ... ... Martin Martin Ota Phua Rudd ... 1234 7778 5878 7878 6078 ... ... ... ... ... ... ... Smith Smith Smith White White ... 1434 5778 7978 2234 1634 ... ... ... ... ... ... ... Akhtar Ganio … … Akhtar … Martin Martin Smith … Page 110 Ganio Hall Jones Jones Jones ... 7678 8078 2434 5978 2634 ... ... ... ... ... ... ...

SELECT lastname, firstname FROM member WHERE lastname = 'Ota'

Clustered Index

Page 140 - Root Page 100 Page 120 Page 130 Page 141 Page 145 Akhtar Barr Con Funk Funk ... 2334 5678 2534 1334 1534 ... ... ... ... ... ... ... Martin Martin Ota Phua Rudd ... 1234 7778 5878 7878 6078 ... ... ... ... ... ... ... Smith Smith Smith White White ... 1434 5778 7978 2234 1634 ... ... ... ... ... ... ... Akhtar Ganio … … Akhtar … Martin Martin Smith … Page 110 Ganio Hall Jones Jones Jones ... 7678 8078 2434 5978 2634 ... ... ... ... ... ... ... Martin Martin Ota Ota 5878 5878 ... ... Martin Martin

id indid = 1 root sysindexes

slide-16
SLIDE 16

Finding Rows in a Clustered Index with a Nonclustered Index

Clustered Index On Last Name Nonclustered Index on First Name Non-Leaf Level Leaf Level

(Clustered Key Value)

Aaron Deanna … Aaron ... Jose Jose Nina … Deanna Don Doug Daum Hall Hampton … … Aaron Adam Amie Con Barr Baldwin … … Jose Judy Mike Lugo Kaethler Nash … … Barr Adam Cox Daum Arlette Deanna … … … … … … Kim Kobara LaBrie Shane Linda Ryan … … … … … … Nagata Nash Nixon Susanne Mike Toby … … … … … … Barr Kim Nagata O’Melia id indid = 2 root sysindexes

SELECT lastname, firstname, phone FROM member WHERE firstname = 'Mike'

Clustered Index On Last Name Nonclustered Index on First Name Non-Leaf Level Leaf Level

(Clustered Key Value)

Aaron Deanna … Aaron ... Jose Jose Nina … Deanna Don Doug Daum Hall Hampton … … Aaron Adam Amie Con Barr Baldwin … … Jose Judy Mike Lugo Kaethler Nash … … Barr Adam Cox Daum Arlette Deanna … … … … … … Kim Kobara LaBrie Shane Linda Ryan … … … … … … Nagata Nash Nixon Susanne Mike Toby … … … … … … Barr Kim Nagata O’Melia Mike Mike Nash Nash Nagata Nagata Nash Nash Mike Mike … …

slide-17
SLIDE 17

 How SQL Server Maintains Index and Heap Structures

 Page Splits in an Index  Forwarding Pointer in a Heap  How SQL Server Updates Rows  How SQL Server Deletes Rows

slide-18
SLIDE 18

Page Splits in an Index

Index Pages Non-Leaf Level INSERT member (last name) VALUES lastname = ‘Jackson'

Lang Smith … Akhtar Ganio … Akhtar … Martin Jackson

Leaf Level

(Key Value) Akhtar Barr Barr Borm Buhl … … … … … Lang Martin Martin Martin Moris … … … … … Smith Smith Smith Smith Smith … … … … …

… … …

Ganio Hall Hart Jones Jones … … … … …

Leaf Level

(Key Value) Akhtar Barr Barr Borm Buhl … … … … … Jackson Jones Jones … … … Lang Martin Martin Martin Moris … … … … … Smith Smith Smith Smith Smith … … … … …

… … …

Ganio Hall Hart … … …

Jackson …

slide-19
SLIDE 19

Forwarding Pointer in a Heap

Non-Leaf Level

Page 12 - Root Page 37 Page 28

Leaf Level

(Key Value) Page 41 Page 51 Page 61 Page 71 Akhtar ... Martin Akhtar Barr Con Funk Funk 4:706:01 4:705:03 4:704:01 4:706:02 4:704:02 Martin Smith ... Smith Smith Smith White White 4:706:03 4:708:04 4:707:01 4:704:03 4:705:02 Akhtar Ganio ... Ganio Hall Jones Jones Jones 4:709:01 4:709:04 4:709:02 4:708:03 4:707:03

Heap

Page 707 Page 708 Page 709 01 02 03 04 ... ... ... ... ... ... Akhtar Funk Smith Martin ... Page 704 Page 705 Page 706 01 02 03 ... ... ... ... ... ... ... Conn Funk White ... ... 01 02 03 ... ... ... ... ... ... ... Rudd White Barr ... ... 01 02 03 04 05 ... ... ... ... ... Smith Ota Jones Corets Nash 01 02 03 04 ... ... ... ... ... ... Martin Phua Jones Smith ... 01 02 03 ... ... ... ... ... ... ... Ganio Jones Hall ... ... Martin Martin Ota Phua Rudd 4:708:01 4:706:04 4:707:02 4:708:02 4:705:01

Non Clustered Index

File ID #4

id indid = 2 root sysindexes

UPDATE member SET Address = <something long> WHERE lastname = 'Ota'

Non-Leaf Level

Page 12 - Root Page 37 Page 28

Leaf Level

(Key Value) Page 41 Page 51 Page 61 Page 71 Akhtar ... Martin Akhtar Barr Con Funk Funk 4:706:01 4:705:03 4:704:01 4:706:02 4:704:02 Martin Smith ... Smith Smith Smith White White 4:706:03 4:708:04 4:707:01 4:704:03 4:705:02 Akhtar Ganio ... Ganio Hall Jones Jones Jones 4:709:01 4:709:04 4:709:02 4:708:03 4:707:03

Heap

Page 707 Page 708 Page 709 01 02 03 04 ... ... ... ... ... ... Akhtar Funk Smith Martin ... Page 704 Page 705 Page 706 01 02 03 ... ... ... ... ... ... ... Conn Funk White ... ... 01 02 03 ... ... ... ... ... ... ... Rudd White Barr ... ... 01 02 03 04 05 ... ... ... ... ... Smith Ota Jones Corets Nash 01 02 03 04 ... ... ... ... ... ... Martin Phua Jones Smith ... 01 02 03 ... ... ... ... ... ... ... Ganio Jones Hall ... ... Martin Martin Ota Phua Rudd 4:708:01 4:706:04 4:707:02 4:708:02 4:705:01

Non clustered Index

File ID #4 02 02 ... ... Ota Ota 02 02 ... ... Ota Ota Martin Martin Martin Martin Ota Ota 4:707:02 4:707:02 02 02 Ota Ota 04 04 ... ... Ota Ota

slide-20
SLIDE 20

How SQL Server Updates Rows

 An Update Generally Does Not Cause a Row to Move  An Update Can Be a Delete Followed by an Insert  Batch Updates Touch Each Index Only Once

slide-21
SLIDE 21

How SQL Server Deletes Rows

Heap records move individually Clustered index pages move as a unit

 How Deletes Cause Ghost Records  How SQL Server Reclaims Space  How Files Can Shrink

slide-22
SLIDE 22

 Deciding Which Columns to Index

 Understanding the Data  Indexing Guidelines  Choosing the Appropriate Clustered Index  Indexing to Support Queries  Determining Selectivity  Determining Density  Determining Distribution of Data

slide-23
SLIDE 23

Understanding the Data

 Logical and Physical Design  Data Characteristics  How Data Is Used

 The types of queries performed  The frequency of queries that are typically performed

slide-24
SLIDE 24

Indexing Guidelines

 Columns to Index

 Primary and foreign keys  Those frequently searched in ranges  Those frequently accessed in sorted order  Those frequently grouped together during aggregation

 Columns Not to Index

 Those seldom referenced in queries  Those that contain few unique values  Those defined with text, ntext, or image data types

slide-25
SLIDE 25

Choosing the Appropriate Clustered Index

 Heavily Updated Tables

 A clustered index with an identity column keeps

updated pages in memory

 Sorting

 A clustered index keeps the data pre-sorted

 Column Length and Data Type

 Limit the number of columns  Reduce the number of characters  Use the smallest data type possible

slide-26
SLIDE 26

Indexing to Support Queries

 Using Search Arguments  Writing Good Search Arguments

 Specify a WHERE clause in the query  Verify that the WHERE clause limits the number of rows  Verify that an expression exists for every table

referenced in the query

 Avoid using leading wildcards

slide-27
SLIDE 27

Determining Selectivity

High selectivity

member_no member_no 1 2 . . last_name last_name first_name first_name Randall Flood Joshua Kathie . 10000 Anderson Bill SELECT * FROM member WHERE member_no > 8999 1000 10000 = 10% Number of rows meeting criteria Total number of rows in table =

Low selectivity

member_no member_no 1 2 . . last_name last_name first_name first_name Randall Flood Joshua Kathie . 10000 Anderson Bill SELECT * FROM member WHERE member_no < 9001 9000 10000 = 90% Number of rows meeting criteria Total number of rows in table =

Good candidate for Index? Good candidate for Index?

slide-28
SLIDE 28

Determining Density

last_name last_name first_name first_name Randall . . . Joshua Randall Cynthia Randall Tristan . . . Ota Lani . . . SELECT * FROM member WHERE last_name = ‘Ota’

Low Density

SELECT * FROM member WHERE last_name = ‘Randall’

High Density

slide-29
SLIDE 29

Determining Distribution of Data

Standard Distribution of Values

F - J A - E K - O P - U V - Z Last Name Number

  • f

Last Names

Even Distribution of Values

Last Name Number

  • f

Last Names C - F A - B G - K L - N O - Z

slide-30
SLIDE 30

Recommended Practices

Use Indexes to Enforce Uniqueness Drop Unused Indexes Avoid Long Clustering Keys Create Indexes on Columns That Join Tables Consider Using a Clustered Index to Support Sorting and Range Searches Create Indexes That Support Search Arguments

slide-31
SLIDE 31

Review

 Introduction to Indexes  Index Architecture  How SQL Server Retrieves Stored Data  How SQL Server Maintains Index and Heap Structures  Deciding Which Columns to Index