You are on page 1of 79

Lecture 05 and 06:

Conventional Indexes
Long Cheng
Assistant Professor
c.long@ntu.edu.sg

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 1


Motivation

A block 1 1500 …
2 1200 …
E# Salary ...
1. Each block holds 2 records 1 1500 ...
3 2100 …
2. Blocks are physically 3 2100 ...
4 1800 …
4 1800 ...
contiguous on the disk 2 1200 ...
6 2300 …
3. The records are stored in an 6 2300 ...
8 1900 …
ascending order of E# 9 1200 ...
8 1900 ...
(within blocks and across … … ...
9 1200 …
… … …
blocks)


Table R
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 2


Motivation
What’s the salary of the 1 1500 …
employee with the E# of 9? 2 1200 …

3 2100 …
SELECT Salary FROM R 4 1800 …
WHERE E# = 9
6 2300 …
8 1900 …

9 1200 …
… … …
4 I/Os


Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 3


Motivation
What’s the salary of the 1 1500 …
employee with the E# of N? 2 1200 …

3 2100 …
SELECT Salary FROM R 4 1800 …
WHERE E# = N
6 2300 …
8 1900 …

9 1200 …
Many I/Os
… … …
(All blocks will be accessed in
the worst case)


Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 4


Motivation

How can we find the tuple with a targeted key


quickly?

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 5


Motivation

1. Suppose you want to read


the part related to indexes
in the book
2. How to find that part fast?

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 6


Motivation

We use the index!!


14 Index Structures … 619

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 7


Types of Indexes in SQL Server

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 8


Types of Indexes in Oracle

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 9


Introduction – Index at the Record Level
What’s the salary of the 1
1 1500 …
employee with the E# of 9? 2
2 1200 …

3 3 2100 …
SELECT Salary FROM R 4 4 1800 …
WHERE E# = 9 6 6 2300 …
8 8 1900 …
9
9 1200 …
We build an index … … …


