DBMS Unit-Iv
DBMS Unit-Iv
But reading several consecutive pages is much cheaper than reading them in random
order
Tapes: Can only read pages in sequence
Indexes are data structures that allow us to find the record ids of records with given
values in index search key fields
Architecture: Buffer manager stages pages from external storage to main memory buffer
pool. File and index layers make calls to the buffer manager.
Primary vs. secondary: If search key contains primary key, then called primary index.
Clustered vs. unclustered: If order of data records is the same as, or `close to’, order of data
entries, then called clustered index.
Cost of retrieving data records through index varies greatly based on whether index is
clustered or not!
Suppose that Alternative (2) is used for data entries, and that the data records are stored
in a Heap file.
To build clustered index, first sort the Heap file (with some free space on each page for
future inserts).
Overflow pages may be needed for inserts. (Thus, order of data recs is `close to’, but not
identical to, the sort order.)
An index on a file speeds up selections on the search key fields for the index.
Any subset of the fields of a relation can be the search key for an index on the relation.
Search key is not the same as key (minimal set of fields that uniquely identify a record in
a relation).
Given data entry k*, we can find record with key k in at most one disk I/O.
(Details soon …)
B+ Tree Indexes
Example B+ Tree
2. Insert/delete: Find data entry in leaf, then change it. Need to adjust parent sometimes.
Hash-Based Indexes
Hashing function h: h(r) = bucket in which (data entry for) record r belongs. h looks
atthe search key fields of r.
Choice of alternative for data entries is orthogonal to the indexing technique used to
– Typically, index contains auxiliary information that directs searches to the desired data
entries
Alternative 1:
– If this is used, index structure is a file organization for data records (instead of a Heap file or
sorted file).
– At most one index on a given collection of data records can use Alternative 1. (Otherwise,
data records are duplicated, leading to redundant storage and potential inconsistency.)
– If data records are very large, # of pages containing data entries is high.
Implies size of auxiliary information in the index is also large, typically.
– Measuring number of page I/O’s ignores gains of pre-fetching a sequence of pages; thus,
even I/O cost is only approximated.
Choice of Indexes
– Which relations should have indexes? What field(s) should be the search key?
Clustered? Hash/tree?
1. One approach: Consider the most important queries in turn. Consider the best plan using
the current indexes, and see if a better plan is possible with an additional index.
– Trade-off: Indexes can make queries go faster, updates slower. Require disk space, too.
Multi-attribute search keys should be considered when a WHERE clause contains several
conditions.
B+ Tree:
B+ Tree: Most Widely Used Index. Insert/delete at log F N cost; keep tree height-balanced. (F
= fanout, N = # leaf pages) Minimum 50% occupancy (except for root). Each node contains d <=
m <= 2d entries. The parameter d is called the order of the tree. Supports equality and
range-searches efficiently.
Example B+ Tree
1. Search begins at root, and key comparisons direct it to a leaf (as in ISAM).
2. Search for 5*, 15*, all data entries >= 24* ...
B+ Trees in Practice
– To split index node, redistribute entries evenly, but push up middle key. (Contrast with leaf
splits.) Splits “grow” tree; root split increases height.
Observe how minimum occupancy is guaranteed in both leaf and index pg splits.
Note difference between copy-up and push-up; be sure you understand the reasons for
this.
Try to re-distribute, borrowing from sibling (adjacent node with same parent as L).
Example Tree After (Inserting 8*, Then) Deleting 19* and 20* ...
Deleting 20* is done with re-distribution. Notice how middle key is copied up.... And
Then Deleting 24*
Must merge.
Observe `toss’ of index entry (on right), and `pull down’ of index entry (below).
Bucket: Hash file stores data in bucket format. Bucket is considered a unit of storage. Bucket
typically stores one complete disk block, which in turn can store one or more records.
Hash Function: A hash function h, is a mapping function that maps all set of search-keys K to
the address where actual records are placed. It is a function from search keyto bucket addresses.