Indexing
Basic Concepts
Indexing mechanisms used to speed up access to desired data such as author catalog in library.
Uses search key which is an attribute or set of attributes used to look up records in a file. An index file consists of records (called index entries) of the form (search-key, pointer).
Index files are typically much smaller than the original file. We will consider several indexing techniques. No one technique is the best. Each technique is best suited for a particular database application.
There are two basic kinds of indices:
- Ordered indices that are search keys are stored in sorted order
- Hash indices that are search keys distributed uniformly across "buckets". The buckets to which a value is assigned is determined by a function, called a hash function.
Types of Indexes
- Ordered index
- entries are stored sorted on the search key value
- uses a primary index or clustering index
- Dense index
- index record appears for every search key value
- contains search key value and a pointer to the actual record
- Sparse index
- contains index records for only some search-key values
- applicable when records are sequentially ordered on search-key
- To locate a record with search key value we:
- Find index record with largest search-key value
- Search file sequentially starting at the record to which the index record points
- less space and less maintenance overhead for insertions and deletions when compared with dense index
- Multilevel Index
- a sparse index of the basic index
- basic index file
- this solves the problem where index size may still grow too large
Updating Index
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
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
Clustering vs Nonclustering indices
Indices offer substantial benefits when searching for records. However, they impose overhead on database modification because when a record is inserted or deleted every index on the relation must be updated. Also when a record is updated, any index on an updated attribute must be updated.
Sequential scan using clustering index is efficient, but a sequential scan using a secondary (non clustering) index is expensive on magnetic disk. Each record access may fetch a new block from disk. Each block fetch on magnetic disk requires about 5 to 10 ms.
Indices on Multiple keys
- In general, a search key can have more than one attribute
- Composite search key: search key containing more than one attribute
B+ Trees
- Disadvantage of indexed-sequential files organization:
- Performance degrades as file grows, since many overflow blocks get created.
- Periodic reorganization of entire file is required: costly.
- 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
A neat visualization tool for B+ Trees.
A B+-tree is a rooted tree satisfying the following properties:
- all paths from root to leaf are of the same length
- each node that is not a root or a leaf has between to children
- a leaf node has between and 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 and values
A typical nodes has
- search key values
- pointers to children for non-leaf nodes
- pointers to records or buckets of records for leaf nodes
- the search keys are ordered
Properties of a leaf node:
- For , pointer points to a file record with search-key value ,
- If are leaf nodes and , ’s search-key values are less than or equal to ’s search-key values
- points to next leaf node in search-key order
Properties of non-leaf nodes:
- Form a multilevel sparse index on the leaf nodes
- All the search keys in the subtree to which points are less than
- For , all the search keays in the subtree to which points have values greater than or equal to and less than
- All the search keys in the subtree to which points have values greater than or equal to
Observations about B+ trees
- 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 of levels
- Level below root has at least values
- Next level has at least values
- and so on ..
- If there are search key values in the file, the tree height is no more than 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.
Queries on B+ Trees
function find(v):C=rootwhile (C is not a leaf node):Let i be least number s.t. v <= Kiif there is no such number i:Set C = last non null pointer in Clusteringelif (v = C.Ki):Set C = P(i+1)elseSet C = C.P_iif for some i, Ki = v:return P_ielsereturn null
- Range queries: find all records with search key values in a given range
- See book for details of functions findRange() which returns set of all such records
- Real implementations usually provide an iterator interface to fetch matching records one at a time, using a next() function
- if there are K search key values in the file, the height of the tree is no more than
- a node is generally the same size as a disk block, typically 4 kB and is typically around 100 (40 B per index entry)
- with 1 million search key values and at most nodes are accessed in a lookup traversal from root to leaf
- contrast this with a balanced binary tree with 1 million search key 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 ms
Non-Unique Keys
- if a search key is not unique, create instead an index on a composite key , which is unique, could be a primary key, record ID, or any other attribute that guarantees uniqueness
Updates on B+ Trees: Insertion
Assume record already added to the file. Let
- pr be pointer to the record, and let
- v be the search key value of the record
Find the leaf node in which the search-key value would appear
- If there is room in the leaf node, insert (v, pr) pair in the leaf node
- Otherwise, split the node (along with the new (v, pr) entry) as discussed in the next slide, and propagate updates to parent nodes.
Splitting a leaf node:
- take the (search-key value, pointer) pairs including the one being inserted in sorted order. Place the first in the original node, and the rest in a new node
- let the new bye , and let be the least key value in . Insert in the parent of the node being split
- if the parent is full, split 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
Updates on B+ Trees: Deletion
Assume record already deleted from file. Let be the search key value of the record, and be the pointer to the record.
- Remove 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
- 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.
Complexity of Updates
- 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 fanout of n, worst case complexity of insert/delete of an entry is
- In practice, number of I/O operations is less:
- Internal nodes tend to be in buffer
- Splits/merges are rare, most insert/delete operations only affect a leaf node
- Average node occupancy depends on insertion order
- 2/3rds with random, with insertion in sorted order
B+ Tree File Organization
- Leaf nodes in a B+ tree file organization store records, instead of pointers
- Helps keep data records clustered even when there are insertions/deletions/updates
- 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
- Good space utilization important since records use more space than pointers.
- To improve space utilization, involve more sibling nodes in redistribution
during splits and merges
- Involving 2 siblings in redistribution (to avoid split / merge where possible) results in each node having at least entries
Other issues
Record relocation and secondary indices
- If a record moves, all secondary indices that store record pointers have to be updated
- Node splits in B+-tree file organizations become very expensive
- Solution: use search key of B+-tree file organization instead of record
pointer in secondary index
- Add record-id if B+-tree file organization search key is non-unique
- Extra traversal of file organization to locate record
- Higher cost for queries, but node splits are cheap
Indexing Strings
- Variable length strings as keys
- Variable fanout
- Use space utilization as criterion for splitting, not number of pointers
- Prefix compression
- Key values at internal nodes can be prefixes of full key
- Keep enough characters to distinguish entries in the subtrees
separated by the key value
- E.g., “Silas” and “Silberschatz” can be separated by “Silb”
- Keep enough characters to distinguish entries in the subtrees
separated by the key value
- Keys in leaf node can be compressed by sharing common prefixes
- Key values at internal nodes can be prefixes of full key
Bulk Loading and Bottom-Up Build
- Inserting entries one-at-a-time into a B+-tree requires 1 IO per entry
- assuming leaf level does not fit in memory
- can be very inefficient for loading a large number of entries at a time (bulk loading)
- Efficient alternative 1:
- sort entries first (using efficient external-memory sort algorithms discussed later in Section 12.4)
- insert in sorted order
- insertion will go to existing page (or cause a split)
- much improved IO performance, but most leaf nodes half full
- Efficient alternative 2: Bottom-up B+-tree construction
- As before sort entries
- And then create tree layer-by-layer, starting with leaf level
- details as an exercise
- Implemented as part of bulk-load utility by most database systems
B Tree Index files
- Similar to B+-tree, but B-tree allows search-key values to appear only once; eliminates redundant storage of search keys.
- Search keys in nonleaf nodes appear nowhere else in the B-tree; an additional pointer field for each search key in a nonleaf node must be included.
- Nonleaf node – pointers Bi are the bucket or file record pointers.
Advantages of B-Tree indices:
- May use less tree nodes than a corresponding B+ -Tree.
- Sometimes possible to find search-key value before reaching leaf node.
Disadvantages of B-Tree indices:
- Only small fraction of all search-key values are found early
- Non-leaf nodes are larger, so fan-out is reduced. Thus, B-Trees typically have greater depth than corresponding B+-Tree
- Insertion and deletion more complicated than in B+-Trees
- Implementation is harder than B+-Trees.
- Typically, advantages of B-Trees do not out weigh disadvantages
Indexing on Flash
- Random I/O cost much lower on flash
- 20 to 100 microseconds for read/write
- Writes are not in-place, and (eventually) require a more expensive erase
- Optimum page size therefore much smaller
- Bulk-loading still useful since it minimizes page erases
- Write-optimized tree structures (discussed later) have been adapted to minimize page writes for flash-optimized search trees
Indexing in Main Memory
- Random access in memory
- Much cheaper than on disk/flash
- But still expensive compared to cache read
- Data structures that make best use of cache preferable
- Binary search for a key value within a large B+-tree node results in many cache misses
- B+-trees with small nodes that fit in cache line are preferable to reduce 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