(over the E#)


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 10


Introduction – Index at the Record Level
1. Corresponds to an array
1 1500 …
2. Each entry is a (search key, 1
2 1200 …
pointer) 2
• corresponds to a record 3 3 2100 …
4 1800 …
• pointer: the address to 4

the record 6 6 2300 …


• search key: the value of 8 8 1900 …

the record’s attribute the 9


9 1200 …
index is based on (e.g., … … …
E#)


3. Entries are sorted by the


Index
Data (on disk)
key
DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 11
Introduction – Index at the Record Level
A block
1 1500 …
1
1. Each entry corresponds to 2 1200 …
2
one record 3 3 2100 …
2. Much more entries than 4 4 1800 …
records can be fit in one
6 6 2300 …
block 8 8 1900 …
• 4 entries / block (index) 9
• 2 records / block (data) 9 1200 …
… … …
3. Index occupies much fewer


blocks


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 12


Query with an Index at the Record Level
What’s the salary of the 1
1 1500 …
employee with the E# of 9? 2
2 1200 …

3 3 2100 …
SELECT Salary FROM R 4 4 1800 …
WHERE E# = 9 6 6 2300 …
8 8 1900 …
9
9 1200 …
3 I/Os
… … …
(less than that when no index is


used)


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 13


Introduction – Index at the Block Level
1 1500 …
1
2 1200 …
2
3 3 2100 …
4 4 1800 …
1. This index is at the record
level 6 6 2300 …
2. We can also do the block 8 8 1900 …

level 9
9 1200 …
… … …


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 14


Introduction – Index at the Block Level
1. Corresponds to an array
2. Each entry is a (search key, 1 1500 …
1
pointer) 3
2 1200 …
• corresponds to a (data) 6
3 2100 …
block 9
4 1800 …
• pointer: to the first
6 2300 …
record of the block 8 1900 …
• search key: the value of
the first record’s 9 1200 …

attribute the index is


based on (e.g., E#)


Index
3. Entries are sorted by the Data (on disk)
key DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 15
Introduction – Index at the Block Level
1 1500 …
1
2 1200 …
3
6
3 2100 …
1. Each entry corresponds to 9
4 1800 …
one (data) block
6 2300 …
2. Index occupies fewer blocks 8 1900 …
(than an index at the record
level) 9 1200 …


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 16


Query with an Index at the Block Level
What’s the salary of the 1
1 1500 …
employee with the E# of 9? 3
2 1200 …

6
3 2100 …
9
SELECT Salary FROM R 4 1800 …
WHERE E# = 9
6 2300 …
8 1900 …

9 1200 …
2 I/Os
(less than that when no index is


used)


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 17


Query with an Index at the Block Level
What’s the salary of the 1
1 1500 …
employee with the E# of 4? 3
2 1200 …

6
3 2100 …
9
SELECT Salary FROM R 4 1800 …
WHERE E# = 4
6 2300 …
8 1900 …

9 1200 …

2 I/Os


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 18


Index at the Block Level

Can we ALWAYS build an index at the block level?

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 19


Sequential File v.s. Non-Sequential File
1 1500 … 1 1500 …
2 1200 … 3 2100 …

3 2100 … 4 1800 …
4 1800 … 2 1200 …
• Sequential file: the records
are stored in a sorted order 6 2300 … 6 2300 …
wrt a key value 8 1900 … 9 1200 …

9 1200 … 8 1900 …
• Non-sequential file: the
other case


Data (on disk) Data (on disk)
Sequential Non-sequential
(wrt E#)
DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 20
Index at the Block Level
1 1500 …
1
2 1200 …
3
6
3 2100 …
9
4 1800 …

6 2300 …
It works for sequential file 8 1900 …

9 1200 …


Index
Data (on disk)
Sequential
DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 21
Index at the Block Level
What’s the salary of the 1
1 1500 …
employee with the E# of 9? 4
3 2100 …

6
4 1800 …
8
SELECT Salary FROM R 2 1200 …
WHERE E# = 9
6 2300 …
9 1200 …
Target is missed!
8 1900 …
Do not work for non-sequential


file! 


Index
Data (on disk)
Non-sequential
DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 22
Index at the Record Level

Can we ALWAYS build an index at the record


level?

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 23


Index at the Record Level
1 1500 …
1
2 1200 …
2
3 3 2100 …
4 4 1800 …

Yes when the data is a 6 6 2300 …


sequential file 8 8 1900 …
9
9 1200 …


Index
Data (on disk)
Sequential
DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 24
Index at the Record Level
1 1500 …
1
3 2100 …
2
3 4 1800 …
4 2 1200 …

How about when the data is a 6 6 2300 …


non-sequential file? 8 9 1200 …
9
8 1900 …


Index
Data (on disk)
Non-sequential
DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 25
Index at the Record Level
What’s the salary of the 1
1 1500 …
employee with the E# of 9? 2
3 2100 …

3 4 1800 …
SELECT Salary FROM R 4 2 1200 …
WHERE E# = 9 6 6 2300 …
8 9 1200 …
9
8 1900 …

It works (with 3 I/Os)


Index
Data (on disk)
Non-sequential
DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 26
Index at the Record Level

Yes
Can we ALWAYS build an index
(for both sequential and non-
at the record level?
sequential data files)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 27


Comparisons between Indexes at Record and Block Levels

1. Block level
2. Smaller index (fewer
Dense blocks)
index 3. For sequential file only

1. Record level Sparse


2. Larger index (more blocks) index
3. For both sequential and
non-sequential file
DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 28
Question 1

Size of a dense index?


Suppose (the number of index blocks)
• A table has 100 tuples
and is stored sequentially
• A block can hold
• 5 records OR
• 10 key-pointer pairs
100 / 10 = 10

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 29


Question 2

Size of a sparse index?


Suppose (the number of index blocks)
• A table has 100 tuples
and is stored sequentially
• A block can hold
• 5 records OR
• 10 key-pointer pairs
100 / 5 / 10 = 2

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 30


Update on Index

1. Locate the targeted record


(deletion) or the place to
How to update an index hold the targeted record
following a deletion or insertion (insertion)
of a record? 2. Update the data file
3. Update the index
accordingly

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 31


Dense index + deletion
Dense index 2 I/Os
1 1500 …
1
2 1200 …
2
Deletion of the record “3” 3 3 2100 …
4 4 1800 …

6 6 2300 …
8 8 1900 …
9
9 1200 …
… … …
Find the target record


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 32


Dense index + deletion
Dense index 2 I/Os
1 1500 …
1
2 1200 …
2
Deletion of the record “3” 3 3 2100 …
4 4 1800 …

6 6 2300 …
8 8 1900 …
9
9 1200 …
… … …
Remove the target record


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 33


Dense index + deletion
Dense index 2 I/Os
1 1500 …
1
2 1200 …
2
Deletion of the record “3” 3
4 1800 …
4

6 6 2300 …
8 8 1900 …
9
9 1200 …
Shift the following records … … …


forward


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 34


Dense index + deletion
Dense index 2 I/Os
1 1500 …
1
2 1200 …
2
Deletion of the record “3” 3 4 1800 …
4

6 6 2300 …
8 8 1900 …
9
9 1200 …
Shift the following records … … …


forward


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 35


Dense index + deletion
Dense index 2 I/Os
1 1500 …
1
2 1200 …
2
Deletion of the record “3” 3 4 1800 …
4

6 6 2300 …
8 8 1900 …
9
9 1200 …
… … …
Update the index


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 36


Dense index + deletion
Dense index 2 I/Os
1 1500 …
1
2 1200 …
2
Deletion of the record “3” 4 4 1800 …

6 6 2300 …
8 8 1900 …
9
9 1200 …
… … …
Update the index


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 37


Dense index + deletion
We ignore the I/Os forDense
writing blocks back
index 2 I/Os
1 1500 …
1
2 1200 …
2
Deletion of the record “3” 4 4 1800 …

6 6 2300 …
8 8 1900 …
9
9 1200 …
… … …
Done!


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 38


Sparse + deletion
Sparse index 2 I/Os
1 1500 …
1
2 1200 …
3
Deletion of the record “3” 6
3 2100 …
9
4 1800 …

6 2300 …
8 1900 …

9 1200 …

Find the target record


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 39


Sparse + deletion
Sparse index 2 I/Os
1 1500 …
1
2 1200 …
3
Deletion of the record “3” 6
3 2100 …
9
4 1800 …

6 2300 …
8 1900 …

9 1200 …

Remove the record


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 40


Sparse + deletion
Sparse index 2 I/Os
1 1500 …
1
2 1200 …
3
Deletion of the record “3” 6
9 4 1800 …

6 2300 …
8 1900 …

9 1200 …

Remove the record


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 41


Sparse + deletion
Sparse index 2 I/Os
1 1500 …
1
2 1200 …
3
Deletion of the record “3” 6
9 4 1800 …

6 2300 …
8 1900 …

9 1200 …
Shift the following records


forward


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 42


Sparse + deletion
Sparse index 2 I/Os
1 1500 …
1
2 1200 …
3
Deletion of the record “3” 6
4 1800 …
9

6 2300 …
8 1900 …

9 1200 …
Shift the following records


forward


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 43


Sparse + deletion
Sparse index 2 I/Os
1 1500 …
1
2 1200 …
3
Deletion of the record “3” 6
4 1800 …
9

6 2300 …
8 1900 …

9 1200 …

Update the index


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 44


Sparse + deletion
Sparse index 2 I/Os
1 1500 …
1
2 1200 …
4
Deletion of the record “3” 6
4 1800 …
9

6 2300 …
8 1900 …

9 1200 …

Update the index


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 45


Sparse + deletion
Sparse index 2 I/Os
1 1500 …
1
2 1200 …
4
Deletion of the record “3” 6
4 1800 …
9

6 2300 …
8 1900 …

9 1200 …

Done!


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 46


Dense index + insertion
Dense index 3 I/Os
Suppose we would like to 1 1500 …
1
maintain a sequential file 2
2 1200 …

Insertion of the record “5” 3 3 2100 …


4 4 1800 …
We always find the first key
that is larger than the target 6 6 2300 …
and follow the corresponding 8 8 1900 …

pointer 9
9 1200 …
Find the place to insert the … … …


target record


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 47


Dense index + insertion
Dense index 3 I/Os
1 1500 …
1
2 1200 …
2
Insertion of the record “5” 3 3 2100 …
4 4 1800 …

6 6 2300 …
8 8 1900 …
9
9 1200 …

Shift some records backward


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 48


Dense index + insertion
Dense index 4 I/Os
1 1500 …
1
2 1200 …
2
Insertion of the record “5” 3 3 2100 …
4 4 1800 …

6
6 2300 …
8
9
8 1900 …
9 1200 …
Shift some records backward


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 49


Dense index + insertion
Dense index 4 I/Os
1 1500 …
1
2 1200 …
2
Insertion of the record “5” 3 3 2100 …
4 4 1800 …

6
6 2300 …
8
9
8 1900 …
9 1200 …
Insert the target record


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 50


Dense index + insertion
Dense index 4 I/Os
1 1500 …
1
2 1200 …
2
Insertion of the record “5” 3 3 2100 …
4 4 1800 …

6 5 … …
8 6 2300 …
9
8 1900 …
9 1200 …
Insert the target record


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 51


Dense index + insertion
Dense index 4 I/Os
1 1500 …
1
2 1200 …
2
Insertion of the record “5” 3 3 2100 …
4 4 1800 …

6 5 … …
8 6 2300 …
9
8 1900 …
9 1200 …
Update the index


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 52


Dense index + insertion
Dense index 4 I/Os
1 1500 …
1
2 1200 …
2
Insertion of the record “5” 3 3 2100 …
4 4 1800 …

5 5 … …
6 6 2300 …
8
9 8 1900 …
9 1200 …
Update the index


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 53


Dense index + insertion
Dense index 4 I/Os
1 1500 …
1
2 1200 …
2
Insertion of the record “5” 3 3 2100 …
4 4 1800 …

5 5 … …
6 6 2300 …
8
9 8 1900 …
9 1200 …
Done!


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 54


Sparse index + insertion
Sparse index 4 I/Os
1 1500 …
Insertion of the record “5” 1
2 1200 …
3
1. We find the last key that is 6
3 2100 …
smaller than the target 9
4 1800
and follow the pointer
2. (If necessary) We find the 6 2300 …
8 1900 …
first key that is larger than
the target and follow the 9 1200 …
pointer


Similar to the case of dense


index Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 55


Sparse index + insertion
Sparse index 4 I/Os
1 1500 …
Insertion of the record “5” 1
2 1200 …
3
1. We find the last key that is 5
3 2100 …
smaller than the target 8
4 1800
and follow the pointer
2. (If necessary) We find the 5 … …
6 2300 …
first key that is larger than
the target and follow the 8 1900 …
pointer 9 1200 …


Similar to the case of dense


index Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 56


Types of Indexes in SQL Server

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 57


Clustered Index
Clustered index
1 1500 …
1
2 1200 …
2
3 3 2100 …
4 4 1800 …
• Clustered index:
6 6 2300 …
• Sequential file (wrt a key)
8 8 1900 …
• Index is on the same key 9
9 1200 …
… … …
Arrows do not cross


Similar keys are stored nearby Index
Data (on disk)
(i.e., clustered) Sequential (wrt E#)
DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 58
Clustered Index
Clustered index
1 1500 …
1
2 1200 …
3
6
3 2100 …
9
4 1800 …
• Clustered index:
6 2300 …
• Sequential file (wrt a key)
8 1900 …
• Index is on the same key
9 1200 …

Arrows do not cross


Index
Data (on disk)
Sequential (wrt E#)
DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 59
Create a Clustered Index in SQL Server

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 60


Non-clustered Index
• A non-clustered index is an Non-clustered index
1 1500 …
index that is not a clustered 1
3 2100 …
one. 2
• Case 1: Non-sequential 3 4 1800 …
4 2 1200 …
file
• Case 2: Sequential file 6 6 2300 …
(wrt Key 1) but index (on 8 9 1200 …

Key 2) 9
8 1900 …

Some arrows cross


Index
Data (on disk)
Similar keys may be scattered
Non-Sequential
(i.e., non-clustered) DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 61
Non-clustered Index
• A non-clustered index is an Non-clustered index
1 1200 …
index that is not a clustered 1200
2 1800 …
one. 1500
• Case 1: Non-sequential 1800 3 1900 …
1900 4 1500 …
file
• Case 2: Sequential file 2100 6 2100 …
(wrt Key 1) but index (on 2300 8 2500 …

Key 2) 2500
9 2300 …

Some arrows cross


Index
Data (on disk)
Similar keys may be scattered
Sequential (E#)
(i.e., non-clustered) DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 62
Create a Non-clustered Index in SQL Server

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 63


Clustered Index vs Non-clustered Index
Clustered index 4 I/Os
1 1500 …
1
2 1200 …
2
3 3 2100 …
4 4 1800 …
What are the salaries of the
employees with the E# in [3,8]? 6 6 2300 …
SELECT Salary FROM R 8 8 1900 …

WHERE E# 3 and E# 8 9
9 1200 …
… … …


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 64


Clustered Index vs Non-clustered Index
Non-clustered index 6 I/Os
1 1500 …
1
3 2100 …
2
3 4 1800 …
4 2 1200 …
What are the salaries of the
employees with the E# in [3,8]? 6 6 2300 …
SELECT Salary FROM R 8 9 1200 …

WHERE E# 3 and E# 8 9
8 1900 …


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 65


Clustered Index vs Non-clustered Index

Should we always use clustered indexes?

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 66


Clustered Index vs Non-clustered Index
Clustered index
1 1500 …
1
2 1200 …
2
Insertion of the record “5” 3 3 2100 …
4 4 1800 …

6 6 2300 …
8 8 1900 …
9
9 1200 …
A few steps … … …


(locating, updating, shifting …)


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 67


Clustered Index vs Non-clustered Index
Clustered index 4 I/Os
1 1500 …
1
2 1200 …
2
Insertion of the record “5” 3 3 2100 …
4 4 1800 …

5 5 … …
6 6 2300 …
8
9 8 1900 …
9 1200 …
At the end


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 68


Clustered Index vs Non-clustered Index
Non-clustered index 3 I/Os
1 1500 …
1
3 2100 …
2
Insertion of the record “5” 3 4 1800 …
4 2 1200 …
We maintain for each entry
6 6 2300 …
whether the corresponding
8 9 1200 …
block is full, which is light 9
8 1900 …
Find the place to insert the


target record


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 69


Clustered Index vs Non-clustered Index
Non-clustered index 3 I/Os
1 1500 …
1
3 2100 …
2
Insertion of the record “5” 3 4 1800 …
4 2 1200 …

6 6 2300 …
8 9 1200 …
9
8 1900 …

Insert the target record


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 70


Clustered Index vs Non-clustered Index
Non-clustered index 3 I/Os
1 1500 …
1
3 2100 …
2
Insertion of the record “5” 3 4 1800 …
4 2 1200 …

6 6 2300 …
8 9 1200 …
9
8 1900 …
5 … …
Insert the target record


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 71


Clustered Index vs Non-clustered Index
Non-clustered index 3 I/Os
1 1500 …
1
3 2100 …
2
Insertion of the record “5” 3 4 1800 …
4 2 1200 …

6 6 2300 …
8 9 1200 …
9
8 1900 …
5 … …
Update the index


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 72


Clustered Index vs Non-clustered Index
Non-clustered index 3 I/Os
1 1500 …
1
3 2100 …
2
Insertion of the record “5” 3 4 1800 …
4 2 1200 …

5 6 2300 …
6 9 1200 …
8
9 8 1900 …
5 … …
Update the index


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 73


Clustered Index vs Non-clustered Index
Non-clustered index 3 I/Os
1 1500 …
1
3 2100 …
2
Insertion of the record “5” 3 4 1800 …
4 2 1200 …

5 6 2300 …
6 9 1200 …
8
9 8 1900 …
5 … …
Done!


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 74


Clustered Index vs Non-clustered Index
• Updating non-clustered
indexes involves no shifting
• Less costly

• Updating clustered indexes


usually involves shifting
• More costly
DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 75
Multi-layer Index
1 1500 …
What if there are many index 1
2 1200 …
2
blocks?
3 3 2100 …
4 4 1800 …

Could be many I/Os for 6 6 2300 …


accessing the index blocks 8 8 1900 …
9
9 1200 …
… … …


Build index of indexes!!


Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 76


No! The dense index would always have the Does it make sense to build
same size as the one at the 1st level. a dense index at the 2nd
Multi-layer Index level?
Sparse index 2nd level 1 level
Dense index
st

1 1500 …
1 1
2 1200 …
Search for the 6 2
3 3 2100 …
record with key = 6
4 4 1800 …


6 6 2300 …
8 8 1900 …
9
3 I/Os 9 1200 …
(even there are … … …


many blocks at the


1st level) Index Index
Data (on disk)

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 77


Recap
• Conventional indexes (dense vs. sparse)
• Queries with indexes
• Updates on indexes
• Clustered vs Non-clustered indexes
• Multiple level indexes

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 78


Questions?

Next lecture:
Lecture 07 and 08: B Tree Index

DATABASE SYSTEM PRINCIPLES: Lecture 05: Conventional Indexes 79

You might also like