1、,Tree-Structured Indexes,Chapter 10,Introduction,As for any index, 3 alternatives for data entries k*:Data record with key value kChoice is orthogonal to the indexing technique used to locate data entries k*: Hash Tree,Introduction,Tree-structured indexing techniques support both range searches and
2、equality searches.,Motivation for Tree Index,Range search : Find all students with gpa 3.0Given Sorted fileUSE: do binary search to find first such student, then scan to find others.Cost of binary search can be quite high.,Motivation for Tree Index,Range search : Find all students with gpa 3.0 Simpl
3、e idea: Create an index file.,Can do binary search on (smaller) index file!,Page 1,Page 2,Page N,Page 3,Data File,k2,kN,k1,Index File,Motivation for Tree Index,Range search : Find all students with gpa 3.0 Sorted file, do binary search to find first such student, then scan to find others. Cost of bi
4、nary search can be quite high.Simple idea: Create an index file.,Can do binary search on (smaller) index file!,Page 1,Page 2,Page N,Page 3,Data File,k2,kN,k1,Index File,Alternative Tree Index Structures,ISAM (Indexed Sequential Access Method): static structure.B+ tree: dynamic structure. Adjust grac
5、efully under inserts and deletes.,ISAM Index,Page 1,Page 2,Page N,Page 3,Data File,k2,kN,k1,Index File,P,0,K,1,P,1,K,2,P,2,K,m,P,m,index entry,ISAM,Index file may still be quite large. But we can apply the idea repeatedly!,Only leaf pages contain data entries.,Non-leaf,Pages,Pages,Primary pages,Leaf
6、,Example ISAM Tree,Each node can hold 2 entries. No need for next-leaf-page pointers. (?),Inserting 23*, 48*, 41*, 42* .,After Inserting 23*, 48*, 41*.,10*,15*,20*,27*,33*,37*,40*,46*,51*,55*,63*,97*,20,33,51,63,40,Root,23*,48*,41*,Overflow,Pages,Leaf,Index,Pages,Pages,Primary,After Inserting 23*, 4
7、8*, 41*, 42* .,10*,15*,20*,27*,33*,37*,40*,46*,51*,55*,63*,97*,20,33,51,63,40,Root,23*,48*,41*,42*,Overflow,Pages,Leaf,Index,Pages,Pages,Primary,Now Lets Delete: 42*, 51*, 97*, ,10*,15*,20*,27*,33*,37*,40*,46*,51*,55*,63*,97*,20,33,51,63,40,Root,23*,48*,41*,42*,Overflow,Pages,Leaf,Index,Pages,Pages,
8、Primary,. After Deleting 42*, 51*, 97*,Note that 51* appears in index levels, but not in leaf!Index still is “fully balanced”, but with many empty slots.,10*,15*,20*,27*,33*,37*,40*,46*,55*,63*,20,33,51,63,40,Root,23*,48*,41*,Comments on ISAM,Data Pages,Index Pages,Overflow pages,Comments on ISAM,Fi
9、le creation: Leaf (data) pages allocated sequentially sorted by search key index pages allocated space for overflow pages later createdIndex entries: ; they direct search for data entries, which are in leaf pages or even in overflow pages.,Data Pages,Index Pages,Overflow pages,Comments on ISAM,Searc
10、h: Start at root; use key comparisons to go to leaf. Cost log F N ; F = # entries/index pg, N = # leaf pgs Insert: Find leaf that data entry belongs to, and put it there. Delete: Find and remove from leaf; if empty overflow page, de-allocate. If empty primary page, leave it.,Data Pages,Index Pages,O
11、verflow pages,Comments on ISAM : Pros? Cons?,Static tree structure: inserts/deletes affect only leaf pages. Long overflow chains may appear. May affect retrieve performance. Keep 20% free in leaf page. Problem might still appear!+ Concurrent access.index page is not locked since it is never changed.
12、? What to do instead ?,B+ Tree: Most Widely Used Index,Guarantee Search/Insert/delete at log F N cost with F = fanout, N = # leaf pages Keep tree height-balanced. Supports equality and range-searches efficiently.,Example B+ Tree,Search begins at root, and key comparisons direct it to a leaf (as in I
13、SAM). Search for 5*; 15*, for all data entries = 24*.,leaf nodes form a sequence to answer range query,Root,17,24,30,2*,3*,5*,7*,14*,16*,19*,20*,22*,24*,27*,29*,33*,34*,38*,39*,13,B+ Tree Concepts,Insert/delete at log F N cost; keep tree height-balanced. Each node contains d = m = 2d entries. The pa
14、rameter d is called order of tree. Minimum 50% occupancy or fill-factor (except for root).,B+ Trees in Practice,Typical order: 100. Typical fill-factor: 67%. average fanout = 133 Typical capacities: Height 4: 1334 = 312,900,700 records Height 3: 1333 = 2,352,637 records Can often hold top levels in
15、buffer pool: Level 1 = 1 page = 8 Kbytes Level 2 = 133 pages = 1 Mbyte Level 3 = 1332 = 17,689 pages = 133 MBytes,Inserting Data Entry into 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
16、 up middle key. Insert index entry pointing to L2 into parent of L.This can happen recursively To split index node, redistribute entries evenly, and push up middle key. (Contrast with leaf splits.),Inserting Data Entry into B+ Tree,Splits “grow” the tree in width. A root split increases height of tr
17、ee. Tree growth: gets wider or one level taller at top.,Root,17,24,30,2*,3*,5*,7*,14*,16*,19*,20*,22*,24*,27*,29*,33*,34*,38*,39*,13,Inserting 8* into Example B+ Tree,Root,17,24,30,2*,3*,5*,7*,14*,16*,19*,20*,22*,24*,27*,29*,33*,34*,38*,39*,13,Inserting 8* into Example B+ Tree,2*,3*,5*,7*,8*,5,(Note
18、 that 5 is,continues to appear in the leaf.),s copied up and,Root,17,24,30,2*,3*,5*,7*,14*,16*,19*,20*,22*,24*,27*,29*,33*,34*,38*,39*,13,Inserting 8* into Example B+ Tree,2*,3*,5*,7*,8*,5,5,24,30,17,13,Entry to be inserted in parent node.,(Note that 17 is pushed up and only once Appears in the inde
19、x.),Inserting 8* into Example B+ Tree,Observe how minimum occupancy is guaranteed in both leaf and index pg splits. Note difference between copy-up and push-up; be sure you understand the reasons for this.,2*,3*,5*,7*,8*,5,Entry to be inserted in parent node.,(Note that 5 is,continues to appear in t
20、he leaf.),s copied up and,appears once in the index. Contrast,Inserting 8* into Example B+ Tree,Observe how minimum occupancy is guaranteed in both leaf and index pg splits.Note difference between copy-up and push-up; be sure you understand the reasons for this.,2*,3*,5*,7*,8*,5,Entry to be inserted
21、 in parent node.,(Note that 5 is,continues to appear in the leaf.),s copied up and,appears once in the index. Contrast,Example B+ Tree After Inserting 8*,Notice that root was split, leading to increase in height.,2*,3*,Root,17,24,30,14*,16*,19*,20*,22*,24*,27*,29*,33*,34*,38*,39*,13,5,7*,5*,8*,Examp
22、le B+ Tree After Inserting 8*,In this example, we could avoid split by re-distributing entries; however, this is usually not done in practice.,2*,3*,Root,17,24,30,14*,16*,19*,20*,22*,24*,27*,29*,33*,34*,38*,39*,13,5,7*,5*,8*,Root,17,24,30,2*,3*,5*,7*,14*,16*,19*,20*,22*,24*,27*,29*,33*,34*,38*,39*,1
23、3,Inserting 8* into Example B+ Tree using Redistribution,Avoid split by re-distributing entries :,Checking sibling to decide whether redistribution is possible. Usually redistribution in leaf, not in non leaf nodes.,Example B+ Tree After Inserting 8* using Redistribution,Root,17,24,30,2*,3*,5*,7*,14
24、*,16*,19*,20*,22*,24*,27*,29*,33*,34*,38*,39*,8,8*,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). I
25、f 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.,Now Try to Delete 19* and then 20*,2*,3*,Root,17,24,30,14*,16*,19*,20*,22*,24*,27*,29*,33*,34*,38*,39*,13,5,7*,5*,8*,Now T
26、ry to Delete 19* ,2*,3*,Root,17,24,30,14*,16*,19*,20*,22*,24*,27*,29*,33*,34*,38*,39*,13,5,7*,5*,8*,Deleting 19* is easy.,Now Delete 20* ,2*,3*,Root,17,24,30,14*,16*,20*,22*,24*,27*,29*,33*,34*,38*,39*,13,5,7*,5*,8*,Now Delete 20* ,2*,3*,Root,17,24,30,14*,16*,20*,22*,24*,27*,29*,33*,34*,38*,39*,13,5
27、,7*,5*,8*,Deleting 20* can be done with re-distribution.,We have deleted 20* .,Deleting 20* done with re-distribution. Notice how middle key is copied up.,2*,3*,Root,17,30,14*,16*,33*,34*,38*,39*,13,5,7*,5*,8*,22*,24*,27,27*,29*,Now Deleting 24* .,Can re-distribution among sibling work again?,2*,3*,
28、Root,17,30,14*,16*,33*,34*,38*,39*,13,5,7*,5*,8*,22*,24*,27,27*,29*,Now Deleting 24* . ?,If not, so lets try merging of siblings ,2*,3*,Root,17,30,14*,16*,33*,34*,38*,39*,13,5,7*,5*,8*,22*,24*,27,27*,29*,Now Deleting 24* via Merging.,2*,3*,Root,17,30,14*,16*,33*,34*,38*,39*,13,5,7*,5*,8*,22*,27,27*,
29、29*,Who link to who ?,22*,27*,29*,Now Deleting 24* via Merging.,2*,3*,Root,17,30,14*,16*,33*,34*,38*,39*,13,5,7*,5*,8*,22*,24*,27,27*,29*,Observe toss of index entry (on right),30,22*,27*,29*,33*,34*,38*,39*,Now Deleting 24* via Merging.,2*,3*,Root,17,14*,16*,13,5,7*,5*,8*,30,22*,27*,29*,33*,34*,38*
30、,39*,Are we done ?,Now Deleting 24* via Merging.,2*,3*,Root,17,14*,16*,13,5,7*,5*,8*,30,22*,27*,29*,33*,34*,38*,39*,First try re-distributingIf not possible, try merging.,Now Deleting 24* via Merging.,2*,3*,Root,17,14*,16*,13,5,7*,5*,8*,30,22*,27*,29*,33*,34*,38*,39*,Observe pull downof index entry.
31、,2*,3*,7*,14*,16*,22*,27*,29*,33*,34*,38*,39*,5*,8*,Root,30,13,5,17,. And Then Deleting 24*,Must merge. Observe toss of index entry (on right), and pull down of index entry (below).,30,22*,27*,29*,33*,34*,38*,39*,2*,3*,7*,14*,16*,22*,27*,29*,33*,34*,38*,39*,5*,8*,Root,30,13,5,17,Example of Non-leaf
32、Re-distribution,Tree is shown below during deletion of 24*. In contrast to previous example, can re-distribute entry from left child of root to right child.,Root,13,5,17,20,22,30,Example of Non-leaf Re-distribution,Tree is shown below during deletion of 24*.,Root,13,5,17,20,22,30,Miscellaneous about
33、 B-Trees,Duplicate keys in B+ Tree,Alternative 1: overflow page Alternative 2: contain duplicates in leaf page and fetch using leaf sequence pointer not efficient in delete! (check duplicate entries) Solution: , use rid as part of searching key. Index includes both key and rid fields. Alternative 3:
34、 , similar with 2.,Prefix Key Compression,Important to increase fan-out. Key values in index entries only direct traffic; can often compress them.Insert/delete must be suitably modified.,Prefix Key Compression of Key Values,Adjacent index entries with search key values:Dannon Yogurt, David Smith and
35、 Devarakonda Murthy, We can abbreviate David Smith to Dav. Is this correct? Not quite! What if there is a data entry Davey Jones? So can only compress David Smith to DaviIn general, while compressing, leave each index entry greater than every key value (in any subtree) to its left.,Loading of a B+ T
36、ree,If we have a large collection of records, and want to create a B+ tree on some field, how do we load these records into index?,Bulk Loading of a B+ Tree,Repeatedly inserting records is very slow. Bulk Loading can be done more efficiently. Step 1: Initialization: Sort all data entries, Insert poi
37、nter to first (leaf) page in a new (root) page.,Sorted pages of data entries; not yet in B+ tree,Root,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.,3*,4*,6
38、*,9*,10*,11*,12*,13*,20*,22*,23*,31*,35*,36*,38*,41*,44*,Root,Data entry pages,not yet in B+ tree,35,23,12,6,10,20,3*,4*,6*,9*,10*,11*,12*,13*,20*,22*,23*,31*,35*,36*,38*,41*,44*,6,Root,10,12,23,20,35,38,not yet in B+ tree,Data entry pages,Summary of Bulk Loading,Option 1: multiple inserts. Slow. Do
39、es 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) Can control “fill factor” on pages.,A Note on Order,Order (d) concept replaced by physical space criterion in practice (at
40、 least half-full). Idea: Merge when more than half of space in node is unused. Why? Index pages can typically hold many more entries than leaf pages. Variable sized records and search keys mean different nodes will contain different numbers of entries. Even with fixed length fields, multiple records
41、 with the same search key value (duplicates) can lead to variable-sized data entries (if we use Alternative (3).,Summary,Tree-structured indexes are ideal for range-searches, also good for 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; log F N cost. High fanout (F) means depth rarely more than 3 or 4. Almost always better than maintaining a sorted file.,
copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1