Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to.

Similar presentations


Presentation on theme: "Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to."— Presentation transcript:

1 Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to 9.5 Glance at 9.6 and 9.8.2 Homework (won’t be graded, but TA’s will be happy to answer questions) 9.1 and 9.2 (with more emphasis on the insertion questions, less on deletion) For Tuesday glance at chapter 10. On Thursday Nov 7 th, there will be no traditional class. Instead, I will be in my office, and the two TAs will be in the TA Lab, to answer questions during the normal class hours. (We will not have prepared remarks, we will simply answer questions. Participation is optional, the usefulness of the experience depends on you coming in with meaningful questions).

2 Indexed Sequential Access Method Data Page 1 Data Page 2 Data Page 3 :: Data Page N-1 Data Page N If our large database is sorted, we can speed up search by doing binary search on the entire database. However this means we must do log(N) disk accesses… The idea of ISAM is to do a faster, approximate binary search in main memory, and use this information to do fewer disk accesses (usually only one).

3 Indexed Sequential Access Method K P An index entry is a pair, where key is the value of the first key on the page, and pointer, points to the page. Example Data Page 7 Maggieq4 Manjulap3 Marged5 Montyf4 Maggie page 7

4 Indexed Sequential Access Method An index file is a concatenation of index entries. Together with one extra pointer at the beginning. Example K1 P1K2 P2K3 P3 K3 P0 Maggie page 2Waylon page 3Maggie page 1

5 Indexed Sequential Access Method 7 P4 K3 P3 Every record pointed to by this pointer has a value greater that or equal to 7 Every record pointed to by this pointer has a value less that 7 Lets look at an index file (this one is the smallest possible example)

6 Indexed Sequential Access Method Data Page 1 Data Page 2 Data Page 3 :: Data Page N-1 Data Page N 125 16 19 :: p p p p p p Index File Data Files Instead of doing binary search on the data files, we can do binary search on the index, to find the largest value, which is equal to or less than the search key. We then use the pointer to go to disk to retrieve the relevant block from disk. Example: we are searching for 8, we do a binary search to find 5, we retrieve page 2, and search it to find a match (if there is one). Find the largest entry less than the key, follow right child. (find 20) Find the smallest entry greater than or equal to the key, follow left child. (find 2)

7 Indexed Sequential Access Method Data Page 1 Data Page 2 Data Page 3 :: Data Page N-1 Data Page N 52 34 77 :: p p p p p p Index File Data Files How big should the index file be? How about more pointers per page? We could have two pointers to each page (on average, or exactly). This does not help, because we have to retrieve a block at a time.

8 Indexed Sequential Access Method How big should the index file be? How about less pointers per page? We could have a pointer for each two pages (on average, or exactly). This might help, because it makes the index smaller. We can do a little trick of adding “sideways” pointers. Actually, these “sideways” pointer can be useful for another reason, they can be helpful for range queries. However, to few pointers leads to chaining…. Data Page 1 Data Page 2 Data Page 3 :: Data Page N-1 Data Page N 125 16 19 :: p p p p p p Index File Data Files

9 Indexed Sequential Access Method Data Page 1 Data Page 2 Data Page 3 :: Data Page N-1 Data Page N 125 16 19 :: p p p p p p Index File Data Files We have seen that too small or too large an index (in other words too few or too many pointers) can be a problem. But suppose the index does not fit in main memory? The key observation is that the index itself is a sort of database, so lets build an index on the index! 21 p

10 30135 1814 163543 Data Page 1 Data Page 2 Data Page 4 Data Page 3 Data Page 5 Data Page 6 Data Page 7 Data Page 9 Data Page 8 Data Page 10 An index of indexes is a tree! We can use this structure to do fast equality search. Find 15, 0 What about range search? It looks like we have solved our fast indexing problem, but there is a catch, what happens if we have a deletion, an insertion? Tree Based Indexing 17 Define root internal node leaf

11 30135 1814 163543 Data Page 1 Data Page 2 Data Page 4 Data Page 3 Data Page 5 Data Page 6 Data Page 7 Data Page 9 Data Page 8 Data Page 10 What happens if we have a deletion? (not much) What happens if we have an insertion? (trouble!) Solution: Overflow Buckets If we have enough overflow buckets, we might as well have no index at all Tree Based Indexing Over- flow 1 Suppose we add a bunch of 15 year olds to the database… 17

12 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: automatically reorganizes itself with small, local, changes, in the face of insertions and deletions. Reorganization of entire file is not required to maintain performance. Disadvantage of B + -trees: extra insertion and deletion overhead, space overhead. Advantages of B + -trees outweigh disadvantages, and they are used extensively. B + -tree indices are an alternative to indexed-sequential files.

