Chapter 1: Introduction
Indexing
Database System Concepts
©Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
1
Indexing
Review of Basic Concepts and Ordered Indices
B+-Tree Index Files
Static Hashing
Dynamic Hashing
Multiple-Key Access
Creation of Indices
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
2
A Few Questions
Do you know that time taken to access some data in disk >> time taken to access some data in main memory?
Do you know that in accessing some data in disk, the whole disk block containing the required data has to be brought from disk into main memory?
Blocks are units of both storage allocation and data transfer.
Disk block read requires about 5 to 10 milliseconds, versus about 100 nanoseconds for memory access
If you want to access some data in a database, is it a good idea to read every record in the database to search for the desired data?
What if the database is so small that it can be stored in main memory?
What if the database is so large that it must be stored in disk?
If the records are sorted in the database, do you know any good searching algorithms to reduce the search time? (binary search)
If the database occupies 1,000,000 blocks, how many blocks have to be read by using binary search for the desired data? (log2(1,000,000)=20)
How much time it takes, if a block read takes 10 ms? (0.2 sec, is it a long time to you?)
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Review of Basic Concepts
It is inefficient to read every record in a (large) database to search for desired data.
Indexing mechanisms used to speed up access to desired data.
E.g., author catalog in library
Search Key – attribute or set of attributes used to look up records in a file.
A sequential file stores records in sequential order, based on the value of the search key of each record.
An index file consists of records (called index entries) of the form
Index files are typically much smaller than the original file
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”.
search-key
pointer
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
4
Index Evaluation Metrics
NO one technique is the best.
Access types
records with a specified value in the attribute
records with an attribute value falling in a specified range of values.
Access time
time to find a data item
Insertion time
time to find the correct insertion point
time to update the index structure
Deletion time
time to find the item to be deleted
time to update the index structure
Space overhead occupied by the index structure
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
5
Ordered Indices
In an ordered index, index entries are sorted on the search key value.
Primary index: an 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.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
6
Dense Index Files
Dense index — index record appears for every search-key value in the file.
E.g. dense primary index on ID attribute of instructor relation
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Dense Index Files (Cont.)
An index on dept_name, with instructor file sorted on dept_name
Is it a primary index?
Is it a dense primary index?
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
8
Sparse Index Files
Sparse Index: contains index records for only some search-key values.
Applicable only if the index is a primary index (why?)
To locate a record with search-key value K,
Find index record with largest search-key value K
Search file sequentially starting at the record to which the index record points
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
9
Sparse Index Files (Cont.)
Compared to dense indices:
Less space and less maintenance overhead for insertions and deletions.
Generally slower than dense index for locating records.
Good tradeoff: one index entry per block (sparse index with an index entry for every block in file, corresponding to least search-key value in the block)
dominant cost: time to bring a block from disk into main memory
minimize block accesses while keeping index size small
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
10
Multilevel Index
If primary index does not fit in memory, access becomes expensive, due to disk block reads.
Solution: treat primary index kept on disk as a sequential file and construct a sparse index on it.
outer index – a sparse index of primary index
inner index – the primary 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.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
11
Multilevel Index (Cont.)
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
12
Secondary Indices
Reminder: Secondary index is an index whose search key specifies an order different from the sequential order of the file.
Frequently, one wants to find all the records whose values in a certain field (which is not the search-key of the primary index) satisfy some condition.
Example: In the instructor relation stored sequentially by ID, we want to find all instructors with a specified salary or with salary in a specified range of values
We can have a secondary index with an index record for every search-key value, and a pointer to every record in the file, i.e., must be dense.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
13
Secondary Indices Example
Index record points to a bucket that contains pointers to all the actual records with that particular search-key value.
Secondary index on salary field of instructor
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
14
Primary and Secondary Indices
Sequential scan using primary index is efficient, but a sequential scan using a secondary index is expensive on magnetic disk
Each record access may fetch a new block from disk
Indices offer substantial benefits when searching for records.
BUT: Updating indices imposes overhead on database modification
when a record is inserted or deleted, every index on the relation must be updated
When a record is updated, any index on an updated attribute must be updated
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
15
Index Update: Deletion
If deleted record was the only record in the file with its particular search-key value, the search-key is deleted from the index also.
Single-level index entry deletion:
Dense indices
Deletion of search-key is similar to file record deletion.
Sparse indices
If an entry for the search key exists in the index, it is deleted by replacing the entry in the index with the next search-key value in the file (in search-key order).
If the next search-key value already has an index entry, the entry is deleted instead of being replaced.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
16
Index Update: Insertion
Single-level index insertion:
Perform a lookup using the search-key value of the record to be inserted.
Dense indices – if the search-key value does not appear in the index, insert it
Indices are maintained as sequential files
Need to create space for new entry, overflow blocks may be required
Sparse indices – if index stores an entry for each block of the file, no change needs to be made to the index unless a new block is created.
If a new block is created, the first search-key value appearing in the new block is inserted into the index.
Multilevel insertion and deletion: algorithms are simple extensions of the single-level algorithms
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
17
Indexing
Review of Basic Concepts and Ordered Indices
B+-Tree Index Files
Static Hashing
Dynamic Hashing
Multiple-Key Access
Creation of Indices
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
18
B+-Tree Index Files
Disadvantage of index-sequential files
Performance degrades as file grows, since many overflow blocks get created.
Periodic reorganization of entire file is required to restore sequential order.
Advantage of B+-tree index files:
Automatically reorganizes itself with small, local changes, in the face of insertions and deletions.
Reorganization of entire file is not required to maintain performance.
Minor disadvantage of B+-trees:
Extra insertion and deletion overhead, space overhead.
Advantages of B+-trees outweigh disadvantages
B+-trees are used extensively.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
19
Example of B+-Tree
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
B+-Tree Index Files (Cont.)
All paths from root to leaf are of the same length
Each internal node (not a root or a leaf) has between n/2 and n children, where n is fixed for a particular tree
A leaf node has between (n–1)/2 and n–1 values
Special cases:
If the root is not a leaf, it has at least 2 children.
If the root is a leaf (that is, there are no other nodes in the tree), it can have between 0 and (n–1) values.
A B+-tree is a balanced tree satisfying the following properties:
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
21
B+-Tree Node Structure
Typical node
Ki are the search-key values
Pi are pointers to children (for non-leaf nodes) or pointers to records (for leaf nodes).
The search-keys in a node are ordered
K1 < K2 < K3 < . . . < Kn–1
(assume no duplicate keys)
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
22
Leaf Nodes in B+-Trees
For i = 1, 2, . . ., n–1, pointer Pi points to a file record with search-key value Ki,
If Li, Lj are leaf nodes and i < j, Li’s search-key values are less than Lj’s search-key values
Pn points to next leaf node in search-key order
Properties of a leaf node:
What is n in this example?
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
23
Non-Leaf Nodes in B+-Trees
Non leaf nodes form a multi-level sparse index on the leaf nodes. For a non-leaf node with m pointers (m n):
All the search-keys in the subtree to which P1 points are less than K1
For 2 i m – 1, all the search-keys in the subtree to which Pi points have values greater than or equal to Ki–1 and less than Ki
All the search-keys in the subtree to which Pm points have values greater than or equal to Km–1
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Example of B+-tree
Leaf nodes must have between 3 and 5 values
((n–1)/2 and n –1, with n = 6).
Internal nodes must have between 3 and 6 children (n/2 and n with n = 6).
Root must have at least 2 children.
B+-tree for instructor file (n = 6)
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
25
Observations about B+-trees
Typically, a node is made to be the same size as a disk block.
Since the inter-node connections are done by pointers, “logically” close blocks need not be “physically” close.
The non-leaf levels of the B+-tree form a hierarchy of sparse indices.
The B+-tree contains a relatively small number of levels. If there are K search-key values in the file, the tree height is no more than logn/2(K), thus searches can be conducted efficiently.
Insertions and deletions to the main file can be handled efficiently, as the index can be restructured in logarithmic time.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
26
Queries on B+-Trees
Find record with search-key value V.
C=root
While C is not a leaf node {
Let i be least value s.t. V Ki.
If no such exists, set C = last non-null pointer in C
Else { if (V= Ki ) Set C = Pi +1 else set C = Pi}
}
Let i be the value s.t. Ki = V /* C is now a leaf node */
If there is such a value i, follow pointer Pi to the desired record.
Else no record with search-key value V exists.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
27
Queries on B+-Trees (Cont.)
If there are K search-key values in the file, the height of the tree is no more than logn/2(K).
Example: A node is generally the same size as a disk block, typically 4 kilobytes and n is typically around 100 (40 bytes per index entry).
With 1 million search key values and n = 100, at most log50(1,000,000) = 4 nodes are accessed in a lookup and every node access may need a disk I/O
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
28
Updates on B+-Trees: Insertion
Find the leaf node in which the search-key value would appear
If there is room in the leaf node, insert (search-key value, record pointer) pair in the leaf node in sorted order
Otherwise, split the node (along with the new entry) as discussed in the next slide.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
29
Updates on B+-Trees: Insertion (Cont.)
Splitting a 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 node, and the rest in a new node.
Let the new node be p, and let k be the least key value in p. Insert (k,p) in the parent of the node being split.
If the parent is full, split it and propagate the split further up.
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 of the tree by 1.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
30
B+-Tree Insertion
B+-Tree before and after insertion of “Adams”
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
31
Splitting a non-leaf node: when inserting (k,p) into an already full internal node N
Copy N to an in-memory area M with space for n+1 pointers and n keys
Insert (k,p) into M
Copy P1,K1, …, K (n+1)/2-1,P (n+1)/2 from M back into node N
Copy P(n+1)/2+1,K (n+1)/2+1,…,Kn,Pn+1 from M into newly allocated node N’
Insert (K n+1/2,N’ ) into parent of N
Read pseudocode in book!
Insertion in B+-Trees (Cont.)
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
32
B+-Tree Insertion
B+-Tree before and after insertion of “Lamport”
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Updates on B+-Trees: Deletion
Perform a lookup on the search-key value of the deleted record to find the leaf node containing the entry to be deleted.
Remove (search-key value, record pointer) from the leaf node.
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:
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 (Ki–1, Pi), where Pi is the pointer to the deleted node, from its parent, recursively using the above procedure.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
34
Updates on B+-Trees: Deletion
Otherwise, if the node has too few entries due to the removal, but the entries in the node and a sibling do not fit into a single node, then redistribute pointers:
Redistribute the pointers between the node and a sibling such that both have more than the minimum number of entries.
Update the corresponding search-key value in the parent of the node.
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.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
35
Examples of B+-Tree Deletion
Deleting “Srinivasan” causes merging of under-full leaves
Before and after deleting “Srinivasan”
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
36
Examples of B+-Tree Deletion (Cont.)
Deletion of “Singh” and “Wu” from result of previous example
Leaf containing Singh and Wu became underfull, and borrowed a value Kim from its left sibling
Search-key value in the parent changes as a result
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
37
Example of B+-tree Deletion (Cont.)
Before and after deletion of “Gold” from earlier example
Node with Gold and Katz became underfull, and was merged with its sibling
Parent node becomes underfull, and is merged with its sibling
Value separating two nodes (at the parent) is pulled down when merging
Root node then has only one child, and is deleted
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
38
More Observations about B+-trees
Although insertion and deletion operations on B+-trees are complicated, they require relatively few expensive I/O operations.
Cost (in terms of number of I/O operations) of insertion and deletion of a single entry proportional to height of the tree
With K entries and maximum number of pointers in a node of n, worst case complexity of insert/delete of an entry is O(logn/2(K))
In practice, number of I/O operations is less than the worst-case.
Even if the relation is very large, it is quite likely that most of the nonleaf nodes are already in the memory
Splits/merges are rare, most insert/delete operations only affect a leaf node.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Bulk Loading and Bottom-Up Build
Inserting entries one-at-a-time into a B+-tree requires 1 I/O per entry
Assuming leaf level does not fit in memory
Can be very inefficient to insert a large number of entries at a time into a non-clustering index (bulk loading)
Example: consider to build a non-clustering B+-tree index on a relation with 100 million records
If each random I/O operation takes 10 msec on a disk, it would take at least 1 million seconds to build the index.
vs
If the size of each record is 100 bytes and the disk can transfer data at 50 Mbytes/sec, it would take 200 sec to read the entire relation.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Bulk Loading and Bottom-Up Build (cont.)
Efficient alternative 1:
Create and sort entries first (using efficient external-memory sort algorithms to be discussed later)
Insert the entries into the B+-tree in sorted order
All entries that go to a particular leaf node will appear consecutively.
Much improved I/O performance.
Example:
If each leaf contains 100 entries, the leaf level will contain 1 million nodes.
If each sequential I/O operation takes 1 msec on a disk, the same index can be built in 1000 seconds.
Efficient alternative 2: Bottom-up B+-tree construction
As before, create and sort entries
And then create tree layer-by-layer, starting with leaf level
Implemented as part of bulk-load utility by most database systems
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Indexing on Flash
Flash storage is structured as pages and the B+-tree index structure can be used with flash-based SSDs (solid state disks).
Since flash pages are smaller than disk blocks, B+-tree node size is also smaller taller trees and more I/O operations to access data.
The impact on read performance is small because random page reads are so much faster with flash storage (20 to 100 microseconds)
Writes are not in-place (i.e., every update turns into a copy + write of an entire page), and (eventually) require a more expensive erase (2 to 4 msec per block erase).
Bulk-loading and bottom-up construction still useful since they minimize page erases.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Indexing in Main Memory
Main memory is large and cheap enough to keep operational data in-memory and B+-tree can be used to index in-memory data.
When reading a memory location, if it is present in cache, the CPU can complete the read in 1 or 2 nanoseconds, whereas a cache miss results in about 50-100 nanoseconds of delay.
B+- trees with small nodes that fit in cache line (typically about 64 bytes) are preferable to reduce cache misses
Otherwise, search for a key value within a large B+-tree node spanning multiple cache lines results in many cache misses.
Key idea: use large node size to optimize disk access, but structure data within a node using a tree with small node size, instead of using an array.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Indexing
Review of Basic Concepts and Ordered Indices
B+-Tree Index Files
Static Hashing
Dynamic Hashing
Multiple-Key Access
Creation of Indices
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
44
Static Hashing
A bucket is a unit of storage containing one or more entries (a bucket is typically a disk block).
We obtain the bucket of an entry 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 entries for access, insertion as well as deletion.
Entries with different search-key values may be mapped to the same bucket; thus entire bucket has to be searched sequentially to locate an entry.
In a hash index, buckets store entries with pointers to records.
In a hash file-organization, buckets store records.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
45
Hash Functions
The 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.
A good hash function gives an average-case lookup time that is a (small) constant, independent of the number of search keys in the file.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
46
Hash Functions
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.
Is a hash function, which maps name beginning with the ith letter of the alphabet to the ith bucket, uniform?
An 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.
Is a hash function, which divides salary into ranges, 1-10000, 10001-20000, etc, random?
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Example of Hash File Organization
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.
Hash file organization of instructor file, using dept_name as key (See figure in next slide.)
There are 8 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 8.
E.g. h(Music) = 1 h(History) = 2
h(Physics) = 3 h(Elec. Eng.) = 3
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Example of Hash File Organization
Hash file organization of instructor file, using dept_name as key (see previous slide for details).
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
49
Handling of Bucket Overflows
Bucket overflow can occur because of
Insufficient buckets
Skew in distribution of records.
poor hash function
multiple records have same search-key value
Although the probability of bucket overflow can be reduced (e.g., by increasing the number of buckets), it cannot be eliminated; it is handled by using overflow buckets.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
50
Handling of Bucket Overflows (Cont.)
Overflow chaining – the overflow buckets of a given bucket are chained together in a linked list.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
51
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 index on instructor, on attribute ID
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
52
Deficiencies of Static Hashing
In static hashing, function h maps search-key values to a fixed set of bucket addresses B. However, databases grow or shrink with time.
If initial number of buckets is too small, and file grows, performance will degrade due to too much overflows.
If space is allocated for anticipated growth, a significant amount of space will be wasted initially.
If database shrinks again, space will be wasted.
One solution: periodic re-organization of the file with a new hash function.
Expensive, disrupts normal operations
Better solution: allow the number of buckets to be modified dynamically.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
53
Indexing
Review of Basic Concepts and Ordered Indices
B+-Tree Index Files
Static Hashing
Dynamic Hashing
Multiple-Key Access
Creation of Indices
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
54
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 (> 4 billion).
At any time, use only a prefix of the hash function to index into a bucket address table.
Let the length of the prefix be i bits, 0 i 32.
Bucket address table size = 2i. Initially i = 0
Value of i grows and shrinks as the size of the database grows and shrinks.
Multiple entries in the bucket address table may point to a bucket. Thus, actual number of buckets is < 2i.
The number of buckets also changes dynamically due to coalescing and splitting of buckets.
Each bucket j stores the length of common hash prefix, ij
All the entries that point to the same bucket have the same values on the first ij bits.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
55
General Extendable Hash Structure
Suppose i = 2, i1 = 1 and i2 = i3 = 2
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
56
Use of Extendable Hash Structure
To locate the bucket containing search-key value Kj:
1. Compute h(Kj) = X
2. Use the first i high order bits of X as a displacement into bucket address table, and follow the pointer to appropriate bucket
To insert a record with search-key value Kj
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 (next slide.)
Overflow buckets used instead in some cases (will see shortly)
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
57
Insertion in Extendable Hash Structure (Cont)
If i > ij (more than one pointer to bucket j)
allocate a new bucket z, and set ij = iz = (ij + 1)
Update the second half of the bucket address table entries originally pointing to j, to point to z
remove each record in bucket j and reinsert (in j or z)
recompute new bucket for Kj and insert record in the bucket (further splitting is required if the bucket is still full)
If i = ij (only one pointer to bucket j)
If i reaches some limit b, or too many splits have happened in this insertion, create an overflow bucket
Else
increment i and double the size of the bucket address table.
replace each entry in the table by two entries that point to the same bucket.
recompute new bucket address table entry for Kj
Now i > ij so use the first case above.
To split a bucket j when inserting record with search-key value Kj:
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
58
Deletion in Extendable Hash Structure
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).
Coalescing of buckets can be done (can coalesce only with a “buddy” bucket having same value of ij and same ij –1 prefix, if it is present)
Decreasing bucket address table size is also possible
Note: decreasing bucket address table size is an expensive operation and should be done only if number of buckets becomes much smaller than the size of the table
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
59
Use of Extendable Hash Structure: Example
Initial Hash structure; bucket size = 2
Hash function
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
60
Example (Cont.)
Hash structure after insertion of “Srinivasan”, “Wu”
and “Mozart” records
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
61
Example (Cont.)
Hash structure after insertion of Einstein record
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
62
Example (Cont.)
Hash structure after insertion of El Said and Gold records
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
63
Example (Cont.)
Hash structure after insertion of Katz record
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
64
Example (Cont.)
And after insertion of
eleven records
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
65
Example (Cont.)
And after insertion of
Kim record in previous
hash structure
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
66
Extendable Hashing
Benefits of extendable hashing:
Hash performance does not degrade with growth of file
Minimal space overhead
no buckets need to be reserved for future growth
Disadvantages of extendable hashing
Extra level of indirection to find desired record
Bucket address table may itself become very big (larger than memory)
Solution: B+-tree structure to locate desired record in bucket address table
Changing size of bucket address table is an expensive operation
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
67
Ordered Indexing versus Hashing
Design considerations:
Cost of periodic re-organization
Relative frequency of insertions and deletions
Is it desirable to optimize average access time at the expense of worst-case access time?
Expected type of queries:
Hashing is generally better at retrieving records having a specified value of the key.
If range queries are common, ordered indices are to be preferred.
In practice:
Hash-indices are extensively used in-memory but not used much on disk.
Oracle supports static hash organization, but not hash indices.
SQL Server and PostgreSQL do not support hashing on disk.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Indexing
Review of Basic Concepts and Ordered Indices
B+-Tree Index Files
Static Hashing
Dynamic Hashing
Multiple-Key Access
Creation of Indices
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
69
Multiple-Key Access
Use multiple indices for certain types of queries.
Example:
select ID
from instructor
where dept_name = “Finance” and salary = 80000
Possible strategies for processing query using indices on single attributes:
1. Use index on dept_name to find instructors with department name Finance; test salary = 80000
2. Use index on salary to find instructors with a salary of $80000; test dept_name = “Finance”.
3. Use dept_name index to find pointers to all records pertaining to the “Finance” department. Similarly use index on salary. Take intersection of both sets of pointers obtained.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
70
Indices on Multiple Keys
Using separate indices could be less efficient — we may fetch many records (or pointers) that satisfy only one of the conditions.
Composite search keys are search keys containing more than one attribute
E.g. (dept_name, salary)
Lexicographic ordering: (a1, a2) < (b1, b2) if either
a1 < b1, or
a1=b1 and a2 < b2
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
71
Indices on Multiple Attributes
Example: suppose we have an index on combined search-key
(dept_name, salary).
With the where clause
where dept_name = “Finance” and salary = 80000
the index on (dept_name, salary) can be used to fetch only records that satisfy both conditions.
Can also efficiently handle
where dept_name = “Finance” and salary < 80000
But cannot efficiently handle
where dept_name < “Finance” and salary = 80000
May fetch many records that satisfy the first but not the second condition due to the ordering of records in the file.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
72
Other Features
Covering indices
Add extra attributes to index so (some) queries can avoid fetching the actual records.
Example: consider an index on ID attribute of instructor relation.
If we store values of salary attribute in the index, we can answer queries that require salary without accessing the instructor record.
Store extra attributes only at leaf.
Particularly useful for secondary indices.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Indexing
Review of Basic Concepts and Ordered Indices
B+-Tree Index Files
Static Hashing
Dynamic Hashing
Multiple-Key Access
Creation of Indices
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
74
Creation of Indices
Most database systems allow specification of type of index, and clustering.
Create an index
create index
E.g.: create index dept_index on instructor(dept_name)
Use create unique index to indirectly specify and enforce the condition that the search key is a candidate key.
To drop an index
drop index
Indices on primary key created automatically by all databases.
Some database also create indices on foreign key attributes.
For example, such an index might be useful for queries in which the join attribute ID is a foreign-key attribute ID of takes references the primary-key attribute ID of student.
takes ⨝ σname=’Shankar’ (student)
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
75
/docProps/thumbnail.jpeg