Title: CS 245: Database System Principles Notes 4: Indexing
1CS 245 Database System PrinciplesNotes 4
Indexing
2Chapter 4
record
?
value
3Topics
- Conventional indexes
- B-trees
- Hashing schemes
4Sequential File
5Sequential File
Dense Index
6Sequential File
Sparse Index
7Sequential File
Sparse 2nd level
8- Comment
- FILE,INDEX may be contiguous
- or not (blocks chained)
9Question
- Can we build a dense, 2nd level index for a dense
index?
10Notes on pointers
- (1) Block pointer (sparse index) can be smaller
than record pointer - BP
- RP
11Notes on pointers
- (2) If file is contiguous, then we can omit
- pointers (i.e., compute them)
12K1
K2
K3
K4
13K1
K2
K3
K4
14Sparse vs. Dense Tradeoff
- Sparse Less index space per record can
keep more of index in memory - Dense Can tell if any record exists
without accessing file - (Later
- sparse better for insertions
- dense needed for secondary indexes)
15Terms
- Index sequential file
- Search key ( ? primary key)
- Primary index (on Sequencing field)
- Secondary index
- Dense index (all Search Key values in)
- Sparse index
- Multi-level index
16Next
- Duplicate keys
- Deletion/Insertion
- Secondary indexes
17Duplicate keys
18Dense index, one way to implement?
Duplicate keys
10
10
10
10
10
10
20
20
20
20
30
30
30
30
30
30
19Duplicate keys
Dense index, better way?
10
20
30
40
20Duplicate keys
Sparse index, one way?
10
10
20
30
21Duplicate keys
Sparse index, one way?
10
10
20
30
22Duplicate keys
Sparse index, another way?
- place first new key from block
10
20
30
30
23Duplicate keys
Sparse index, another way?
- place first new key from block
10
20
30
30
24 Duplicate values, primary index
Summary
- Index may point to first instance of each value
only - File
- Index
a
a
a
. .
b
25Deletion from sparse index
10
30
50
70
90
110
130
150
26Deletion from sparse index
10
30
50
70
90
110
130
150
27Deletion from sparse index
10
30
50
70
90
110
130
150
28Deletion from sparse index
10
30
50
70
90
110
130
150
29Deletion from sparse index
10
30
50
70
90
110
130
150
30Deletion from sparse index
10
30
50
70
90
110
130
150
31Deletion from sparse index
10
30
50
70
90
110
130
150
32Deletion from sparse index
10
30
50
70
90
110
130
150
33Deletion from dense index
10
20
30
40
50
60
70
80
34Deletion from dense index
10
20
30
30
40
40
50
60
70
80
35Deletion from dense index
10
20
30
30
40
40
50
60
70
80
36Deletion from dense index
10
20
30
30
40
40
50
60
70
80
37Insertion, sparse index case
10
30
40
60
38Insertion, sparse index case
10
30
40
60
39Insertion, sparse index case
10
30
40
60
40Insertion, sparse index case
10
30
40
60
41Insertion, sparse index case
10
30
40
60
42Insertion, sparse index case
10
30
40
60
- Illustrated Immediate reorganization
- Variation
- insert new block (chained file)
- update index
43Insertion, sparse index case
10
30
40
60
44Insertion, sparse index case
10
30
40
60
45Insertion, dense index case
- Similar
- Often more expensive . . .
46Secondary indexes
Sequence field
47Secondary indexes
Sequence field
48Secondary indexes
Sequence field
49Secondary indexes
Sequence field
50Secondary indexes
Sequence field
51Secondary indexes
Sequence field
52With secondary indexes
- Lowest level is dense
- Other levels are sparse
53Duplicate values secondary indexes
54Duplicate values secondary indexes
one option...
55Duplicate values secondary indexes
one option...
- Problem
- excess overhead!
- disk space
- search time
56Duplicate values secondary indexes
another option...
10
57Duplicate values secondary indexes
another option...
10
Problem variable size records in index!
58Duplicate values secondary indexes
?
?
Another idea (suggested in class)Chain records
with same key?
?
?
59Duplicate values secondary indexes
?
?
Another idea (suggested in class)Chain records
with same key?
?
?
- Problems
- Need to add fields to records
- Need to follow chain to know records
60Duplicate values secondary indexes
buckets
61Why bucket idea is useful
- Indexes Records
- Name primary EMP (name,dept,floor,...)
- Dept secondary
- Floor secondary
62Query Get employees in (Toy Dept) (2nd
floor)
63Query Get employees in (Toy Dept) (2nd
floor)
? Intersect toy bucket and 2nd Floor
bucket to get set of matching EMPs
64This idea used in text information retrieval
...the cat is fat ...
...was raining cats and dogs...
...Fido the dog ...
65This idea used in text information retrieval
...the cat is fat ...
...was raining cats and dogs...
...Fido the dog ...
66IR QUERIES
- Find articles with cat and dog
- Find articles with cat or dog
- Find articles with cat and not dog
67IR QUERIES
- Find articles with cat and dog
- Find articles with cat or dog
- Find articles with cat and not dog
- Find articles with cat in title
- Find articles with cat and dog within 5
words
68Common technique more info in inverted
list
position
location
type
d1
Title
5
Author
10
Abstract
57
d2
d3
dog
Title
100
Title
12
69Posting an entry in inverted list. Represents
occurrence of term in article
- Size of a list 1 Rare words or
- (in postings) miss-spellings
- 106 Common words
Size of a posting 10-15 bits (compressed)
70IR DISCUSSION
- Stop words
- Truncation
- Thesaurus
- Full text vs. Abstracts
- Vector model
71Vector space model
- w1 w2 w3 w4 w5 w6 w7
- DOC lt1 0 0 1 1 0 0 gt
- Query lt0 0 1 1 0 0 0 gt
72Vector space model
- w1 w2 w3 w4 w5 w6 w7
- DOC lt1 0 0 1 1 0 0 gt
- Query lt0 0 1 1 0 0 0 gt
73- Tricks to weigh scores normalize
- e.g. Match on common word not as useful as
match on rare words...
74- How to process V.S. Queries?
- w1 w2 w3 w4 w5 w6
- Q lt 0 0 0 1 1 0 gt
75- Try Stanford Libraries
- Try Google, Yahoo, ...
76Summary so far
- Conventional index
- Basic Ideas sparse, dense, multi-level
- Duplicate Keys
- Deletion/Insertion
- Secondary indexes
- Buckets of Postings List
77Conventional indexes
- Advantage
- - Simple
- - Index is sequential file
- good for scans
Disadvantage - Inserts expensive, and/or -
Lose sequentiality balance
78- Example Index (sequential)
- continuous
- free space
10
20
30
40
50
60
70
80
90
79- Example Index (sequential)
- continuous
- free space
10
20
30
40
50
60
70
80
90
80Outline
- Conventional indexes
- B-Trees ? NEXT
- Hashing schemes
81- NEXT Another type of index
- Give up on sequentiality of index
- Try to get balance
Note This index is called B tree, but Gradiance
homeworks just call it B-tree.
82BTree Example n3
100
120 150 180
30
3 5 11
120 130
180 200
100 101 110
150 156 179
30 35
83Sample non-leaf
57 81 95
- to keys to keys to keys to keys
- lt 57 57? klt81 81?klt95 ?95
84Sample leaf node
- From non-leaf node
- to next leaf
- in sequence
57 81 95
To record with key 57 To record with key
81 To record with key 85
85In textbooks notation n3
30 35
30
35
30
30
86- Size of nodes n1 pointers
- n keys
(fixed)
87Dont want nodes to be too empty
- Use at least
- Non-leaf ?(n1)/2? pointers
- Leaf ?(n1)/2? pointers to data
88n3
- Full node min. node
- Non-leaf
- Leaf
120 150 180
30
3 5 11
30 35
counts even if null
89Btree rules tree of order n
- (1) All leaves at same lowest level (balanced
tree) - (2) Pointers in leaves point to records except
for sequence pointer
90- (3) Number of pointers/keys for Btree
Max Max Min Min ptrs keys
ptrs?data keys
Non-leaf (non-root)
n1
n
?(n1)/2?
?(n1)/2?- 1
Leaf (non-root)
n1
n
?(n1)/2?
?(n1)/2?
Root
n1
n
1
1
91Insert into Btree
- (a) simple case
- space available in leaf
- (b) leaf overflow
- (c) non-leaf overflow
- (d) new root
-
92n3
100
30
3 5 11
30 31
93n3
100
30
3 5 11
30 31
94n3
100
30
3 5 11
30 31
95n3
100
30
3 5 11
30 31
96n3
100
30
3 5 11
30 31
97n3
100
120 150 180
180 200
150 156 179
98n3
100
120 150 180
180 200
150 156 179
99n3
100
120 150 180
180 200
150 156 179
100n3
100
120 150 180
180 200
150 156 179
101n3
10 20 30
1 2 3
10 12
20 25
30 32 40
102n3
10 20 30
1 2 3
10 12
20 25
30 32 40
103n3
10 20 30
1 2 3
10 12
20 25
30 32 40
104n3
10 20 30
1 2 3
10 12
20 25
30 32 40
105Deletion from Btree
- (a) Simple case - no example
- (b) Coalesce with neighbor (sibling)
- (c) Re-distribute keys
- (d) Cases (b) or (c) at non-leaf
106- (b) Coalesce with sibling
- Delete 50
n4
10 40 100
10 20 30
40 50
107- (b) Coalesce with sibling
- Delete 50
n4
10 40 100
10 20 30
40 50
108- (c) Redistribute keys
- Delete 50
n4
10 40 100
10 20 30 35
40 50
109- (c) Redistribute keys
- Delete 50
n4
10 40 100
10 20 30 35
40 50
110- (d) Non-leaf coalesce
- Delete 37
n4
25
10 20
30 40
25 26
30 37
40 45
20 22
10 14
1 3
111- (d) Non-leaf coalesce
- Delete 37
n4
25
10 20
30 40
25 26
30 37
40 45
20 22
10 14
1 3
112- (d) Non-leaf coalesce
- Delete 37
n4
25
10 20
30 40
25 26
30 37
40 45
20 22
10 14
1 3
113- (d) Non-leaf coalesce
- Delete 37
n4
25
10 20
30 40
25 26
30 37
40 45
20 22
10 14
1 3
114Btree deletions in practice
- Often, coalescing is not implemented
- Too hard and not worth it!
115Comparison B-trees vs. static indexed
sequential file
- Ref 1 Held Stonebraker
- B-Trees Re-examined
- CACM, Feb. 1978
116- Ref 1 claims
- - Concurrency control harder in B-Trees
- - B-tree consumes more space
- For their comparison
- block 512 bytes
- key pointer 4 bytes
- 4 data records per block
117Example 1 block static index
1 data block
-
- 127 keys
- (1271)4 512 Bytes
- -gt pointers in index implicit! up to 127
- blocks
k1
k2
k3
118Example 1 block B-tree
k1
1 data block
63 keys 63x(44)8 512 Bytes -gt
pointers needed in B-tree up to 63 blocks
because index is blocks not contiguous
k2
...
k63
-
next
119Size comparison Ref. 1
- Static Index B-tree
- data data
- blocks height blocks height
- 2 -gt 127 2 2 -gt 63 2
- 128 -gt 16,129 3 64 -gt 3968 3
- 16,130 -gt 2,048,383 4 3969 -gt 250,047
4 - 250,048 -gt 15,752,961 5
120Ref. 1 analysis claims
- For an 8,000 block file, after 32,000 inserts
- after 16,000 lookups
- ? Static index saves enough accesses to allow
for reorganization
121Ref. 1 analysis claims
- For an 8,000 block file, after 32,000 inserts
- after 16,000 lookups
- ? Static index saves enough accesses to allow
for reorganization
122Ref 2 M. Stonebraker, Retrospection on a
database system, TODS, June 1980
123- DBA does not know when to reorganize
- DBA does not know how full to load pages of new
index
124- Buffering
- B-tree has fixed buffer requirements
- Static index must read several
overflow blocks to be efficient (large
variable size buffers needed for this)
125- Speaking of buffering
- Is LRU a good policy for Btree buffers?
126- Speaking of buffering
- Is LRU a good policy for Btree buffers?
? Of course not! ? Should try to keep root in
memory at all times (and perhaps some nodes
from second level)
127Interesting problem
- For Btree, how large should n be?
n is number of keys / node
128Sample assumptions
- (1) Time to read node from disk is (STn) msec.
129Sample assumptions
- (1) Time to read node from disk is (STn) msec.
(2) Once block in memory, use binary search to
locate key (a b LOG2 n) msec. For some
constants a,b Assume a ltlt S
130Sample assumptions
- (1) Time to read node from disk is (STn) msec.
(2) Once block in memory, use binary search to
locate key (a b LOG2 n) msec. For some
constants a,b Assume a ltlt S
(3) Assume Btree is full, i.e., nodes to
examine is LOGn N where N records
131?Can get f(n) time to find a record
132? FIND nopt by f(n) 0
- Answer is nopt few hundred
- (see homework for details)
133? FIND nopt by f(n) 0
- Answer is nopt few hundred
- (see homework for details)
134Exercise
S 14000
T 0.2
b 0.002
a 0
N 10000000
135N10 million records
S 14000
T 0.2
b 0.002
a 0
N 10000000
n
times in microseconds
136N100 million records
S 14000
T 0.2
b 0.002
a 0
N 10000000
n
times in microseconds
137N100 million records
S varies
T 0.2
b 0.002
a 0
N 10000000
S20000
S14000
S10000
n
times in microseconds
138Variation on Btree B-tree (no )
- Idea
- Avoid duplicate keys
- Have record pointers in non-leaf nodes
139- to record to record to record
- with K1 with K2 with K3
- to keys to keys to keys to
keys - lt K1 K1ltxltK2 K2ltxltk3 gtk3
140B-tree example n2
65 125
145 165
85 105
25 45
10 20
30 40
110 120
90 100
70 80
170 180
50 60
130 140
150 160
141B-tree example n2
65 125
145 165
85 105
25 45
10 20
30 40
110 120
90 100
70 80
170 180
50 60
130 140
150 160
142Note on inserts
- Say we insert record with key 25
10 20 30
n3
leaf
143Note on inserts
- Say we insert record with key 25
10 20 30
n3
leaf
144So, for B-trees
- MAX MIN
- Tree Rec Keys Tree Rec Keys
- Ptrs Ptrs Ptrs Ptrs
- Non-leaf
- non-root n1 n n ?(n1)/2? ?(n1)/2?-1
?(n1)/2?-1 - Leaf
- non-root 1 n n 1 ?n/2? ?n/2?
- Root
- non-leaf n1 n n 2 1 1
- Root
- Leaf 1 n n 1 1 1
145Tradeoffs
- ? B-trees have faster lookup than Btrees
- ? in B-tree, non-leaf leaf different sizes
- ? in B-tree, deletion more complicated
146Tradeoffs
- ? B-trees have faster lookup than Btrees
- ? in B-tree, non-leaf leaf different sizes
- ? in B-tree, deletion more complicated
? Btrees preferred!
147But note
- If blocks are fixed size (due to disk and
buffering restrictions) - Then lookup for Btree is actually better!!
148- Example
- - Pointers 4 bytes
- - Keys 4 bytes
- - Blocks 100 bytes (just example)
- - Look at full 2 level tree
149B-tree
- Root has 8 keys 8 record pointers 9 son
pointers - 8x4 8x4 9x4 100 bytes
150B-tree
- Root has 8 keys 8 record pointers 9 son
pointers - 8x4 8x4 9x4 100 bytes
Each of 9 sons 12 rec. pointers (12 keys)
12x(44) 4 100 bytes
151B-tree
- Root has 8 keys 8 record pointers 9 son
pointers - 8x4 8x4 9x4 100 bytes
Each of 9 sons 12 rec. pointers (12 keys)
12x(44) 4 100 bytes
2-level B-tree, Max records 12x9 8 116
152Btree
- Root has 12 keys 13 son pointers
- 12x4 13x4 100 bytes
153Btree
- Root has 12 keys 13 son pointers
- 12x4 13x4 100 bytes
Each of 13 sons 12 rec. ptrs (12 keys)
12x(4 4) 4 100 bytes
154Btree
- Root has 12 keys 13 son pointers
- 12x4 13x4 100 bytes
Each of 13 sons 12 rec. ptrs (12 keys)
12x(4 4) 4 100 bytes
2-level Btree, Max records 13x12 156
155So...
8 records
- ooooooooooooo ooooooooo
- 156 records 108 records
- Total 116
B
B
156So...
8 records
- ooooooooooooo ooooooooo
- 156 records 108 records
- Total 116
B
B
- Conclusion
- For fixed block size,
- B tree is better because it is bushier
157An Interesting Problem...
- What is a good index structure when
- records tend to be inserted with keysthat are
larger than existing values?(e.g., banking
records with growing data/time) - we want to remove older data
158One Solution Multiple Indexes
day days indexed days indexed
I1
I2 10 1,2,3,4,5 6,7,8,9,10 11 11,2,3,4,5 6,7,8
,9,10 12 11,12,3,4,5 6,7,8,9,10 13 11,12,13,4,5
6,7,8,9,10
- advantage deletions/insertions from smaller
index - disadvantage query multiple indexes
159Another Solution (Wave Indexes)
day I1 I2 I3 I4 10 1,2,3 4,5,6 7,8,9 10 11
1,2,3 4,5,6 7,8,9 10,11 12 1,2,3 4,5,6 7,8
,9 10,11, 12 13 13 4,5,6 7,8,9 10,11,
12 14 13,14 4,5,6 7,8,9 10,11,
12 15 13,14,15 4,5,6 7,8,9 10,11,
12 16 13,14,15 16 7,8,9 10,11, 12
- advantage no deletions
- disadvantage approximate windows
160Outline/summary
- Conventional Indexes
- Sparse vs. dense
- Primary vs. secondary
- B trees
- Btrees vs. B-trees
- Btrees vs. indexed sequential
- Hashing schemes --gt Next