Presentation is loading. Please wait.

Presentation is loading. Please wait.

©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B+-Tree Index Files B-Tree.

Similar presentations


Presentation on theme: "©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B+-Tree Index Files B-Tree."— Presentation transcript:

1 ©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B+-Tree Index Files B-Tree Index Files Static Hashing Dynamic Hashing Comparison of Ordered Indexing and Hashing

2 ©Silberschatz, Korth and Sudarshan12.2Database System Concepts Basic Concepts of Indexing Speed up data access  E.g., book indices 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

3 ©Silberschatz, Korth and Sudarshan12.3Database System Concepts Index Evaluation Metrics Access types supported efficiently. E.g.,  records with a specified value in the attribute  records with an attribute value falling in a specified range of values. Access time Insertion time Deletion time Space overhead

4 ©Silberschatz, Korth and Sudarshan12.4Database System Concepts Ordered Indices Index entries are sorted by search key values  E.g., author catalog in library. Primary index (clustering index)  the index whose search key specifies the sequential order of the file  Index-sequential files: files ordered on a primary index  The search key of a primary index is usually the primary key Secondary index (non-clustering index)  An index whose search key specifies an order different from the sequential order of the file.

5 ©Silberschatz, Korth and Sudarshan12.5Database System Concepts Dense Index Files Dense index — Index record exists for every search-key value in the file.

6 ©Silberschatz, Korth and Sudarshan12.6Database System Concepts Sparse Index Files 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 K we:  Find index record with largest search-key value < K  Search file sequentially starting at the record to which the index record points Less space and less maintenance overhead for insertions and deletions. 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.

7 ©Silberschatz, Korth and Sudarshan12.7Database System Concepts Example of Sparse Index Files

8 ©Silberschatz, Korth and Sudarshan12.8Database System Concepts Multilevel Index If primary index does not fit in memory, access becomes expensive. To reduce number of disk accesses to index records, 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.

9 ©Silberschatz, Korth and Sudarshan12.9Database System Concepts Multilevel Index (Cont.)

10 ©Silberschatz, Korth and Sudarshan12.10Database System Concepts Index Update: Insertion/Deletion Single-level index insertion:  Look up 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.  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. Single-level index deletion:  Dense indices  Sparse indices Multilevel insertion (deletion)  Simple extensions of the single-level algorithms

11 ©Silberschatz, Korth and Sudarshan12.11Database System Concepts Secondary Indices 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 1: In the account database stored sequentially by account number, we may want to find all accounts in a particular branch  Example 2: Find all accounts with a specified balance or range of balances A secondary index with an index record for each search-key value  Index record points to a bucket that contains pointers to all the actual records with that particular search-key value

12 ©Silberschatz, Korth and Sudarshan12.12Database System Concepts Secondary Index on balance field of account

13 ©Silberschatz, Korth and Sudarshan12.13Database System Concepts Primary and Secondary Indices Secondary indices have to be dense. Indices offer substantial benefits when searching for records  Compare to the sequential access without any index When a file is modified, every index on the file must be updated - overhead Sequential scan using primary index is efficient, but a sequential scan using a secondary index is expensive

14 ©Silberschatz, Korth and Sudarshan12.14Database System Concepts B + -Tree Index Files Disadvantage of indexed-sequential files  Performance degrades as file grows, since many overflow blocks get created. Periodic reorganization of the entire file is required. B + -tree index files:  Automatically reorganize with small, local changes during insertions and deletions. Reorganization of entire file is hardly required. Disadvantage of B + -trees  Extra insertion and deletion overhead, space overhead. B + -tree indices are an alternative to indexed-sequential files.

15 ©Silberschatz, Korth and Sudarshan12.15Database System Concepts B + -Tree Index Files (Cont.) All paths from root to leaf are of the same length Each node that is not a root or a leaf has between [n/2] and n children. 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:

16 ©Silberschatz, Korth and Sudarshan12.16Database System Concepts B + -Tree Node Structure Typical node  K i are the search-key values  P i are pointers to children (for non-leaf nodes) or pointers to records or buckets of records (for leaf nodes). The search-keys in a node are ordered K 1 < K 2 < K 3 <... < K n–1

