Let’s buiId Indexes
1
Example [Reminder]
2
CName_Index
PriceDate_Index
Block #
Block #
Block #
How?
IO Aware
For big files (bigger than RAM), we need efficient data structures that work with HDD/SSD IO systems
How to build?
4
Recall
Data Layout
5
Company(CName, StockPrice, Date, Country)
Row1
Row3
Row5
Row8
Col3
Page
Row1
Row2
Row N
Row N +1
. . . . .
Row 2N
. . . . .
Page
Page … n (RAM/DIsk)
Page
CName
Page
Date
Page
Page
Price
Company
Row based storage
(aka Row Store)
Column based storage
(aka Column Store)
Logical Table
Index on row store
6
Query: Search for cname with specific price?
⇒ If ‘price’ is an indexed column, query will be fast.
⇒ ‘Price’ is search key(SKey). Values in price column are search values .
(e.g. ‘price’ == 19?)
Page
Row1
Row2
Row N
Row N +1
. . . . .
Row 2N
. . . . .
Page
Page … n (RAM/DIsk)
“Real” data layout, with full records
(including cname, prices, etc.)
Index for Price
where is 19?
where is 33?
(e.g, disk block 0x45AB)
Note: search key does not mean it’s unique. It’s what are you searching for.
(vs Primary KEYs in SQL that are unique)
Our 1st Hashing index
7
21
22
27
28
30
33
35
37
19
11
Option2:
Sorted
19
22
27
28
30
33
35
37
11
21
If sorted, can do better. Maintain locations only of smallest SValue in each block. (e.g., 11, 27, 33)
Index for Price
How it works in practice?
1. Schema designer picks a column to keep data sorted by (e.g., price). Index for that column is cheap.
2. For other columns, index will be bigger (e.g., CName)
where is 19?
where is 33?
Hash
Function
SKey = <19>?
0x7c6D
0x7c6D
Goal of Index: where is location of each SValue
19
For simplicity, we’ll only show the SValues for Price index
Example row: <’goog’, price=19, date=Oct 1, ...> as <price=19> or <19>
Option1:
Unsorted
If unsorted, maintain locations of all SValues in each block.
Index on row store
Query: Search for cname with specific price?
Page
Row1
Row2
Row N
Row N +1
. . . . .
Row 2N
. . . . .
Page
Page … n (RAM/DIsk)
“Real” data layout, with full records
(including cname, prices, etc.)
Index for Price
where is 19?
where is 33?
(e.g, block 0x45AB)
How do we store Index?
⇒ Idea: Index is just a table (rows/columns). Same ideas
If Index fits in RAM?
If Index does not fit in RAM?
Index Types
9
These data structures are “IO aware”
Real difference between structures:
costs of ops determines which index you pick and why
B+ Trees
Idea in B+ Trees
Search trees that are IO aware
What you will learn about in this section
B+ Tree Exact Search
80 | | | | ||||
| | | | |
20 | 60 | | | ||||
| | | | |
100 | 120 | 140 | | ||||
| | | | |
10 | 15 | 18 | | ||||
| | | | |
20 | 30 | 40 | 50 | ||||
| | | | |
60 | 65 | | | ||||
| | | | |
80 | 85 | 90 | | ||||
| | | | |
SKey = 30?
Row1
Row2
Row N
Row N +1
. . . . .
Row 2N
. . . . .
“Real” data layout, with full records
(including cname, prices, etc.)
Note: the pointers at the leaf level will be to the actual data records (rows).
We truncate and only display SValues for simplicity (as before)…
Data!
Index!
B+ Tree Exact Search
80 | | | | ||||
| | | | |
20 | 60 | | | ||||
| | | | |
100 | 120 | 140 | | ||||
| | | | |
10 | 15 | 18 | | ||||
| | | | |
20 | 30 | 40 | 50 | ||||
| | | | |
60 | 65 | | | ||||
| | | | |
80 | 85 | 90 | | ||||
| | | | |
10
12
15
20
28
30
40
60
63
80
84
89
SKey = 30?
30 < 80
30 in [20,60)
Data! [simplified]
Not all nodes pictured
30 in [30,40)
Searching a B+ Tree
SELECT cname
FROM Company
WHERE price = 25
SELECT cname
FROM Company
WHERE 20 <= price
AND price <= 30
B+ Tree Range Search
80 | | | | ||||
| | | | |
20 | 60 | | | ||||
| | | | |
100 | 120 | 140 | | ||||
| | | | |
10 | 15 | 18 | | ||||
| | | | |
20 | 30 | 40 | 50 | ||||
| | | | |
60 | 65 | | | ||||
| | | | |
80 | 85 | 90 | | ||||
| | | | |
10
12
15
20
28
30
40
59
63
80
84
89
SKey in [30,85]?
30 < 80
30 in [20,60)
To the data!
Not all nodes pictured
30 in [30,40)
What you will learn about in this section
B+ Tree Basics -- Root and non-leaf nodes
10 | 20 | 30 | |||
| | | |
k < 10
21
22
27
28
30
33
35
37
15
11
Parameter f = fanout
(e.g. f = 3)
Each non-leaf node has
<= f SKeys
22 | 25 | 28 | |||
| | | |
B+ Tree Basics -- Leaf nodes
10 | 20 | 30 | |||
| | | |
22 | 25 | 28 | 29 | ||||
| | | | |
Leaf nodes
32 | 34 | 37 | 38 | ||||
| | | | |
Non-leaf or internal node
12 | 17 | ||
| | |
Leaf nodes
SKey slots contain pointers to data records
21
22
27
28
30
33
35
37
15
11
Pointer to the next leaf node, for faster sequential traversal
Costs of B+ trees
B+ Tree: High Fanout = Lower IO
The fanout is defined as the number of pointers to child nodes coming out of a [leaf] node
Note that fanout is dynamic- we’ll often assume it’s constant just to come up with approximate eqns!
Cost Model for Indexes -- [Baseline simplest model]
Page
Row1
Row2
Row ...
Row...
. . . . .
Row ...
. . . . .
Page
Page … n (RAM/DIsk)
“Real” data layout, with full records
(including cname, prices, etc.)
E.g, where is 19?
where is 33?
(e.g, disk block 0x45AB)
Question: What’s physical layout? What are costs?
Let us build an index for an SKey (e.g., CName) in data
For B+ tree
Simplified cost model
N index pages (leaf)
M data
pages
‘f’ fanout
<value, record location>
Cost Model for Indexes -- [Baseline simplest model]
Page
Row1
Row2
Row ..
Row ...
. . . . .
Page
Page … n (RAM/DIsk)
“Real” data layout, with full records
(including cname, prices, etc.)
E.g, where is 19?
where is 33?
(e.g, disk block 0x45AB)
N index pages (leaf)
M
data pages
‘f’ fanout
<value, record location>
Example 1:Search Amazon’s Product Catalog
AMAZING: Worst-case for 1 Trillion SKeys
PageSize | 64KB | 64MB |
f | ~4000 | ~4 million |
h | ~4 | ~2 |
Simple Cost Model for Range Search
Fast Insertions & Self-Balancing
B+ Trees also (relatively) fast for single insertions!
However, can become bottleneck if many insertions (if fill-factor slack is used up…)
What you will learn about in this section
Clustered vs. Unclustered Index
30 | |
| |
22 | 25 | 28 | 29 | ||||
| | | | |
32 | 34 | 37 | 38 | ||||
| | | | |
19
22
27
28
30
33
35
37
30 | |
| |
22 | 25 | 28 | 29 | ||||
| | | | |
32 | 34 | 37 | 38 | ||||
| | | | |
19
22
27
28
30
33
35
37
Clustered
Unclustered
Index Entries
Data Records
(sorted data)
An index is clustered if the underlying data is ordered in the same way as the index’s data entries.
Clustered vs. Unclustered Index
Summary