13 B + -Tree Index Files (Cont.) All paths from root to leaf are of the same length Let d be the order of the tree (user specified), and let d = n/2 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 rooted tree satisfying the following properties:

14 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 (for leaf nodes). –The fan-out is the number of pointers The search-keys in a node are ordered K 1 < K 2 < K 3 <... < K n–1

15 Leaf Nodes in B + -Trees For i = 1, 2,..., n–1, pointer P i points to a file record with search- key value K i. 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:

16 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 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 m–1 5 60.0000 or greater 60 59.99.. or less 4.9999.. or less

17 Example of a B + -tree B + -tree for account file (n = 3) Leaf nodes must have between 1 and 2 values (  (n–1)/2  and n –1, with n = 3). Non-leaf nodes other than root must have between 2 and 3 children (  (n/2  and n with n =3). Root must have at least 2 children.

18 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)

19 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. (and a large base logarithm at that) Insertions and deletions to the main file can be handled efficiently, as the index can be restructured in logarithmic time (as we shall see).

20 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 exists, assume it is K j. Then follow P i to the child node 3.Otherwise k  K m–1, where there are m pointers in the node. Then follow P m to the child node. 2.If the node reached by following the pointer above is not a leaf node, repeat the above procedure on the node, and follow the corresponding pointer. 3.Eventually reach a leaf node. If for some i, key K i = k follow pointer P i to the desired record. Else no record with search-key value k exists.

21 2*3* Root 17 30 14*16* 33*34* 38* 39* 135 7*5*8*22*24* 27 27*29* Entries <= 17 Entries > 17 What is the difference between 5 and 5* ? How come 30 is in the index, but there is no 30* in the file? Find 28*, Find 0*, Find all records > 25 and < 31 Queries on B + -Trees

22 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) . 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 log 50 (1,000,000) = 4 nodes are accessed in a lookup. Contrast this with a balanced binary free 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!

23 Updates on B + -Trees: Insertion Find the leaf node in which the search-key value would appear If the search-key value is already there in the leaf node, record is added to file and if necessary a pointer is inserted into the bucket. If the search-key value is not there, then add the record to the main file and create a bucket if necessary. Then: –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.

24 2*3* 19*20* 1713 16*14*24*27*40*41* 2430 5*7* 22* 28*45*77* Updates on B + -Trees: Insertion 2*3* 19*20* 1713 16*14*24*27*40*41* 2430 5*7* 22* 28*45*77* Insert 23 23* This is the easy case!

25 2*3* 19*20* 1713 16*14*24*27*40*41* 2430 5*7* 22* 28*45*77* Updates on B + -Trees: Insertion Insert 8 2*3* 19*20* 1713 16*14*24*27*40*41* 2430 22* 28*45*77*5*7*8* 5 Because the insertion will cause overfill, we split the leaf node into two nodes, we split the data into two nodes (and distribute the data evenly between them). “5” is special, since it discriminates between the two new siblings, so it is copied up. We now need to insert 5 into the parent node…

26 Updates on B + -Trees: Insertion 2*3* 19*20* 1713 16*14*24*27*40*41* 2430 22* 28*45*77*5*7*8* 5 2*3* 19*20* 3024 16*14*24*27*40*41* 22* 28*45*77*5*7*8* 135 17 We now need to insert 5 into the parent node… Because the insertion will cause overfill, we split the node into two nodes, we split the data into two nodes. “17” is special, since it discriminates between the two new siblings, so it is pushed up.

27 Updates on B + -Trees: Insertion 2*3* 19*20* 3024 16*14*24*27*40*41* 22* 28*45*77*5*7*8* 135 17 2*3* 19*20* 3024 16*14*24*27*40*41* 22* 28*45*77*5*7*8* 135 17 The insertion of 8 has increased the height of the tree by one (this is rare).

28 Updates on B + -Trees: Insertion (Cont.) Splitting a 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. The 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 Brighton and Downtown on inserting Clearview

29 Updates on B + -Trees: Insertion (Cont.) B + -Tree before and after insertion of “Clearview”

30 Updates on B + -Trees: Deletion Find the record to be deleted, and remove it from the main file and from the bucket (if present) 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.

31 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 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.

32 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”

33 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

34 Example of B + -tree Deletion (Cont.) Parent of leaf containing Perryridge became underfull, and borrowed a pointer from its left sibling Search-key value in the parent’s parent changes as a result Before and after deletion of “Perryridge” from earlier example

35 B + -Tree File Optimization Good space utilization important since 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 2*3* 19*20* 1713 16*14*24*27*40*41* 2430 5*7* 21* 28*45*77* 22* Insert 22

36 Bulk Loading

37 Indices in SQL CREATE INDEX gpa_ranking ON Students WITH STRUCTURE = BTREE KEY = gpa


Download ppt "Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to."

Similar presentations


Ads by Google