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
Indexing and Hashing 1
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 Indexing and Hashing 2
Basic Concepts
Indexing is used to speed up access to desired data.
E.g. author catalog in library
A search key is an attribute or set of attributes used to look up records in a file. Unrelated to keys in the db schema. An index file consists of records called index entries. An index entry for key k may consist of
An actual data record (with search key value k) A pair (k, rid) where rid is a pointer to the actual data record A pair (k, bid) where bid is a pointer to a bucket of record pointers
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 Indexing and Hashing 3
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 Indexing and Hashing 5
CIS552
Less space and less maintenance overhead for insertions and deletions. Generally slower than dense index for locating records. Good tradeoff: sparse index with an index entry for every block in file, corresponding to least search-key value in the block.
CIS552 Indexing and Hashing 7
CIS552
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 Indexing and Hashing 9
inner index
Data Block 0
M
M
M
Data Block 1
Index Block 1
M M M
M
M
CIS552
M
10
CIS552
11
Multilevel insertion (as well as deletion) algorithms are simple extensions of the single-level algorithms
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.
We can have a non-clustering index with an index record for each search-key value. The index record points to a bucket that contains pointers to all the actual records with that particular search-key value.
CIS552
13
Brighton Downtown Downtown Miami Perryridge Perryridge Perryridge Redwood Round Hill
CIS552
14
CIS552
15
CIS552
17
Ki are the search-key values Pi are pointers to children (for non-leaf nodes) or pointers to records or buckets of records (for leaf nodes). The search-keys in a node are ordered K1 < K2 < K3 < < Kn-1
CIS552
18
Downtown
Brighton Downtown Downtown
account file
M
CIS552
19
CIS552
20
Examples of a B+-tree
Perryridge
Miami
Redwood
Brighton
Downtown
Miami
Perryridge
Redwood
Round Hill
CIS552
21
Example of a B+-tree
Perryridge
Brighton Downtown Miami
Leaf nodes must have between 2 and 4 values ( (n1)/2 and n 1, with n=5). Non-leaf nodes other than root must have between 3 and 5 children ( n/2 and n with n = 5). Root must have at least 2 children
CIS552
22
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
CIS552
27
Redwood
Brighton
Downtown
Miami
Perryridge
Perryridge
Redwood
Round Hill
Downtown Miami
Brighton Clearview Downtown
Miami
Redwood
Perryridge
CIS552
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
The node deletions may cascade upwards till a node which has n/2 or more pointers is found. If the root node has only one pointer after deletion, it is deleted and the sole child becomes the root.
CIS552
30
Miami
Brighton Clearview
Redwood
Miami
Perryridge
Result after deleting Downtown from account The removal of the leaf node containing Downtown did not result in its parent having too little pointers. So the cascaded deletions stopped with the deleted leaf nodes parent.
CIS552 Indexing and Hashing 31
Downtown
Brighton Clearview
Redwood
Downtown
Miami
Deletion of Perryridge instead of Downtown The deleted Perryridge nodes parent became too small, but its sibling did not have space to accept one more pointer, so redistribution is performed. Observe that the root nodes search-key value changes as a result.
CIS552 Indexing and Hashing 32
CIS552
33
CIS552
34
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 Indexing and Hashing 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. An ideal hash function is uniform, i.e. each bucket is assigned the same number of search-key values from the set of all possible values. 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. Typical hash functions perform computation on the internal binary representation of the search-key. For example, for a string search-key, the binary representations of all the characters in the string could be added and the sum modulo number of buckets could be returned.
CIS552
37
bucket 1
bucket 6
bucket 2
bucket 7
bucket 3
Brighton A-217 Round Hill A-305 750 350
500 600
bucket 4 Redwood
bucket 9
A-222
700
CIS552
38
Hash file organization of account file, using branch-name as key. (See figure in previous slide.) There are 10 buckets. The binary representation of the ith character is assumed to be the integer i. The hash function returns the sum of the binary representations of the characters modulo 10.
CIS552
39
Although the probability of bucket overflow can be reduced, it can not be eliminated; it is handled by using overflow buckets. Overflow chaining the overflow buckets of a given bucket are chained together in a linked list Above scheme is called closed hashing. An alternative, called open hashing, is not suitable for database applications.
CIS552 Indexing and Hashing 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
A-215 A-305
bucket 2
A-101 A-110
bucket 3
A-217 A-102
bucket 4
A-201
A-218
bucket 5
Brighton Downtown Downtown Miaimi Perryridge Perryridge Perryridge Redwood Round Hill
bucket 6
A-222
CIS552 Indexing and Hashing 42
These problems can be avoided by using techniques that allow the number of buckets to be modified dynamically.
CIS552
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
To insert a record with search-key value Ki, follow same procedure as look-up and locate the bucket, say j. If there is room in the bucket j insert record in the bucket. Else the bucket must be split and insertion re-attempted. (See next slide.)
CIS552 Indexing and Hashing 46
When inserting a value, if the bucket is full after several splits (that is, i reaches some limit b) create an overflow bucket instead of splitting bucket entry value further. To delete a key value, locate it in its bucket and remove it. The bucket itself can be removed if it becomes empty (with appropriate updates to the bucket address table). Merging of buckets and decreasing bucket address table size is also possible.
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
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 Indexing and Hashing 50
Example (3)
1
hash suffix ..00 ..01 ..10 ..11
Brighton
A-217
750
Downtown Downtown
A-101 A-110
2 500 600 2
Miami
A-215
700
2
Brighton A-217 750
Example (4)
3
..000
..001 ..010 ..011
Redwood Redwood
A-222 A-722
700 750
3
Downtown Downtown A-101 A-110 500 600 Downtown A-611
3
820
..100
..101 ..110 ..111
Round Hill A-432
3
500
3
bucket address table
Miami Perryridge
A-102 A-201
400 900
CIS552
54
Multiple-Key Access
Use multiple indices for certain types of queries. Example:
select account-number from account where branch-name = Perryridge and Balance = 1000
CIS552
55
Using separate indices is less efficient we may fetch many records (or pointers) that satisfy only one of the conditions. Can also efficiently handle
where branch-name = Perryridge and balance < 1000
where branch-name < Perryridge and balance = 1000 May fetch many records that satisfy the first but not the second condition.
Indexing and Hashing 56
CIS552