CIS552 Indexing and Hashing 1
CIS552 Indexing and Hashing 1
CIS552 Indexing and Hashing 1
Cost estimation
Basic Concepts
Ordered Indices
B+ - Tree Index Files
B - Tree Index Files
Static Hashing
Dynamic Hashing
Comparison of Ordered Indexing and Hashing
Index Definition in SQL
Multiple-Key Access
CIS552
Estimating Costs
For simplicity we estimate the cost of an
operation by counting the number of blocks
that are read or written to disk.
We ignore the possibility of blocked access
which could significantly lower the cost of
I/O.
We assume that each relation is stored in a
separate file with B blocks and R records
per block.
CIS552
Basic Concepts
Indexing is used to speed up access to desired data.
E.g. author catalog in library
Index files are typically much smaller than the original file
if the actual data records are in a separate file.
If the index contains the data records, there is a single file
with a special organization.
CIS552
CIS552
Types of Indices
The records in a file may be unordered or ordered
sequentially by some search key.
A file whose records are unordered is called a heap file.
If an index contains the actual data records or the records
are sorted by search key in a separate file, the index is
called clustering (otherwise non-clustering).
In an ordered index, index entries are sorted on the search
key value. Other index structures include trees and hash
tables.
A primary index is an index on a set of fields that includes
the primary key. Any other index is a secondary index.
CIS552
CIS552
Brighton
A-217
750
Downtown
A-101
500
Downtown
A-110
600
Miami
A-215
700
Perryridge
A-102
400
Perryridge
A-201
900
Perryridge
A-218
700
Redwood
A-222
700
Round Hill
A-305
350
CIS552
Brighton
A-217
750
Downtown
A-101
500
Downtown
A-110
600
Miami
A-215
700
Perryridge
A-102
400
Perryridge
A-201
900
Perryridge
A-218
700
Redwood
A-222
700
Round Hill
A-305
350
Multilevel Index
If an index does not fit in memory, access becomes
expensive.
To reduce number of disk accesses to index records, treat
the index kept on disk as a sequential file and construct a
sparse index on it.
outer index a sparse index on main index
inner index the main index file
If even outer index is too large to fit in main memory, yet
another level of index can be created, and so on.
Indices at all levels must be updated on insertion or
deletion from the file.
CIS552
outer index
Index
Block 0
Data
Block 0
M
M M
Index
Block 1
Data
Block 1
M
M
CIS552
10
CIS552
11
CIS552
12
Non-clustering Indices
Frequently, one wants to find all the records whose values in
a certain field satisfy some condition, and the file is not
ordered on the field.
Example 1: In the account database stored sequentially by account
number, we may want to find all accounts in a particular branch.
Example 2: As above, but where we want to find all accounts with a
specified balance or range of balances.
13
350
400
500
600
700
750
900
CIS552
Brighton
A-217
750
Downtown
A-101
500
Downtown
A-110
600
Miami
A-215
700
Perryridge
A-102
400
Perryridge
A-201
900
Perryridge
A-218
700
Redwood
A-222
700
Round Hill
A-305
350
14
CIS552
15
16
CIS552
17
K1
P2
Pn-1
Kn-1
Pn
CIS552
18
For i = 1,2,, n-1, pointer P i either points to a file record with searchkey value Ki, or to a bucket of pointers to file records, each record
having search-key value Ki. Only need bucket structure if search-key
does not form a superkey and the index is non-clustering.
If Li, Lj are leaf nodes and i < j, L is search-key values are less than L js
search-key values
Pn points to next leaf node in search-key order
Brighton
Downtown
leaf node
Brighton
Downtown
Downtown
A-212
A-101
A-110
750
500
600
account file
CIS552
19
CIS552
K1
P2
Pn-1
Kn-1
Pn
20
Examples of a B+-tree
Perryridge
Miami
Brighton
Downtown
Redwood
Miami
Perryridge
Redwood
Round Hill
CIS552
21
Example of a B+-tree
Perryridge
Brighton Downtown
Miami
CIS552
22
23
Queries on B+-Trees
Find all records with a search-key value of k.
Start with the root node
Examine the node for the smallest search-key value > k.
If such a value exists, assume it is Ki. Then follow Pi to the child
node
Otherwise k Km-1, where there are m pointers in the node, Then
follow Pm to the child node.
If the node reached by following the pointer above is not a leaf
node, repeat the above procedure on the node, and follow the
corresponding pointer.
Eventually reach a leaf node. If key Ki = k, follow pointer Pi to the
desired record or bucket. Else no record with search-key value k
exists.
CIS552
24
CIS552
25
CIS552
26
27
Brighton
Downtown
Redwood
Miami
Redwood
Perryridge
Round Hill
Perryridge
Downtown Miami
Brighton
Clearview
CIS552
Downtown
Redwood
Miami
Perryridge
Updates on B+-Trees:Deletion
Find the record to be deleted, and remove it from the main
file and from the bucket (if present)
Remove (search-key value, pointer) from the leaf node if
there is no bucket or if the bucket has become empty
If the node has too few entries due to the removal, and the
entries in the node and a sibling fit into a single node, then
Insert all the search-key values in the two nodes into a single node
(the one on the left), and delete the other node.
Delete the pair (Ki1, Pi), where Pi is the pointer to the deleted
node, from its parent, recursively using the above procedure.
CIS552
29
CIS552
30
Miami
Brighton
Clearview
Redwood
Miami
Perryridge
31
Redwood
Downtown
Brighton
Clearview
Downtown
Miami
32
CIS552
33
K1
P2
Pn-1
Kn-1
Pn
CIS552
B1
K1
P2
B2
K2
Pm-1
Bm-1
Km-1
Pm
34
CIS552
35
Static Hashing
A bucket is a unit of storage containing one or more
records (a bucket is typically a disk block). In a hash file
organization we obtain the bucket of a record directly from
its search-key value using a hash function.
Hash function h is a function from the set of all search-key
values K to the set of all bucket addresses B.
Hash function is used to locate records for access, insertion,
and deletion.
Records with different search-key values may be mapped to
the same bucket; thus entire bucket has to be searched
sequentially to locate a record.
CIS552
36
Hash Functions
Worst hash function maps all search-key values to the same bucket;
this makes access time proportional to the number of search-key
values in the file.
Ideal hash function is random, so each bucket will have the same
number of records assigned to it irrespective of the actual distribution
of search-key values in the file.
CIS552
37
bucket 5
Perryridge
Perryridge
Perryridge
Miami
CIS552
A-102
A-201
A-218
A-215
400
900
700
700
500
600
bucket 1
bucket 6
bucket 2
bucket 7
bucket 3
bucket 8
Downtown A-101
Downtown A-110
Brighton
A-217
Round Hill A-305
750
350
bucket 4
Redwood
700
A-222
bucket 9
38
CIS552
39
*
*
Insufficient buckets
Skew in distribution of records. This can occur due to two reasons:
multiple records have same search-key value
chosen hash function produces non-uniform distribution of key
values
40
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.
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, the term hash index is used to refer to both
secondary index structures and hash organized files.
CIS552
41
bucket 1
A-215
A-305
bucket 2
A-101
A-110
bucket 3
A-217
A-102
A-201
bucket 4
A-218
Brighton
Downtown
Downtown
Miaimi
Perryridge
Perryridge
Perryridge
Redwood
Round Hill
A-217
A-101
A-110
A-215
A-102
A-201
A-218
A-222
A-305
750
500
600
700
400
900
700
700
350
bucket 5
bucket 6
A-222
CIS552
42
43
Dynamic Hashing
Good for database that grows and shrinks in size
Allows the hash function to be modified dynamically
Extendable hashing one form of dynamic hashing
Hash function generates values over a large range typically
b-bit integers, with b = 32.
At any time use only the last i bits of the hash function to index
into a table of bucket addresses, where: 0 i 32
Initially i = 0
Value of i grows and shrinks as the size of the database grows and
shrinks.
Actual number of buckets is < 2i, and this also changes
dynamically due to merging and splitting of buckets.
CIS552
44
bucket 1
i2
..01
bucket 2
..10
..11
M
i3
bucket 3
45
46
47
CIS552
48
h(branch-name)
0110 1101 1111 1011 0010 1100 0011 0000
0010 0011 1010 0000 1100 0110 1001 0001
1110 0111 1110 1101 1011 1111 0011 0011
1011 0001 0010 0100 1001 0011 0110 1111
0011 0101 1010 0110 1100 1001 1110 1110
1111 1000 0011 1111 1001 1100 0000 1101
0
bucket 1
49
Example (2)
Insert account records from branches:
1) Brighton
2) Downtown
3) Downtown
4) Miami
5) Perryridge
6) Redwood
7) Roundhill
8) Redwood
9) Downtown
CIS552
50
Example (3)
hash suffix
..00
Brighton
A-217
750
A-101
A-110
2
500
600
..01
..10
Downtown
Downtown
..11
bucket address table
Miami
A-215
700
51
2
Brighton
A-217
750
Example (4)
2
3
..000
..001
..010
..011
Redwood
Redwood
A-222
A-722
700
750
3
Downtown
Downtown
..100
..101
..110
A-101
A-110
500
600
3
Downtown
A-611
820
3
Round Hill
A-432
500
..111
3
bucket address table
Miami
Perryridge
A-102
A-201
400
900
52
53
CIS552
54
Multiple-Key Access
CIS552
55
CIS552
56