Indexes
Indexes
Indexes
search-key pointer
Table scan
Read all the rows (stored on the secondary memory) one by one
Select the rows that mach the query
Index
Read (search) the index file, to select the rows that mach the query
Access only selected rows find in index
SQL server
Find if index exist, optimizer decide if it will use index for efficient
selection of the rows, or will scan the table
Index Evaluation Metrics
Clustered/unclustered
Clustered = records close in the index are close in the data
Unclustered = records close in the index may be far in the data
Primary/secondary
Sometimes means this:
Primary = includes primary key
Secondary = otherwise
Sometimes means clustered/unclustered
Dense/sparse
Dense = every key in the data appears in the index
Sparse = the index contains only some keys
B+ tree / Hash table / …
Clustered vs. Unclustered Index
Data entries
Data entries
(Index File)
(Data file)
CLUSTERED UNCLUSTERED
Clustered Index
10 10
20 20
30
40 30
40
50
60
50
70
60
80
70
80
Clustered Index
Sparse index
10 10
30 20
50
70 30
40
90
110
50
130
60
150
70
80
Unclustered Indexes
10
20
10 30
20
20 30
20
20
30
10
30
20
30
10
30
Ordered Indices
Indexing techniques:
If deleted record was the only record in the file with its particular
search-key value, the search-key is deleted from the index also.
Single-level index deletion:
Dense indices – deletion of search-key is similar to file record
deletion.
Sparse indices –
if an entry for the search key exists in the index, it is deleted by replacing the entry
in the index with the next search-key value in the file (in search-key order).
If the next search-key value already has an index entry, the entry is deleted
instead of being replaced.
Index Update: Insertion
bucket
Primary and Secondary Indices
Creation of index
create index <index-name> on <relation-name>
(<attribute-list>)
example: create index b-index on branch(branch-name)
We use create unique index for indirect specification that
search key is primary key
This is not needed if SQL support unique declaration
To drop the index
drop index <index-name>
Examples
SELECT
SELECT**
FROM
FROM Person
Person
WHERE
WHERE name
name==“Smith”
“Smith”
CREATE
CREATEINDEX
INDEX nameIndex
nameIndexON
ONPerson(name)
Person(name)
Examples (1)
SELECT
SELECT**
FROM
FROMPerson
Person
WHERE
WHEREage
age>>25
25AND
ANDage
age<<28
28
SELECT
SELECT**
And in FROM
FROM Person
Person
WHERE
WHEREage
age==55
55
B+-Tree index files
Keys k < 30
Keys 30<=k<120 Keys 120<=k<240 Keys 240<=k
Next leaf
40 50 60
B+ Tree Example
80
40 80
20 < 40 60
10 15 18 20 30 40 50 60 65 80 85 90
30 < 40 40
10 15 18 20 30 40 50 60 65 80 85 90
Insertion in a B+ Tree
Insert K=19
80
10 15 18 20 30 40 50 60 65 80 85 90
10 15 18 20 30 40 50 60 65 80 85 90
Insertion in a B+ Tree
After insertion
80
10 15 18 19 20 30 40 50 60 65 80 85 90
10 15 18 19 20 30 40 50 60 65 80 85 90
Insertion in a B+ Tree
Now insert 25
80
10 15 18 19 20 30 40 50 60 65 80 85 90
10 15 18 19 20 30 40 50 60 65 80 85 90
Insertion in a B+ Tree
After insertion
80
10 15 18 19 20 25 30 40 50 60 65 80 85 90
10 15 18 19 20 25 30 40 50 60 65 80 85 90
Insertion in a B+ Tree
10 15 18 19 20 25 30 40 50 60 65 80 85 90
10 15 18 19 20 25 30 40 50 60 65 80 85 90
Insertion in a B+ Tree
10 15 18 19 20 25 30 40 50 60 65 80 85 90
10 15 18 19 20 25 30 40 50 60 65 80 85 90
Deletion from a B+ Tree
Delete 30
80
10 15 18 19 20 25 30 40 50 60 65 80 85 90
10 15 18 19 20 25 30 40 50 60 65 80 85 90
Deletion from a B+ Tree
After deleting 30
80
May change to
40, or not
10 15 18 19 20 25 40 50 60 65 80 85 90
10 15 18 19 20 25 40 50 60 65 80 85 90
Deletion from a B+ Tree
Now delete 25
80
10 15 18 19 20 25 40 50 60 65 80 85 90
10 15 18 19 20 25 40 50 60 65 80 85 90
Deletion from a B+ Tree
After deleting 25
Need to rebalance
80
Rotate
10 15 18 19 20 40 50 60 65 80 85 90
10 15 18 19 20 40 50 60 65 80 85 90
Deletion from a B+ Tree
Now delete 40
80
10 15 18 19 20 40 50 60 65 80 85 90
10 15 18 19 20 40 50 60 65 80 85 90
Deletion from a B+ Tree
After deleting 40
Rotation not possible
Need to merge nodes
80
10 15 18 19 20 50 60 65 80 85 90
10 15 18 19 20 50 60 65 80 85 90
Deletion from a B+ Tree
Final tree
80
10 15 18 19 20 50 60 65 80 85 90
10 15 18 19 20 50 60 65 80 85 90
Observations about B+-trees
Select
Selectname
name
Range queries: From
Frompeople
people
B+ trees Where
Where20 20<=
<=age
ageand
and age
age<=
<=30
30
Use index exclusively Select
Selectdistinct
distinctage
age
From
Frompeople
people
Example B+-tree
g
2
a
3
c
Searching in a Hash Table
Search for a:
Compute h(a)=3
Read bucket 3
e
1 disk access 0
b
1
f
g
2
a
3
c
Insertion in Hash Table
e
0
b
1
f
g
2
d
a
3
c
Hash Functions
e
0
b k
1
f
It is possible to g
chain several over- 2
d
flow buckets
a
3
c
Hash Indices
Hashing can be used not only for file organization, but also for
index-structure creation.
A hash index organizes the search keys, with their associated
record pointers, into a hash file structure.
Strictly speaking, hash indices are always secondary indices
if the file itself is organized using hashing, a separate primary hash
index on it using the same search-key is unnecessary.
However, we use the term hash index to refer to both secondary
index structures and hash organized files.
Example of Hash Index
Deficiencies of Static Hashing