Tải bản đầy đủ (.ppt) (31 trang)

SQL Server - Bài 11

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (452.04 KB, 31 trang )

Planning and Creating Index
Vu Tuyet Trinh

Hanoi University of Technology
1
MicrosoftMicrosoft
Outline

Introduction to Indexes

Understanding Index Architecture

Retrieving Stored Data with SQL Server

Defining Indexes

Creating Indexes

Maintaining Index
2
MicrosoftMicrosoft
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
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...

...
...
...
...
...
...
...
MicrosoftMicrosoft
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, or image data
types
MicrosoftMicrosoft
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
MicrosoftMicrosoft
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
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 new
rows being inserted

Connecting data pages

Reclaiming space for new rows in the heap when a row is
deleted
MicrosoftMicrosoft
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
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
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-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
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'
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-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
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
Martin
Martin
Martin
470801
470801
Martin
Martin
470604
470604
Ota
Ota
470702

470702
Phua
Phua
470802
470802
Rudd
Rudd
470501
470501
Martin
Martin
...
...
470801
470801
Phua
Phua
...
...
470802
470802
Ota
Ota
...
...
470702
470702
Martin
Martin
...

...
470604
470604
Rudd
Rudd
...
...
470501
470501
MicrosoftMicrosoft
Accessing a Range of Data in Clustering Key
1234
Index
Pages
Non-leaf
Level
Page 12 - Root
1234
...
5678
Page 37
1234
2234
...
Page 28
5678
7678
...
Page 41 Page 61
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
Page 51
2234
2334
2434
2534
2634
White
Akhtar
Jones
Con

Jones6002
Leaf Level
(Key
Value)
Clustered
Index
Akhtar
...
Martin
Page 140 - Root
Page 100 Page 110 Page 120 Page 130
1234
2334
5678
2534
1334
1534
...
Akhtar
Barr
Con
Funk
Funk
...
...
...
...
...
...
...

4705
...
7678
8078
2434
5978
2634
...
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
Smith
...
1234
Index
Pages
Non-Leaf
Level
Page 12 - Root
1234
...
5678

Page 37
1234
2234
...
Page 28
5678
7678
...
Page 41 Page 61
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

Page 51
2234
2334
2434
2534
2634
White
Akhtar
Jones
Con
Jones6002
Leaf Level
(Key
Value)
Clustered
Index
Akhtar
...
Martin
Page 140 - Root
Martin
Smith
...
Page 100 Page 110 Page 120 Page 130
1234
2334
5678
2534
1334
1534

...
Akhtar
Barr
Con
Funk
Funk
...
...
...
...
...
...
...
4705
...
7678
8078
2434
5978
2634
...
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
6078
Rudd

Rudd
5678
5678
5778
5778
5878
5878
5978
5978
Barr
Barr
Smith 8721
Smith 8721
Ota
Ota
Jones 5788
Jones 5788
5678
5678
5678
5678
Martin
Martin
Akhtar
Akhtar
Martin
Martin
Smith
Smith
6078

6078
Rudd
Rudd
...
...
5878
5878
Ota
Ota
...
...
5778
5778
Smith
Smith
...
...
8721
8721
5678
5678
Barr
Barr
...
...
5978
5978
Jones
Jones
...

...
5788
5788
Akhtar
Akhtar
Ganio
Ganio
...
...
MicrosoftMicrosoft
Managing Indexes

Creating Indexes

Creating Unique Indexes

Creating Composite Indexes

Obtaining Information on Existing Indexes

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×