Professional Documents
Culture Documents
■ Basic Concepts
■ Ordered Indices
■ B+-Tree Index Files
■ B-Tree Index Files
■ Static Hashing
Database System Concepts - 6th Edition 11.2 ©Silberschatz, Korth and Sudarshan
■ Indexing mechanisms used to speed up access to desired data. ■ Access types supported efficiently. E.g.,
● E.g., author catalog in library ● records with a specified value in the attribute
■ Search Key - attribute to set of attributes used to look up records in a ● or records with an attribute value falling in a specified range of
file. values.
■ An index file consists of records (called index entries) of the form ■ Access time
■ Insertion time
search-key pointer
■ Deletion time
■ Index files are typically much smaller than the original file
■ Space overhead
■ Two basic kinds of indices:
● Ordered indices: search keys are stored in sorted order
● Hash indices: search keys are distributed uniformly across
“buckets” using a “hash function”.
Database System Concepts - 6th Edition 11.3 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.4 ©Silberschatz, Korth and Sudarshan
Ordered Indices Dense Index Files
■ Dense index — Index record appears for every search-key
■ In an ordered index, index entries are stored sorted on the search key value in the file.
value. E.g., author catalog in library. ■ E.g. index on ID attribute of instructor relation
■ Primary index: in a sequentially ordered file, the index whose search
key specifies the sequential order of the file.
● Also called clustering index
● The search key of a primary index is usually but not necessarily the
primary key.
■ Secondary index: an index whose search key specifies an order
different from the sequential order of the file. Also called
non-clustering index.
■ Index-sequential file: ordered sequential file with a primary index.
Database System Concepts - 6th Edition 11.5 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.6 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 6th Edition 11.7 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.8 ©Silberschatz, Korth and Sudarshan
Sparse Index Files (Cont.) Secondary Indices Example
■ Compared to dense indices:
● 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.
Database System Concepts - 6th Edition 11.9 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.10 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 6th Edition 11.11 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.12 ©Silberschatz, Korth and Sudarshan
Multilevel Index (Cont.) Index Update: Deletion
● Dense indices – if the search-key value does not appear in ● Example 1: In the instructor relation stored sequentially by
the index, insert it. ID, we may want to find all instructors in a particular
department
● Sparse indices – if index stores an entry for each block of
the file, no change needs to be made to the index unless a ● Example 2: as above, but where we want to find all
new block is created. instructors with a specified salary or with salary in a
specified range of values
4 If a new block is created, the first search-key value
appearing in the new block is inserted into the index. ■ We can have a secondary index with an index record for
each search-key value
■ Multilevel insertion and deletion: algorithms are simple
extensions of the single-level algorithms
Database System Concepts - 6th Edition 11.15 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.16 ©Silberschatz, Korth and Sudarshan
B+-Tree Index Files Example of B+-Tree
Database System Concepts - 6th Edition 11.17 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.18 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 6th Edition 11.19 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.20 ©Silberschatz, Korth and Sudarshan
Leaf Nodes in B+-Trees Non-Leaf Nodes in B+-Trees
Properties of a leaf node: ■ Non leaf nodes form a multi-level sparse index on the leaf
nodes. For a non-leaf node with m pointers:
■ For i = 1, 2, . . ., n–1, pointer Pi points to a file record with
search-key value Ki, ● All the search-keys in the subtree to which P1 points are
less than K1
■ If Li, Lj are leaf nodes and i < j, Li’s search-key values are less
than or equal to Lj’s search-key values ● For 2 £ i £ n – 1, all the search-keys in the subtree to which
Pi points have values greater than or equal to Ki–1 and less
■ Pn points to next leaf node in search-key order
than Ki
● All the search-keys in the subtree to which Pn points have
values greater than or equal to Kn–1
Database System Concepts - 6th Edition 11.21 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.22 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 6th Edition 11.23 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.24 ©Silberschatz, Korth and Sudarshan
Queries on B+-Trees Queries on B+-Trees
■ Find record with search-key value V.
1. C=root ■ If there are K search-key values in the file, the height of the tree is no
2. While C is not a leaf node { more than élogén/2ù(K)ù.
1. Let i be least value s.t. V £ Ki. ■ A node is generally the same size as a disk block, typically 4
2. If no such exists, set C = last non-null pointer in C kilobytes
3. Else { if (V= Ki ) Set C = Pi +1 else set C = Pi} ● and n is typically around 100 (40 bytes per index entry).
} ■ With 1 million search key values and n = 100
3. Let i be least value s.t. Ki = V ● at most log50(1,000,000) = 4 nodes are accessed in a lookup.
4. If there is such a value i, follow pointer Pi to the desired record. ■ Contrast this with a balanced binary tree with 1 million search key
5. Else no record with search-key value k exists. values — around 20 nodes are accessed in a lookup
● above difference is significant since every node access may need
a disk I/O, costing around 20 milliseconds
Database System Concepts - 6th Edition 11.25 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.28 ©Silberschatz, Korth and Sudarshan
2. If the search-key value is already present in the leaf node ● take the n (search-key value, pointer) pairs (including the one
being inserted) in sorted order. Place the first én/2ù in the original
1. Add record to the file node, and the rest in a new node.
2. If necessary add a pointer to the bucket. ● let the new node be p, and let k be the least key value in p. Insert
3. If the search-key value is not present, then (k,p) in the parent of the node being split.
● If the parent is full, split it and propagate the split further up.
1. add the record to the main file (and create a bucket if
necessary) ■ Splitting of nodes proceeds upwards till a node that is not full is found.
In the worst case the root node may be split increasing the height
2. If there is room in the leaf node, insert (key-value, pointer) ●
of the tree by 1.
pair in the leaf node
3. Otherwise, split the node (along with the new (key-value,
pointer) entry) as discussed in the next slide.
Result of splitting node containing Brandt, Califieri and Crick on inserting Adams
Next step: insert entry with (Califieri,pointer-to-new-node) into parent
Database System Concepts - 6th Edition 11.29 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.30 ©Silberschatz, Korth and Sudarshan
B+-Tree Insertion B+-Tree Insertion
+“Adams”
+“Lamport”
B+-Tree before and after insertion of “Adams” B+-Tree before and after insertion of “Lamport”
Database System Concepts - 6th Edition 11.31 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.32 ©Silberschatz, Korth and Sudarshan
Califieri
■ Deleting “Srinivasan”
Exercise: causes
Deletion merging ofand
of “Singh” under-full
“Wu” leaves
from the above tree
Database System Concepts - 6th Edition 11.33 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.34 ©Silberschatz, Korth and Sudarshan
Examples of B+-Tree Deletion (Cont.) Example of B+-tree Deletion (Cont.)
–“Gold”
■ Find the record to be deleted, and remove it from the main file and ■ Otherwise, if the node has too few entries due to the removal, but the
from the bucket (if present) entries in the node and a sibling do not fit into a single node, then
redistribute pointers:
■ Remove (search-key value, pointer) from the leaf node if there is no
bucket or if the bucket has become empty ● Redistribute the pointers between the node and a sibling such that
both have more than the minimum number of entries.
■ 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 merge siblings: ● Update the corresponding search-key value in the parent of the
node.
● Insert all the search-key values in the two nodes into a single node
(the one on the left), and delete the other node. ■ The node deletions may cascade upwards till a node which has én/2ù
or more pointers is found.
● Delete the pair (Ki–1, Pi), where Pi is the pointer to the deleted
node, from its parent, recursively using the above procedure. ■ If the root node has only one pointer after deletion, it is deleted and
the sole child becomes the root.
Database System Concepts - 6th Edition 11.37 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.38 ©Silberschatz, Korth and Sudarshan
B+-Tree File Organization B+-tree exercise
■ Index file degradation problem is solved by using B+-Tree indices. ■ Construct a B+-tree for the following table given search key is
“Instructor name”
■ Data file degradation problem is solved by using B+-Tree File
Organization.
■ The leaf nodes in a B+-tree file organization store records, instead of
pointers.
■ Leaf nodes are still required to be half full
● Since records are larger than pointers, the maximum number of
records that can be stored in a leaf node is less than the number of
pointers in a nonleaf node.
■ Insertion and deletion are handled in the same way as insertion and
deletion of entries in a B+-tree index.
Database System Concepts - 6th Edition 11.40 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.52 ©Silberschatz, Korth and Sudarshan
Questions?
1. What is hashing?
2. Basic idea of hashing?
a) What is bucket/block?
b) What is hash function?
Hashing c) What is bucket overflow?
d) What is a good functions?
3. What is static hashing?
4. What is dynamic hashing?
Database System Concepts - 6th Edition 11.54 ©Silberschatz, Korth and Sudarshan
Static Hashing Example of Hash File Organization
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
b(MUSIC) = 13 + 21 + 19 + 9 + 3 = 65
Database System Concepts - 6th Edition 11.55 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.56 ©Silberschatz, Korth and Sudarshan
■ 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
the number of buckets could be returned. .
■ Bucket overflow can occur because of ■ Overflow chaining – the overflow buckets of a given bucket are
● Insufficient buckets chained together in a linked list.
● Skew in distribution of records. This can occur due to two ■ Above scheme is called closed hashing.
reasons: ● An alternative, called open hashing, which does not use overflow
4 multiple records have same search-key value buckets, is not suitable for database applications.
Database System Concepts - 6th Edition 11.59 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.60 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 6th Edition 11.61 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.62 ©Silberschatz, Korth and Sudarshan
Deficiencies of Static Hashing Dynamic Hashing
■ In static hashing, function h maps search-key values to a fixed set of B ■ Good for database that grows and shrinks in size
of bucket addresses. Databases grow or shrink with time. ■ Allows the hash function to be modified dynamically
● If initial number of buckets is too small, and file grows, performance ■ Extendable hashing – one form of dynamic hashing
will degrade due to too much overflows. ● Hash function generates values over a large range — typically b-bit
● If space is allocated for anticipated growth, a significant amount of integers, with b = 32.
space will be wasted initially (and buckets will be underfull). ● At any time use only a prefix of the hash function to index into a
● If database shrinks, again space will be wasted. table of bucket addresses.
● Let the length of the prefix be i bits, 0 £ i £ 32.
■ One solution: periodic re-organization of the file with a new hash
function 4 Bucket address table size = 2i. Initially i = 0
● Expensive, disrupts normal operations 4 Value of i grows and shrinks as the size of the database grows
■ Better solution: allow the number of buckets to be modified dynamically. and shrinks.
● Multiple entries in the bucket address table may point to a bucket
(why?)
● Thus, actual number of buckets is < 2i
4 The number of buckets also changes dynamically due to
coalescing and splitting of buckets.
Database System Concepts - 6th Edition 11.63 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.64 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 6th Edition 11.65 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.66 ©Silberschatz, Korth and Sudarshan
Insertion in Extendable Hash Structure (Cont) Deletion in Extendable Hash Structure
To split a bucket j when inserting record with search-key value Kj:
■ To delete a key value,
■ If i > ij (more than one pointer to bucket j)
● locate it in its bucket and remove it.
● allocate a new bucket z, and set ij = iz = (ij + 1)
● The bucket itself can be removed if it becomes empty (with
● Update the second half of the bucket address table entries originally appropriate updates to the bucket address table).
pointing to j, to point to z
● Coalescing of buckets can be done (can coalesce only with a
● remove each record in bucket j and reinsert (in j or z)
“buddy” bucket having same value of ij and same ij –1 prefix, if it is
● recompute new bucket for Kj and insert record in the bucket (further present)
splitting is required if the bucket is still full)
● Decreasing bucket address table size is also possible
■ If i = ij (only one pointer to bucket j)
4 Note: decreasing bucket address table size is an expensive
● If i reaches some limit b, or too many splits have happened in this
insertion, create an overflow bucket operation and should be done only if number of buckets becomes
much smaller than the size of the table
● Else
4 increment i and double the size of the bucket address table.
4 replace each entry in the table by two entries that point to the
same bucket.
4 recompute new bucket address table entry for Kj
Now i > ij so use the first case above.
Database System Concepts - 6th Edition 11.67 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.68 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 6th Edition 11.69 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.70 ©Silberschatz, Korth and Sudarshan
Example (Cont.)
Database System Concepts - 6th Edition 11.71 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.72 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 6th Edition 11.73 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.74 ©Silberschatz, Korth and Sudarshan
Example (Cont.) Example (Cont.)
■ Hash structure after insertion of Katz record
Database System Concepts - 6th Edition 11.75 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 11.76 ©Silberschatz, Korth and Sudarshan
End of Chapter