17 ©Silberschatz, Korth and Sudarshan12.17Database System Concepts Leaf Nodes in B + -Trees For i = 1, 2,..., n–1, pointer P i either points to a file record with search key value K i, or to a bucket of pointers to file records, each record having search-key value K i. Only need bucket structure if search-key does not form a primary key. If L i, L j are leaf nodes and i < j, L i ’s search-key values are less than L j ’s search-key values P n points to next leaf node in search-key order Properties of a leaf node:

18 ©Silberschatz, Korth and Sudarshan12.18Database System Concepts 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 n pointers:  All the search-keys in the subtree to which P 1 points are less than K 1  For 2  i  n – 1, all the search-keys in the subtree to which P i points have values greater than or equal to K i–1 and less than K i+1

19 ©Silberschatz, Korth and Sudarshan12.19Database System Concepts Example of a B + -tree B + -tree for account file (n = 3)

20 ©Silberschatz, Korth and Sudarshan12.20Database System Concepts Example of B + -tree Leaf nodes must have between 2 and 4 values (  (n–1)/2  and n –1, with n = 5). Non-leaf nodes other than root must have between 3 and 5 children (  (n/2  and n with n =5). Root must have at least 2 children. B + -tree for account file (n - 5)

21 ©Silberschatz, Korth and Sudarshan12.21Database System Concepts 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 number of levels (logarithmic in the size of the main file), thus searches can be conducted efficiently.  Why is this important? Insertions and deletions to the main file can be handled efficiently, as the index can be restructured in logarithmic time (as we shall see).

22 ©Silberschatz, Korth and Sudarshan12.22Database System Concepts Queries on B + -Trees Find all records with a search key value of k. 1. Start with the root node 1. Examine the node for the smallest search key value > k. 2. If such a value K j exists, follow P i to the child node 3. Otherwise k  K n–1. Follow P n to the child node. 2. If the node reached by following the pointer is not a leaf node, repeat the above procedure. 3. Eventually reach a leaf node. If for some i, key K i = k follow pointer P i to the desired record or bucket. Else no record with search key value k exists.

23 ©Silberschatz, Korth and Sudarshan12.23Database System Concepts Queries on B +- Trees (Cont.) In processing a query, a path is traversed in the tree from the root to some leaf node. If there are K search-key values in the file, the path is no longer than  log  n/2  (K) . With 1 million search key values and n = 100, at most log 50 (1,000,000) = 4 nodes are accessed in a lookup. A balanced binary tree with 1 million search key values — 20 nodes are accessed in a lookup  The difference is significant since every node access may need a disk I/O, costing around 20 milliseconds! 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).

24 ©Silberschatz, Korth and Sudarshan12.24Database System Concepts Updates on B + -Trees: Insertion Find the leaf node to which the search key value should belong If the search key value is already in the leaf node  Add the record to the file  Insert the pointer into the bucket If the search key value is not there  Add the record to the file  If there is room in the leaf node, insert (key-value, pointer) pair in the leaf node  Otherwise, split the node

25 ©Silberschatz, Korth and Sudarshan12.25Database System Concepts Updates on B + -Trees: Insertion (Cont.) Splitting a node:  take the n(search-key value, pointer) pairs 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 proceeds upwards till a node that is not full is found. In the worst case, split the root node, increasing the height of the tree by 1.

26 ©Silberschatz, Korth and Sudarshan12.26Database System Concepts Updates on B + -Trees: Insertion (Cont.) B + -Tree before and after insertion of “Clearview”

27 ©Silberschatz, Korth and Sudarshan12.27Database System Concepts Updates on B + -Trees: Deletion Find the record to be deleted, and remove it from the file Remove (search-key value, pointer) from the leaf node if there is no bucket or if the bucket has become empty 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  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 (K i–1, P i ), where P i is the pointer to the deleted node, from its parent, recursively using the above procedure.

28 ©Silberschatz, Korth and Sudarshan12.28Database System Concepts Updates on B + -Trees: Deletion Otherwise, if the node has too few entries due to the removal, and the entries in the node and a sibling do NOT fit into a single node, then  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.

29 ©Silberschatz, Korth and Sudarshan12.29Database System Concepts Examples of B + -Tree Deletion The removal of the leaf node containing “Downtown” did not result in its parent having too little pointers. So the cascaded deletions stopped with the deleted leaf node’s parent. Before and after deleting “Downtown”

