Professional Documents
Culture Documents
Outline
Data Storage
storing data: disks buffer manager representing data external sorting
Indexing
types of indexes B+ trees hash tables (maybe)
2
Physical Addresses
Each block and each record have a physical address that consists of:
The host The disk The cylinder number The track number The block within the track For records: an offset in the block
sometimes this is in the blocks header
Logical Addresses
Logical address: a string of bytes (10-16) More flexible: can blocks/records around But need translation table:
Logical address L1 L2 L3
Physical address P1 P2 P3
4
Memory address M1 M2 M3
Logical address L1 L2 L3
5
In databases time is dominated by I/O cost Assumption: cost is given only by I/O Consequence: need to redesign certain algorithms Will illustrate here with sorting
Sorting
Illustrates the difference in algorithm design when your data is not in main memory:
Problem: sort 1Gb of data with 1Mb of RAM.
Pass 2, 3, , etc.:
three buffer pages used.
INPUT 1 OUTPUT INPUT 2
Disk
Disk
8
2,3 4,6
! log2 N 1
So total cost is:
4-page runs
2 N log 2 N 1
Improvement: start with larger runs Sort 1GB with 1MB memory in 10 passes
8-page runs
Can We Do Better ?
We have more main memory Should use it to improve performance
10
11
Indexes
12
Outline
Types of indexes B+ trees Hash tables (maybe)
13
Indexes
An index on a file speeds up selections on the search key field(s) Search key = any subset of the fields of a relation
Search key is not the same as key (minimal set of fields that uniquely identify a record in a relation).
14
Index Classification
Clustered/unclustered
Clustered = records sorted in the key order Unclustered = no
Dense/sparse
Dense = each record has an entry in the index Sparse = only some records have
Primary/secondary
Primary = on the primary key Secondary = on any key Some books interpret these differently
Clustered Index
File is sorted on the index attribute Dense index: sequence of (key,pointer) pairs
10 20 30 40
10 20 30 40
50 60 70 80
50 60 70 80 16
Clustered Index
Sparse index: one key per data block
10 30 50 70
10 20 30 40
50 60 70 80
17
10 10 10 20
50 60 70 80
20 20 30 40
18
10 10 10 20 20 20
Search for 20
30 40
19
10 10 10 20
50 60 70 80
30 30 40 50 20
Unclustered Indexes
To index other attributes than primary key Always dense (why ?)
10 10 20 20
20 30 30 20
20 30 30 30
10 20 10 30 21
Data Records
Data Records
CLUSTERED
UNCLUSTERED
22
B+ Trees
Search trees Idea in B Trees:
make 1 node = 1 block
Idea in B+ Trees:
Make leaves into a linked list (range queries are easier)
24
B+ Trees Basics
Parameter d = the degree Each node has >= d and <= 2d keys (except root)
30 120 240
Keys k < 30
Keys 30<=k<120
Keys 120<=k<240
Keys 240<=k
Next leaf
40 50 60
25
B+ Tree Example
d=2
80
20
60
100
120
140
10
15
18
20
30
40
50
60
65
80
85
90
10
15
18
20
30
40
50
60
65
80
85
90 26
B+ Tree Design
How large d ? Example:
Key size = 4 bytes Pointer size = 8 bytes Block size = 4096 byes
27
Searching a B+ Tree
Exact key values:
Start at the root Proceed down, to the leaf
Select name From people Where age = 25
Range queries:
As above Then sequential traversal
Select name From people Where 20 <= age and age <= 30
28
B+ Trees in Practice
Typical order: 100. Typical fill-factor: 67%.
average fanout = 133
Typical capacities:
Height 4: 1334 = 312,900,700 records Height 3: 1333 = 2,352,637 records
29
Insertion in a B+ Tree
Insert (K, P) Find leaf where K belongs, insert If no overflow (2d keys or less), halt If overflow (2d+1 keys), split node, insert in parent:
(K3, ) to parent
K1 P0 K2 P1 P2 K3 P3 K4 P4 K5 p5 K1 P0 P1 K2 P2 K4 P3 P4 K5 p5
If leaf, keep K3 too in right node When root splits, new root has 1 key only
30
Insertion in a B+ Tree
Insert K=19
80
20
60
100
120
140
10
15
18
20
30
40
50
60
65
80
85
90
10
15
18
20
30
40
50
60
65
80
85
90 31
Insertion in a B+ Tree
After insertion
80
20
60
100
120
140
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 32
Insertion in a B+ Tree
Now insert 25
80
20
60
100
120
140
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 33
Insertion in a B+ Tree
After insertion
80
20
60
100
120
140
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 34
Insertion in a B+ Tree
But now have to split !
80
20
60
100
120
140
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 35
Insertion in a B+ Tree
After the split
80
20
30
60
100
120
140
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 36
20
30
60
100
120
140
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 37
100
120
140
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 38
20
30
60
100
120
140
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 39
100
120
140
10
15
18
19
20
40
50
60
65
80
85
90
10
15
18
19
20
40
50
60
65
80
85
90 40
19
30
60
100
120
140
10
15
18
19
20
40
50
60
65
80
85
90
10
15
18
19
20
40
50
60
65
80
85
90 41
19
30
60
100
120
140
10
15
18
19
20
50
60
65
80
85
90
10
15
18
19
20
50
60
65
80
85
90 42
19
60
100
120
140
10
15
18
19
20
50
60
65
80
85
90
10
15
18
19
20
50
60
65
80
85
90 43
Hash Tables
Secondary storage hash tables are much like main memory ones Recall basics:
There are n buckets A hash function f(k) maps a key k to {0, 1, , n-1} Store in bucket f(k) a pointer to record with key k
0 1 2 3
e b f g a c
46
49
50
i=1
0(010)
0 1
1(011)
i=1
0(010)
0 1
1(011) 1(110)
52
i=1
0(010)
0 1
53
i=2
0(010)
00 01 10 11
54
i=2
00 01 10 11
55
00 01 10 11
56
57
58
00 01 10
(10)10
59
(10)00
00 01 10
(10)10
60
61
00 01 10
(10)10 (10)10
00 01 10 11
(01)11
62
Extension from n=4 to n=5 (new bit) Need to touch every single block (why ?)
(01)00 (11)00
i=2 (10)10
00 01 10 11
(01)11
63