420 likes | 439 Views
Tree Indices Chapter 11. Job Interview Question. B + -Tree Index Files. Disadvantage of indexed-sequential files performance degrades as file grows, since many overflow blocks get created. Periodic reorganization of entire file is required. Advantage of B + -tree index files:
E N D
B+-Tree Index Files • Disadvantage of indexed-sequential files • performance degrades as file grows, since many overflow blocks get created. • Periodic reorganization of entire file is required. • Advantage of B+-treeindex 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 B+-tree indices are an alternative to indexed-sequential files.
B+-Tree Index Files (Cont.) 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/2 and n children (pointers). • n is given and is the same for all nodes. • n is called the order of the tree. • Some books refer to n/2 as the order of the tree. • 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.
B+-Tree Node Structure • Typical node • Ki are the search-key values • Pi 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 K1 <K2 <K3 <. . .<Kn–1 (Initially assume no duplicate keys, address duplicates later)
Leaf Nodes in B+-Trees Properties of a leaf node: • For i = 1, 2, . . ., n–1, pointer Pi points to a file record with search-key value Ki, • If Li, Lj are leaf nodes and i < j, Li’s search-key values are less than or equal to Lj’s search-key values • Pn points to next leaf node in search-key order
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 m pointers: • All the search-keys in the subtree to which P1 points are less than K1 • For 2 i n – 1, all the search-keys in the subtree to which Pi points have values greater than or equal to Ki–1 and less than Ki • All the search-keys in the subtree to which Pn points have values greater than or equal to Kn–1
Example of B+-tree • Leaf nodes must have between 3 and 5 values ((n–1)/2 and n –1, with n = 6). • Non-leaf nodes other than root must have between 3 and 6 children ((n/2 and n with n =6). • Root must have at least 2 children. B+-tree for instructor file (n = 6 pointers → n – 1 = 5 keys (values))
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 • Level below root has at least 2* n/2 values • Next level has at least 2* n/2 * n/2 values • .. etc. • If there are K search-key values in the file, the tree height is no more than logn/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 • Find record with search-key value V. • C=root • While C is not a leaf node { • Let i be least value s.t. V Ki. • If no such exists, set C = last non-null pointer in C • Else { if (V= Ki ) Set C = Pi +1 else set C = Pi} } • Let i be least value s.t. Ki = V • If there is such a value i, follow pointer Pito the desired record. • Else no record with search-key value k exists.
Queries on B+-Trees (Cont.) • If there are K search-key values in the file, the height of the tree is no more than logn/2(K). • 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). • With 1 million search key values and n = 100 • at most log50(1,000,000) = 4 nodes are accessed in a lookup. • 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 milliseconds
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 buffer pool: • Level 1 = 1 page = 8 Kbytes • Level 2 = 133 pages = 1 Mbyte • Level 3 = 17,689 pages = 133 MBytes
Updates on B+-Trees: Insertion • Find the leaf node in which the search-key value would appear • If the search-key value is already present in the leaf node • Add record to the file • If necessary add a pointer to the bucket. • If the search-key value is not present, then • add the record to the main file (and create a bucket if necessary) • If there is room in the leaf node, insert (key-value, pointer) pair in the leaf node • Otherwise, split the node (along with the new (key-value, pointer) entry) as discussed in the next slide.
Updates on B+-Trees: Insertion (Cont.) • Splitting a leaf node: • take the n (search-key value, pointer) pairs (including the one being inserted) 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 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. Result of splitting node containing Brandt, Califieri and Crick on inserting Adams Next step: insert entry with (Califieri,pointer-to-new-node) into parent
B+-Tree Insertion B+-Tree before and after insertion of “Adams”
B+-Tree Insertion • Insert “Lamport” B+-Tree before and after insertion of “Lamport”
Insertion in B+-Trees (Cont.) • Splitting a non-leaf node: when inserting (k,p) into an already full internal node N • Copy N to an in-memory area M with space for n+1 pointers and n keys • Insert (k,p) into M • Copy P1,K1, …, K n/2-1,P n/2 from M back into node N • Copy Pn/2+1,Kn/2+1,…,Kn,Pn+1 from M into newly allocated node N’ • Insert (Kn/2,N’) into parent N • Read pseudocode in book! Califieri Adams Brandt Califieri Crick Adams Brandt Crick
Example B+ Tree - Inserting 15* Root 30 24 13 17 39* 22* 24* 27* 38* 3* 5* 19* 20* 29* 33* 34* 2* 7* 14* 16* 16* 14* 15* Dose not violates the 50% rule
Example B+ Tree - Inserting 8* Root 30 24 13 17 39* 22* 24* 27* 38* 3* 5* 19* 20* 29* 33* 34* 2* 7* 14* 16* 3* 5* 8* 2* 7* Violate the 50% rule, split the leaf.
Example B+ Tree - Inserting 8* Violate the 50% rule, split the internal node. Root 7 30 24 13 17 39* 5* 22* 24* 27* 38* 3* 19* 20* 29* 33* 34* 2* 14* 16* 8* 7*
Example B+ Tree After Inserting 8* Root 17 24 5 13 30 39* 2* 3* 5* 7* 8* 19* 20* 22* 24* 27* 38* 29* 33* 34* 14* 16* • Notice that root was split, leading to increase in height. • In this example, we can avoid split by re-distributing entries; however, this is usually not done in practice.
Updates on B+-Trees: Deletion • Find the record to be deleted, and remove it • 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.
Updates on B+-Trees: Deletion • 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. • Rule of Thumb: • First attempt must be redistribution. It is cheaper! • Then, merge.
Example Tree (including 8*) Delete 19* and 20* ... Root 17 24 30 5 13 39* 2* 3* 19* 20* 22* 24* 27* 38* 5* 7* 8* 29* 33* 34* 14* 16*
Example Tree (including 8*) After 19* is Deleted. Delete 20 • Deleting 19* is easy. Root 17 24 30 5 13 39* 2* 3* 20* 22* 24* 27* 38* 5* 7* 8* 29* 33* 34* 14* 16*
Example Tree (including 8*) Delete 20* • Underflow! → Redistribute. Root 17 24 30 5 13 39* 2* 3* 24* 27* 38* 5* 7* 8* 29* 33* 34* 14* 16* 22* Violation of the 50% rule
Example Tree After (Inserting 8*, Then) Deleting 19* and 20* ... • Deleting 20* is done with re-distribution. Notice how the lowest key is copied up. Root 17 27 5 13 30 39* 2* 3* 5* 7* 8* 22* 24* 27* 29* 38* 33* 34* 14* 16*
... And Then Deleting 24* Root 17 27 5 13 30 39* 2* 3* 5* 7* 8* 22* 24* 27* 29* 38* 33* 34* 14* 16* • Underflow! • Can we do redistribution? • MERGE!
... And Then Deleting 24* • Must merge. • Observe `toss’ of index entry (on right), and `pull down’ of index entry (below). 30 39* 22* 27* 38* 29* 33* 34* Root 5 13 17 30 3* 39* 2* 5* 7* 8* 22* 38* 27* 33* 34* 14* 16* 29*
2* 3* 5* 7* 8* 39* 17* 18* 38* 20* 21* 22* 27* 29* 33* 34* 14* 16* Example of Non-leaf 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 22 30 17 20 5 13
After Re-distribution • Intuitively, entries are re-distributed by `pushingthrough’ the splitting entry in the parent node. • It suffices to re-distribute index entry with key 20. Root 20 30 5 13 22 17 2* 3* 5* 7* 8* 39* 17* 18* 38* 20* 21* 22* 27* 29* 33* 34* 14* 16*
After Re-distribution • Intuitively, entries are re-distributed by `pushingthrough’ the splitting entry in the parent node. • We’ve re-distributed 17 as well for illustration. Root 17 22 30 5 13 20 2* 3* 5* 7* 8* 39* 17* 18* 38* 20* 21* 22* 27* 29* 33* 34* 14* 16*
Another Examples of B+-Tree Deletion • Deleting “Srinivasan” causes merging of under-full leaves Violation of the 50% rule Underflow! → Redistribute Before and after deleting “Srinivasan” Cannot Redistribute → Merge
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 • 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 • Implemented as part of bulk-load utility by most database systems
3* 3* 6* 6* 9* 9* 10* 10* 11* 11* 12* 12* 13* 13* 23* 23* 31* 31* 36* 36* 38* 38* 41* 41* 44* 44* 4* 4* 20* 20* 22* 22* 35* 35* Bulk Loading of a B+ Tree: Alternative 2 Root Root Sorted pages of data entries; not yet in B+ tree 6 Sorted pages of data entries; not yet in B+ tree
3* 6* 9* 10* 11* 12* 13* 23* 31* 36* 38* 41* 44* 4* 20* 22* 35* Bulk Loading of a B+ Tree Root Sorted pages of data entries; not yet in B+ tree 6 10
Bulk Loading (Contd.) Root 10 20 • 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! Data entry pages 6 12 23 35 not yet in B+ tree 3* 6* 9* 10* 11* 12* 13* 23* 31* 36* 38* 41* 44* 4* 20* 22* 35* Root 20 10 Data entry pages 35 not yet in B+ tree 6 23 12 38 3* 6* 9* 10* 11* 12* 13* 23* 31* 36* 38* 41* 44* 4* 20* 22* 35*
Animation • https://www.cs.usfca.edu/~galles/visualization/BPlusTree.html
Indices on Multiple Keys • Composite search keys are search keys containing more than one attribute • E.g. (dept_name, salary) • Lexicographic ordering: (a1, a2) < (b1, b2) if either • a1 < b1, or • a1=b1 and a2 < b2
Indices on Multiple Attributes • With the where clausewhere dept_name = “Finance” andsalary = 80000the index on (dept_name, salary) can be used to fetch only records that satisfy both conditions. • Using separate indices is less efficient — we may fetch many records (or pointers) that satisfy only one of the conditions. • Can also efficiently handle where dept_name = “Finance” and salary < 80000 • But cannot efficiently handlewhere dept_name < “Finance” andbalance = 80000 • May fetch many records that satisfy the first but not the second condition Suppose we have an index on combined search-key (dept_name, salary).