30 ©Silberschatz, Korth and Sudarshan12.30Database System Concepts Examples of B + -Tree Deletion (Cont.) Node with “Perryridge” becomes underfull (actually empty, in this special case) and merged with its sibling. As a result “Perryridge” node’s parent became underfull, and was merged with its sibling (and an entry was deleted from their parent) Root node then had only one child, and was deleted and its child became the new root node Deletion of “Perryridge” from result of previous example

31 ©Silberschatz, Korth and Sudarshan12.31Database System Concepts Example of B + -tree Deletion (Cont.) Before and after deletion of “Perryridge” from earlier example

32 ©Silberschatz, Korth and Sudarshan12.32Database System Concepts B + -Tree File Organization Performance degradation problem of  index lookup: use B + -Tree indices  data file access: use B + -tree file organization Let the leaf nodes in a B + -tree file organization store records (instead of pointers) Insertion and deletion are handled in the same way as insertion and deletion of entries in a B + -tree index.

33 ©Silberschatz, Korth and Sudarshan12.33Database System Concepts B + -Tree File Organization (Cont.) Good space utilization is important  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 Example of B + -tree File Organization

34 ©Silberschatz, Korth and Sudarshan12.34Database System Concepts B-Tree Index Files nB-tree allows search-key values to appear only once to eliminates redundant storage of search keys. Advantages May use less tree nodes than a corresponding B + -Tree. Disadvantages: Non-leaf nodes are larger, so fan-out is reduced. B-Trees typically have greater depth Insertion and deletion are more complicated. Implementation is harder. Typically, advantages of B-Trees do not out weigh disadvantages.

35 ©Silberschatz, Korth and Sudarshan12.35Database System Concepts Static Hashing A bucket is a unit of storage containing one or more records (a bucket is typically a disk block). In a hash file organization we obtain the bucket of a record directly 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 records for access, insertion as well as deletion. Records with different search-key values may be mapped to the same bucket; thus entire bucket has to be searched sequentially to locate a record.

36 ©Silberschatz, Korth and Sudarshan12.36Database System Concepts Example of Hash File Organization (Cont.) There are 10 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 10  E.g. h(Perryridge) = 5 h(Round Hill) = 3 h(Brighton) = 3 Hash file organization of account file, using branch-name as key (See figure in next slide.)

37 ©Silberschatz, Korth and Sudarshan12.37Database System Concepts Example of Hash File Organization Hash file organization of account file, using branch-name as key (see previous slide for details).

38 ©Silberschatz, Korth and Sudarshan12.38Database System Concepts Hash Functions Ideal hash function  Uniform: each bucket is assigned the same number of search key values from the set of all possible values.  Random: 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  Add the binary representations of all the characters in the string  Return (sum % number of buckets)

39 ©Silberschatz, Korth and Sudarshan12.39Database System Concepts Handling of Bucket Overflows Bucket overflow can occur because of  Insufficient buckets  Skew in distribution of records. This can occur due to two reasons:  chosen hash function produces non-uniform distribution of key values  multiple records have same search key value Although the probability of bucket overflow can be reduced, it cannot be eliminated; it is handled by using overflow buckets.

40 ©Silberschatz, Korth and Sudarshan12.40Database System Concepts Handling of Bucket Overflows (Cont.) Overflow chaining (closed hashing)  Overflow buckets of a given bucket are chained together in a linked list. Linear probing

41 ©Silberschatz, Korth and Sudarshan12.41Database System Concepts Example of Hash Index

42 ©Silberschatz, Korth and Sudarshan12.42Database System Concepts Deficiencies of Static Hashing In static hashing, function h maps search-key values to a fixed set of B of bucket addresses.  Databases grow with time. If initial number of buckets is too small, performance will degrade due to too much overflows.  If file size at some point in the future is anticipated and number of buckets allocated accordingly, significant amount of space will be wasted initially.  If database shrinks, again space will be wasted.  One option is periodic re-organization of the file with a new hash function, but it is very expensive. Allow the number of buckets to be modified dynamically.

