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 KK we:
      • Find index record with largest search-key value leqKleq K
      • 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 n/2n/2 to nn children
  • a leaf node has between [(n1)/2][(n-1)/2] and n1n-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 00 and (n1)(n-1) 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 i=1,2,...,n1i = 1, 2, . . ., n–1, pointer PiP_i points to a file record with search-key value KiK_i,
  • If Li,LjL_i, L_j are leaf nodes and i<ji < j, LiL_i’s search-key values are less than or equal to LjL_j’s search-key values
  • PnP_n 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 P1P_1 points are less than K1K_1
  • For 2in12 \leq i \leq n-1, all the search keays in the subtree to which PiP_i points have values greater than or equal to Ki1K_{i-1} and less than KiK_i
  • All the search keys in the subtree to which PnP_n points have values greater than or equal to Kn1K_{n-1}

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 2(n/2)2*(n/2) values
    • Next level has at least 2(n/2)(n/2)2*(n/2) * (n/2) values
    • and so on ..
    • If there are KK search key values in the file, the tree height is no more than logn/2(K)\log_{n/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.

Queries on B+ Trees

function find(v):
C=root
while (C is not a leaf node):
Let i be least number s.t. v <= Ki
if there is no such number i:
Set C = last non null pointer in Clustering
elif (v = C.Ki):
Set C = P(i+1)
else
Set C = C.P_i
if for some i, Ki = v:
return P_i
else
return 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 [logn/2(K)][\log_{n/2}(K)]
  • a node is generally the same size as a disk block, typically 4 kB and nn is typically around 100 (40 B per index entry)
  • with 1 million search key values and n=100n=100 at most log50(1000000)=4\log_{50}(1000000) = 4 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 aia_i is not unique, create instead an index on a composite key (ai,Ap)(a_i, A_p), which is unique, ApA_p 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

  1. If there is room in the leaf node, insert (v, pr) pair in the leaf node
  2. 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 nn (search-key value, pointer) pairs including the one being inserted in sorted order. Place the first n/2n/2 in the original node, and the rest in a new node
  • let the new bye pp, and let kk be the least key value in pp. Insert (k,p)(k,p) 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 VV be the search key value of the record, and PrPr be the pointer to the record.

  • Remove (Pr,V)(Pr, V) 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 O(log(n/2)(K))O(\log_{(n/2)}(K))
  • 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, 1/21/2 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 2/32/3 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”
    • Keys in leaf node can be compressed by sharing common prefixes

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