210 likes | 382 Views
Indexes. A Heap file allows record retrieval: by specifying the rid , or by scanning all records sequentially Sometimes, retrieval of records by specifying the values in one or more fields is needed (semantic search or value-based query), e.g.,
E N D
Indexes • A Heap file allows record retrieval: • by specifying the rid, or • by scanning all records sequentially • Sometimes, retrieval of records by specifying the values in one or more fields is needed (semantic search or value-based query), e.g., • Find all students in CS dept; Find students with gpa > 3 • Indexes are files (separate from the data file they index) that enable answering these value-based queries efficiently. • Indexes contain “search keys”, k, which are values from the attribute being indexed and “data entries”, k*, which lead us to the records containing the search key value (usually pointers).
Index Classification • Primary vs. secondary: If the search key contains the clustered primary key, then it is called a primary index, else it is called a secondary index. • Clustered vs. unclustered: If the closeness of the data records is the same as the closeness of the data entries, the index is called a clustered index. • A file can be clustered on at most 1 attribute (search key) • Cost of retrieving data records through an index varies greatly based on whether index is clustered or not!
Primary Index PRIMARY INDEX: I(k,p) k = ordered or clustered "key" field values from ordered or clustered field of file withuniqueness property (individual value occurrences are "unique" i.e., each value can occur at most once.) p = pointer to page containing record(s) with value, k Primary indexes can be either: DENSE: (every record is indexed) or NON-DENSE: only key-values of records at the beginning of a page are indexed (anchor record of page). (and then the pointer is page-# only) Example: Assume the blocking factor (bfr) is 2 which means 2 records/page. STUDENT |S#|SNAME |LCODE |pg |17|BAID |NY2091|1 |25|CLAY |NJ5101|1 |32|THAISZ|NJ5102|2 |38|GOOD |FL6321|2 |57|BROWN |NY2092|3 |83|THOM |ND3450|3 Non-dense Primary Index on S# |S#|pg |17| 1 |32| 2 |57| 3 Dense Primary Index on S# |S#|pg offset |17| 1 0 |25| 1 1 |32| 2 0 |38| 2 1 |57| 3 0 |83| 3 1 RID Inserting and deleting are major problems. - must move records to maintain ordering - anchors change (in non-dense case)
Clustering Index like a primary index except that the attribute nee not be a key - the file must be clustered on the attribute, k - the pointer for any k is the address of 1st page with that k-value ENROLL2 |S#|C#|GRADE pg |17|6 | 96 |1 |25|6 | 76 |1 |32|6 | 62 |2 |38|6 | 98 |2 |32|6 | 91 |3 |25|7 | 68 |3 |32|8 | 89 |4 |17|9 | 95 |4 |C#|pg| Dense Clustering_Index on C# |6 | 1| |7 | 3| |8 | 4| |9 | 4| |C#|pg| Non-dense Clustering_Index on C# |6 | 1| (indexing new anchor records only) |8 | 4| There's no more search overhead with this 2nd type of non-dense clustering index, but - How can you know which page has C#=7? (search pages starting at pg=1) - How can you know which page has C#=9? (search pags starting at pg=4)
Option3: Use 1 index entry for each value, 1 pointer to "list" or "linked list" of record pointers. (1 level of indirection) |S#|C#|GRADE pg ENROLL (unclustered C#) |32|8 | 89 |1 |25|6 | 76 |1 |32|6 | 62 |2 |25|8 | 86 |2 |38|6 | 98 |3 |32|7 | 91 |3 |17|5 | 96 |4 |25|7 | 68 |4 |17|8 | 95 |5 |C#| page Secondary_Index, opt3 on C# |5 | -->|4| |6 | -->|1|->|2|->|3| |7 | -->|3|->|4| |8 | -->|1|->|2|->|4| Option2: Use repeating groups of pointers (requires variable length pointer(s) |C#|page |5 | 4 |6 | 1,2,3 |7 | 3,4 |8 | 1,2,4 Secondary Index These indexes are the same as the previous except, - the file is need not be clustered on k - p points to the page or record containing k - every record must be indexed (dense) Option1: If there are multiple occurences of k, use multiple index entries for that k. S#|C#|GRADE ENROLL (unclustered C#) 32|8 | 89 |1 25|6 | 76 |1 32|6 | 62 |2 25|8 | 86 |2 38|6 | 98 |3 32|7 | 91 |3 17|5 | 96 |4 25|7 | 68 |4 17|8 | 95 |5 C#|pg Secondary_Index, Option1 on C# 5 | 4 6 | 1 6 | 2 6 | 3 7 | 3 7 | 4 8 | 1 8 | 2 8 | 4
Multi-level Index (made up of an index on an index) For any index, since it is a file clustered on the key, k, it can have a primary or clustering index on it. (constituting the second level of the multilevel index). _ STUDENT |S#|SNAME |LCODE |pg |17|BAID |NY2091|1 |25|CLAY |NJ5101|1 |32|THAISZ|NJ5102|2 |38|GOOD |FL6321|2 |57|BROWN |NY2092|3 |83|THOM |ND3450|3 |91|PARK |MN7334|4 |94|SIVA |OR1123|4 |S#|pg|pg(of index file) S#-index (nondense, primary) |17| 1|1 |32| 2|1 |57| 3|2 |91| 4|2 2nd_LEVEL (a second level, nondense index) |S#|pg| |17| 1| |57| 2|
Anchor records of each page Name, age, bonus Ashby, 25, 3000 22 Basu, 33, 4003 25 Bristow, 30, 2007 30 Ashby 33 Cass, 50, 5004 Cass Smith Daniels, 22, 6003 40 Jones, 40, 6003 44 44 Smith, 44, 3000 50 Tracy, 44, 5004 Sparse Index Dense Index on on Data File Name Age Index Classification (Contd.) • If there is at least one index entry per existing attribute value, then it is called dense, else sparse • Every sparse index must be clustered! Sparse indexes are smaller. • Tree-structured indexing techniques support both range searches (AKA inequality searches) and equality searches. • ISAM: (variation of multilevel clustering) static structure; • B+ tree: dynamic, adjust gracefully under insert and delete.
index entry K* K K* K K* K* K m 0 1 2 1 m 2 Non-leaf (inode Leaf Pages Overflow page Primary pages • Leaf pages contain data entries, <k,k*>. In inodes, k*=indirect ptr. 1 index entry per page of data file, of the form: <k,k*> sorted on the attrribute value, k. k* points to 1st page (possibly) containing k. Provides alternate entry points into the file – faster than binary search which has just one entry point. ISAM Index file may still be quite large. But we can apply the idea repeatedly!
Example ISAM Tree • Where each node can hold 2 (k,k*) entries • in any internal node or inode (non-leaf) add ptr for key_values < the first k-value Root 40,40* 20,20* 33,33* 51,51* 63,63* 46,46* 55,55* 10,10* 15,15* 20,20* 27,27* 33,33* 37,37* 40,40* 51,51* 97,97* 63,63*
48,48* 42,42* Index Pages Primary Leaf Pages 23,23* Overflow Pages Insert k=48 Insert k=23 Insert k=41 40,40* Insert k=42 20,20* 33,33* 51,51* 63,63* 46,46* 55,55* 10,10* 15,15* 20,20* 27,27* 33,33* 37,37* 40,40* 51,51* 97,97* 63,63* Need overflow page Need overflow page 41,41* Need overflow page
48,48* 42,42* 23,23* 40,40* Deleting 51 Deleting 42 Deleting 97 20,20* 33,33* 51,51* 63,63* 46,46* 55,55* 10,10* 15,15* 20,20* 27,27* 33,33* 37,37* 40,40* 51,51* 97,97* 63,63* 41,41* • Note that 51* appears in index levels, but not in leaf!
Index Entries (“Direct search set or index set”) Data Entries ("Sequence set") B+ Tree: The Most Widely Used Index • keeps tree height-balanced. • Minimum 50% occupancy (except for root). Each node contains m entries, where d m 2d. • d is called the degree or order of the index. • Supports equality and range-searches efficiently.
Example B+ Tree (d=2) • Search for 5 • Search for15 • Search begins at root, key comparisons direct it to a leaf. • Search for all data entries 24 Root 30 13 17 24 Leaves are doubly linked for fast sequential < search 39* 3* 5* 19* 20* 22* 24* 27* 38* 2* 7* 14* 16* 29* 33* 34* 5* 15 is not in the file!
Example B+ Tree (contd.) • Search for all data entries < 23 • (note, this is the reason for the double linkage). Root 30 13 17 24 39* 20* 3* 5* 19* 22* 24* 27* 38* 2* 7* 14* 16* 29* 33* 34*
Inserting a Data Entry into a B+ Tree • Find correct leaf L. • Put data entry in L. • If L has enough space, done! • Else, must splitL (into L and a new node L2) • Redistribute entries, copy up (promote)middle key. • middle value which was promoted and is now the anchor key for L2). • This can happen recursively (e.g., if there is no space for the promoted middle value in the inode to which it is promoted) • To split inode, redistribute entries evenly, but push up (promote)middle key. • So promote means Copy up at leaf; Move up at inode. • Splits “grow” tree • only a root split increases height. • Only tree growth possible: wider or 1 level taller at top.
Entry to be inserted in parent node. (Note that 17 is moved up and only appears once in the index. Contrast this with a leaf split.) 5 17 3* 5* 2* 7* 5 24 13 30 30 13 17 24 5 to be inserted in parent node. 39* 38* 3* 5* 19* 20* 22* 24* 27* 2* 7* 14* 16* 29* 33* 34* 2* 5* 3* 7* 8* (Note that 5 is s copied up and continues to appear in the new leaf node, L2, as anchor value.) Inserting 8* No room for 5, so split and move 17 up. No room for 8, so split. Observe how minimum occupancy is guaranteed in both leaf and index pg splits. • Note difference between copy-up (leaf)and move-up (inode)
Root 30 13 17 24 39* 38* 3* 5* 19* 20* 22* 24* 27* 2* 7* 14* 16* 29* 33* 34* Root 17 24 5 13 30 After Inserting 8* 39* 2* 3* 5* 7* 8* 19* 20* 22* 24* 27* 38* 29* 33* 34* 14* 16* B+ Tree Before Inserting 8* Note height_increase, balance and occupancy maintenance.
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, mergeL and a sibling. • Merge could propagate to root, and therefore decreasing height.
Root 17 17 Then Deleting 19*, 20* Root 24 5 13 30 27 5 13 30 39* 2* 3* 19* 20* 22* 24* 27* 5* 7* 8* 38* 29* 33* 34* 14* 16* 39* 2* 3* 5* 7* 8* 22* 24* 27* 29* 38* 33* 34* 14* 16* Example Tree After Inserting 8* • Deleting 19* is easy. • Deleting 20* is done with re-distribution of 24* (and revision of anchor value (from 24 to 27) in inode.
27 17 30 5 13 17 39* 2* 3* 27* 29* 5* 7* 8* 22* 38* 34* 33* 14* 16* 27 30 5 13 39* 2* 3* 5* 7* 8* 22* 24* 27* 29* 38* 34* 33* 14* 16* Root 5 13 17 30 3* 39* 2* 5* 7* 8* 22* 38* 27* 33* 34* 14* 16* 29* ... And Then Deleting 24* • Must merge. • Observe `toss’ of index entry, 27, now that inode is below min occupancy so merge it with its sibling • and index entry, 17 can be `pulled down’ (sibling merge, followed by pull-down)
Summary • Tree indexes are ideal for range-searches and equality searches. • ISAM is a static structure. • Only leaf pages modified; overflow pages needed. • Overflow chains can degrade performance unless size of data set and data distribution stay constant. • B+ tree is a dynamic structure. • Inserts/deletes leave tree height-balanced. • High fanout (F) means depth rarely more than 3 or 4. • Almost always better than maintaining a sorted file. • Typically, 67% occupancy on average. • Usually preferable to ISAM • adjusts to growth gracefully. • Most widely used index in database management systems because of its versatility. One of the most optimized components of a DBMS. • Caution! There is much variation in implementation