43 ©Silberschatz, Korth and Sudarshan12.43Database System Concepts Extendible Hashing Situation: Bucket becomes full. Why not re-organize file by doubling # of buckets?  Reading and writing all pages is expensive!  Idea: Use directory of pointers to buckets, double # of buckets by doubling the directory, splitting just the bucket that overflowed!  Directory much smaller than file, so doubling it is much cheaper. Only one page of data entries is split. No overflow page!  Trick lies in how hash function is adjusted!

44 ©Silberschatz, Korth and Sudarshan12.44Database System Concepts Example Directory is array of size 4. To find bucket for r, take last `global depth’ # bits of h(r); we denote r by h(r).  If h(r) = 5 = binary 101, it is in bucket pointed to by 01. v Insert : If bucket is full, split it ( allocate new page, re-distribute ). v If necessary, double the directory. (As we will see, splitting a bucket does not always require doubling; we can tell by comparing global depth with local depth for the split bucket.) 13* 00 01 10 11 2 2 2 2 2 LOCAL DEPTH GLOBAL DEPTH DIRECTORY Bucket A Bucket B Bucket C Bucket D DATA PAGES 10* 1*21* 4*12*32* 16* 15*7*19* 5*

45 ©Silberschatz, Korth and Sudarshan12.45Database System Concepts Insert h(r)=20 (Causes Doubling) 20* 00 01 10 11 2 2 2 2 LOCAL DEPTH 2 2 DIRECTORY GLOBAL DEPTH Bucket A Bucket B Bucket C Bucket D Bucket A2 (`split image' of Bucket A) 1* 5*21*13* 32* 16* 10* 15*7*19* 4*12* 19* 2 2 2 000 001 010 011 100 101 110 111 3 3 3 DIRECTORY Bucket A Bucket B Bucket C Bucket D Bucket A2 (`split image' of Bucket A) 32* 1*5*21*13* 16* 10* 15* 7* 4* 20* 12* LOCAL DEPTH GLOBAL DEPTH

46 ©Silberschatz, Korth and Sudarshan12.46Database System Concepts Points to Note 20 = binary 10100. Last 2 bits (00) tell us r belongs in A or A2. Last 3 bits needed to tell which.  Global depth of directory: Max # of bits needed to tell which bucket an entry belongs to.  Local depth of a bucket: # of bits used to determine if an entry belongs to this bucket. When does bucket split cause directory doubling?  Before insert, local depth of bucket = global depth. Insert causes local depth to become > global depth; directory is doubled by copying it over and `fixing’ pointer to split image page. (Use of least significant bits enables efficient doubling via copying of directory!)

47 ©Silberschatz, Korth and Sudarshan12.47Database System Concepts Directory Doubling 0 0101 1010 1 2 Why use least significant bits in directory? ó Allows doubling via copying! 000 001 010 011 3 100 101 110 111 vs. 0 1 1 6* 6 = 110 0 1010 0101 1 2 3 0 1 1 6* 6 = 110 000 100 010 110 001 101 011 111 Least SignificantMost Significant

48 ©Silberschatz, Korth and Sudarshan12.48Database System Concepts Comments on Extendible Hashing If directory fits in memory, equality search answered with one disk access; else two.  100MB file, 100 bytes/rec & 4K pages, contains 1,000,000 records (as data entries) and 25,000 directory elements; chances are high that directory will fit in memory.  Directory grows in spurts, and, if the distribution of hash values is skewed, directory can grow large.  Multiple entries with same hash value cause problems! Delete: If removal of data entry makes bucket empty, can be merged with `split image’. If each directory element points to same bucket as its split image, can halve directory.

49 ©Silberschatz, Korth and Sudarshan12.49Database System Concepts Extendable Hashing vs. Other Schemes Benefits of extendable hashing:  Hash performance does not degrade with growth of file  Minimal space overhead Disadvantages of extendable hashing  Extra level of indirection to find desired record  Bucket address table may itself become very big (larger than memory)  Need a tree structure to locate desired record in the structure!  Changing size of bucket address table is an expensive operation Linear hashing is an alternative mechanism which avoids these disadvantages at the possible cost of more bucket overflows

50 ©Silberschatz, Korth and Sudarshan12.50Database System Concepts Comparison of Ordered Indexing and Hashing Frequency of insertions and deletions Cost of periodic re-organization 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

51 End of Chapter


Download ppt "©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B+-Tree Index Files B-Tree."

Similar presentations


Ads by Google