Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Indexing Structures for Files.

Similar presentations


Presentation on theme: "Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Indexing Structures for Files."— Presentation transcript:

1 Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Indexing Structures for Files

2 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Indexes as Access Paths A single-level index is an auxiliary file that makes it more efficient to search for a record in the data file. The index is usually specified on one field of the file (although it could be specified on several fields) One form of an index is a file of entries, which is ordered by field value The index is called an access path on the field.

3 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Indexes as Access Paths (cont.) The index file usually occupies considerably less disk blocks than the data file because its entries are much smaller A binary search on the index yields a pointer to the file record Indexes can also be characterized as dense or sparse A dense index has an index entry for every search key value (and hence every record) in the data file. A sparse (or nondense) index, on the other hand, has index entries for only some of the search values

4 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Indexes as Access Paths (cont.) Example: Given the following data file EMPLOYEE(NAME, SSN, ADDRESS, JOB, SAL,... ) Suppose that: record size R=100 bytesblock size B=1024bytesr=30000 records Then, we get: blocking factor Bfr= B div R= 1024 / 150= 10 records/block number of file blocks b= (r/Bfr)= (30000/10)= 3000 blocks For an index on the SSN field, assume the field size V SSN =9 bytes, assume the record pointer size P R =6 bytes. Then: index entry size R I =(V SSN + P R )=(9+6)=15 bytes index blocking factor Bfr I = B div R I = 1024 / 15= 68 entries/block number of index blocks b= (r/ Bfr I )= (30000/68)= 45 blocks binary search needs log 2 bI= log 245 = 6 block accesses We need one more block access to the data file. Total we need 7 block accesses This is compared to an average linear search cost of: (b/2)= 30000/2= block accesses If the file records are ordered, the binary search cost would be: log 2 b= log = 12 block accesses

5 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Types of Single-Level Indexes Primary Index Defined on an ordered data file The data file is ordered on a key field Includes one index entry for each block in the data file; the index entry has the key field value for the first record in the block, which is called the block anchor A similar scheme can use the last record in a block. A primary index is a nondense (sparse) index, since it includes an entry for each disk block of the data file and the keys of its anchor record rather than for every search value. A major problem with a primary index—as with any ordered file—is insertion and deletion of records.

6 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Primary Index on the Ordering Key Field

7 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Types of Single-Level Indexes Clustering Index Defined on an ordered data file The data file is ordered on a non-key field unlike primary index, which requires that the ordering field of the data file have a distinct value for each record. Includes one index entry for each distinct value of the field; the index entry points to the first data block that contains records with that field value. A clustering index is another example of a nondense index because it has an entry for every distinct value of the indexing field Record insertion and deletion still cause problems because the data records are physically ordered. To alleviate the problem of insertion, it is common to reserve a whole block (or a cluster of contiguous blocks) for each value of the clustering field; all records with that value are placed in the block (or block cluster). This makes insertion and deletion relatively straightforward.

8 Copyright © 2011 Ramez Elmasri and Shamkant Navathe A Clustering Index Example

9 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Another Clustering Index Example

10 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Types of Single-Level Indexes Secondary Index A secondary index provides a secondary means of accessing a file for which some primary access already exists. The secondary index may be on a field which is a candidate key and has a unique value in every record, or a non-key with duplicate values. The index is an ordered file with two fields. The first field is of the same data type as some non-ordering field of the data file that is an indexing field. The second field is either a block pointer or a record pointer. There can be many secondary indexes (and hence, indexing fields) for the same file. Includes one entry for each record in the data file; hence, it is a dense index

11 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Example of a Dense Secondary Index

12 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Example of a Secondary Index

13 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Properties of Index Types

14 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Multi-Level Indexes Because a single-level index is an ordered file, we can create a primary index to the index itself; In this case, the original index file is called the first-level index and the index to the index is called the second-level index. We can repeat the process, creating a third, fourth,..., top level until all entries of the top level fit in one disk block A multi-level index can be created for any type of first-level index (primary, secondary, clustering) as long as the first-level index consists of more than one disk block The value bfr i is called the fan-out of the multilevel index, and usually is referred by the symbol fo. Each level reduces the number of entries at the previous level by a factor of fo. a multilevel index with r1 first-level entries will have approximately t levels, where t = (log fo (r1)).

15 Copyright © 2011 Ramez Elmasri and Shamkant Navathe A Two-Level Primary Index

16 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Multi-Level Indexes Such a multi-level index is a form of search tree However, insertion and deletion of new index entries is a severe problem because every level of the index is an ordered file.

17 Copyright © 2011 Ramez Elmasri and Shamkant Navathe A Node in a Search Tree with Pointers to Subtrees Below It

18 Copyright © 2011 Ramez Elmasri and Shamkant Navathe

19 Dynamic Multilevel Indexes Using B- Trees and B+-Trees Most multi-level indexes use B-tree or B+-tree data structures because of the insertion and deletion problem This leaves space in each tree node (disk block) to allow for new index entries These data structures are variations of search trees that allow efficient insertion and deletion of new search values. In B-Tree and B+-Tree data structures, each node corresponds to a disk block Each node is kept between half-full and completely full

20 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Dynamic Multilevel Indexes Using B- Trees and B+-Trees (cont.) An insertion into a node that is not full is quite efficient If a node is full the insertion causes a split into two nodes Splitting may propagate to other tree levels A deletion is quite efficient if a node does not become less than half full If a deletion causes a node to become less than half full, it must be merged with neighboring nodes

