Find All Students With Gpa 3.0'': Can Do Binary Search On (Smaller) Index File!
Find All Students With Gpa 3.0'': Can Do Binary Search On (Smaller) Index File!
Find All Students With Gpa 3.0'': Can Do Binary Search On (Smaller) Index File!
k1 k2 kN Index File
P K P K 2 P K m Pm
0 1 1 2
Non-leaf
Pages
Leaf
Pages
Overflow
page
Primary pages
Review of Indexes
As for any index, 3 alternatives for data entries k*:
Data record with key value k
<k, rid of data record with search key value k>
<k, list of rids of data records with search key k>
Choice is orthogonal to the indexing technique used to locate data
entries k*.
Tree-structured indexing techniques support both range searches
and equality searches.
ISAM: static structure; B+ tree: dynamic, adjusts gracefully under
inserts and deletes.
Comments on ISAM
Data
Pages
20 33 51 63
10* 15* 20* 27* 33* 37* 40* 46* 51* 55* 63* 97*
10.Trees
Pages
20 33 51 63
Primary
Leaf
10* 15* 20* 27* 33* 37* 40* 46* 51* 55* 63* 97*
Pages
Pages
42*
10. Trees
Root
40
20 33 51 63
10* 15* 20* 27* 33* 37* 40* 46* 55* 63*
Data Entries
("Sequence set")
10. Trees
Example B+ Tree
Search begins at root, and key comparisons
direct it to a leaf (as in ISAM).
Search for 5*, 15*, all data entries >= 24* ...
Root
13 17 24 30
2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39*
occupancy is
guaranteed in 2* 3* 5* 7* 8*
5 13 24 30
2* 3* 5* 7* 8* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39*
17
5 13 27 30
2* 3* 5* 7* 8* 14* 16* 22* 24* 27* 29* 33* 34* 38* 39*
Observe `toss of
index entry (on right),
22* 27* 29* 33* 34* 38* 39*
and `pull down of
index entry (below).
Root
5 13 17 30
22
5 13 17 20 30
2* 3* 5* 7* 8* 14* 16* 17* 18* 20* 21* 22* 27* 29* 33* 34* 38* 39*
10. Trees
After Re-distribution
Intuitively, entries are re-distributed by `pushing
through the splitting entry in the parent node.
It suffices to re-distribute index entry with key 20;
weve re-distributed 17 as well for illustration.
Root
17
5 13 20 22 30
2* 3* 5* 7* 8* 14* 16* 17* 18* 20* 21* 22* 27* 29* 33* 34* 38* 39*
10. Trees
B+ Trees in Practice
Typical values for B+ tree parameters
Page size 8K
Key: at most 8 bytes (compression later)
Pointer: at most 4 bytes
Thus entries in index are at most 12 bytes, and a
page can hold at least 683 entries.
Occupancy: 67%, so a page can hold at least 455
entries, estimate that conservatively with 256 = 28.
Top two levels often in memory:
Top level, root of tree: 1 page = 8K bytes
Next level, 28 pages = 28 * 23K bytes = 2 Megabytes
10. Trees
B-Trees vs Hash Indexes
A typical B-tree height is 2-3
Height 0 supports 28 = 256 records
Height 2 supports 224 = 32M records
Height 3 supports 232 = 4G records
A B-tree of height 2-3 requires 2-3 I/Os
Including one I/O to access data
Assuming top two levels are in memory
Assuming alternative 2 or 3
This is why DBMSs either dont support or
dont recommend hash indexes on base tables
Though hashing is widely used elsewhere.
10. Trees
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
Bulk Loading (Contd.) 10. Trees
Root 10 20
considers locking!
3* 4* 6* 9* 10* 11* 12* 13* 20*22* 23* 31* 35* 36* 38*41* 44*
10. Trees
A Note on `Order
Order (d) concept replaced by physical space
criterion in practice (`at least half-full).
Index pages can typically hold many more entries
than leaf pages.
Variable sized records and search keys mean different
nodes will contain different numbers of entries.
Even with fixed length fields, multiple records with
the same search key value (duplicates) can lead to
variable-sized data entries (if we use Alternative (3)).
10.8.4 Effect of Inserts and Deletes on RIDs
Static Hashing
# primary pages fixed, allocated sequentially,
never de-allocated; overflow pages if needed.
h(k) mod M = bucket to which data entry with
key k belongs. (M = # of buckets)
0
h(key) mod M
1
key
h
M-1
Primary bucket pages Overflow pages
11.Hash
Example 2 2
Bucket B
00 1* 5* 21* 13*
Directory is array of size 4.
01
To find bucket for r, take 10 2
last `global depth # bits of 11 10*
Bucket C
2 2
3 2
00 1* 5* 21*13* Bucket B 000 1* 5* 21* 13* Bucket B
01 001
10 2 2
010
10* Bucket C
11 10*
011 Bucket C
100
2
DIRECTORY 101 2
Bucket D
15* 7* 19*
110 15* 7* 19* Bucket D
111
2
3
4* 12* 20* Bucket A2
DIRECTORY 4* 12* 20* Bucket A2
(`split image'
of Bucket A) (`split image'
of Bucket A)
11.Hash
Points to Note
20 = binary 10100. Last 2 bits (00) tell us r belongs in
A or A2. Last 3 bits needed to tell which.
Global depth of directory: Max # of bits needed to tell
which bucket an entry belongs to.
Local depth of a bucket: # of bits used to determine if an
entry belongs to this bucket.
When does bucket split cause directory doubling?
Before insert, local depth of bucket = global depth. Insert
causes local depth to become > global depth; directory is
doubled by copying it over and `fixing pointer to split
image page. (Use of least significant bits enables efficient
doubling via copying of directory!)
11.Hash
Performance, Deletions
If directory fits in memory, equality search
answered with one disk access; else two.
100MB file, 100 bytes/rec, contains 1,000,000 records (as
data entries). If pages are 4K then the file requires 25,000
directory elements; chances are high that directory will
fit in memory.
Directory grows in spurts, and, if the distribution of hash
values is skewed, directory can grow large.
Multiple entries with same hash value cause problems!
Delete: If removal of data entry makes bucket
empty, can be merged with `split image. If each
directory element points to same bucket as its split
image, can halve directory.
11.Hash
Overview of LH File
In the middle of a round.
Buckets split in this round:
Bucket to be split If h Level ( search key value )
Next is in this range, must use
h Level+1 ( search key value )
Buckets that existed at the
to decide if entry is in
beginning of this round: `split image' bucket.
this is the range of
hLevel
`split image' buckets:
created (through splitting
of other buckets) in this round
11.Hash
When to split?
Insert: Find bucket by applying hLevel / hLevel+1:
If bucket to insert into is full:
Add overflow page and insert data entry.
(Maybe) Split Next bucket and increment Next.
Can choose any criterion to `trigger split.
Since buckets are split round-robin, long overflow
chains dont develop!
Doubling of directory in Extendible Hashing is
similar; switching of hash functions is implicit in
how the # of bits examined is increased.
11.Hash
32*
Level=0, N=4 000 00
Next=1
h h PRIMARY 9* 25* 5*
001 01
1 0 Next=0 PAGES
LH Described as a Variant of EH
The two schemes are actually quite similar:
Begin with an EH index where directory has N elements.
Use overflow pages, split buckets round-robin.
First split is at bucket 0. (Imagine directory being doubled
at this point.) But elements <1,N+1>, <2,N+2>, ... are the
same. So, need only create directory element N, which
differs from 0, now.
When bucket 1 splits, create directory element N+1, etc.
So, directory can double gradually. Also, primary
bucket pages are created in order. If they are allocated
in sequence too (so that finding ith is easy), we
actually dont need a directory! Voila, LH.