21 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Difference between B-tree and B+-tree In a B-tree, pointers to data records exist at all levels of the tree In a B+-tree, all pointers to data records exists at the leaf-level nodes A B+-tree can have less levels (or higher capacity of search values) than the corresponding B-tree

22 Copyright © 2011 Ramez Elmasri and Shamkant Navathe B-Trees. The B-tree is always balanced and that the space wasted by deletion, if any, never becomes excessive. A B-tree of order p, when used as an access structure on a key field to search for records in a data file, can be defined as follows: 1. Each internal node in the B-tree is of the form, P2,,...,, Pq> where q ≤ p. Each Pi is a tree pointer—a pointer to another node in the B-tree. Each Pri is a data pointer—a pointer to the record whose search key field value is equal to Ki (or to the data file block containing that record). 2. Within each node, K1 < K2 <... < Kq−1. 3. For all search key field values X in the subtree pointed at by Pi (the ith sub-tree) we have: Ki–1 < X < Ki for 1 < i < q; X < Ki for i = 1; and Ki–1 < X for i = q. 4. Each node has at most p tree pointers. 5. Each node, except the root and leaf nodes, has at least (p/2) tree pointers. The root node has at least two tree pointers unless it is the only node in the tree. 6. A node with q tree pointers, q ≤ p, has q – 1 search key field values (and hence has q – 1 data pointers). 7. All leaf nodes are at the same level. Leaf nodes have the same structure as internal nodes except that all of their tree pointers Pi are N U LL.

23 Copyright © 2011 Ramez Elmasri and Shamkant Navathe B-tree Structures

24 Copyright © 2011 Ramez Elmasri and Shamkant Navathe B+-Trees The structure of the internal nodes of a B+-tree of order p is as follows: 1. Each internal node is of the form where q ≤ p and each Pi is a tree pointer. 2. Within each internal node, K1 < K2 <... < Kq−1. 3. For all search field values X in the subtree pointed at by Pi, we have K i−1 < X≤ K i for 1 < i < q; X ≤ K i for i = 1; and K i−1 < X for i = q 4. Each internal node has at most p tree pointers. 5. Each internal node, except the root, has at least (p/2) tree pointers. The root node has at least two tree pointers if it is an internal node. 6. An internal node with q pointers, q ≤ p, has q − 1 search field values. The structure of the leaf nodes of a B+-tree of order p is as follows: 1. Each leaf node is of the form,,...,, Pnext> where q ≤ p, each Pri is a data pointer, and Pnext points to the next leaf node of the B+-tree. 2. Within each leaf node, K1 ≤ K2..., Kq−1, q ≤ p. 3. Each Pri is a data pointer that points to the record whose search field value is Ki or to a file block containing the record (or to a block of record pointers that point to records whose search field value is Ki if the search field is not a key). 4. Each leaf node has at least (p/2) values. 5. All leaf nodes are at the same level.

25 Copyright © 2011 Ramez Elmasri and Shamkant Navathe The Nodes of a B+-tree

26 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Example B+ Tree Search begins at root, and key comparisons direct it to a leaf. Search for 5*, 15*, all data entries >= 24*... * Based on the search for 15*, we know it is not in the tree! Root * 3*5* 13*14*16* 19*20*24*25*27* 30*33*34* 38* 39* 13 17*

27 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Inserting a Data Entry into a B+ Tree Find correct leaf L. Put data entry onto L. If L has enough space, done! Else, must split L (into L and a new node L2) Redistribute entries evenly, copy up middle key. Insert index entry pointing to L2 into parent of L. This can happen recursively To split index node, redistribute entries evenly, but push up middle key. (Contrast with leaf splits.) Splits “grow” tree; root split increases height. Tree growth: gets wider or one level taller at top.

28 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Example of an Insertion in a B+-tree

29 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Deleting a Data Entry from a B+ Tree Start at root, find leaf L where entry belongs. Remove the entry. If L is at least half-full, done! If L has only d-1 entries, Try to re-distribute, borrowing from sibling (adjacent node with same parent as L). If re-distribution fails, merge L and sibling. If merge occurred, must delete entry (pointing to L or sibling) from parent of L. Merge could propagate to root, decreasing height.

30 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Example of a Deletion in a B+-tree

31 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Bulk Loading of a B+ Tree If we have a large collection of records, and we want to create a B+ tree on some field, doing so by repeatedly inserting records is very slow. Bulk Loading can be done much more efficiently. Initialization: Sort all data entries, insert pointer to first (leaf) page in a new (root) page. 3* 4* 6*9*10*11*12*13* 20*22* 23*31* 35* 36*38*41*44* Sorted pages of data entries; not yet in B+ tree Root

32 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Bulk Loading (Contd.) Index entries for leaf pages always entered into right- most index page just above leaf level. When this fills up, it splits. (Split may go up right-most path to the root.) Much faster than repeated inserts, especially when one considers locking! 3* 4* 6*9*10*11*12*13* 20*22* 23*31* 35* 36*38*41*44* Root Data entry pages not yet in B+ tree * 4* 6*9*10*11*12*13* 20*22* 23*31* 35* 36*38*41*44* 6 Root not yet in B+ tree Data entry pages

33 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Summary of Bulk Loading Option 1: multiple inserts. Slow. Does not give sequential storage of leaves. Option 2: Bulk Loading Has advantages for concurrency control. Fewer I/Os during build. Leaves will be stored sequentially (and linked, of course). Can control “fill factor” on pages.


Download ppt "Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Indexing Structures for Files."

Similar presentations


Ads by Google