Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC-310 Database Systems

Similar presentations


Presentation on theme: "CPSC-310 Database Systems"— Presentation transcript:

1 CPSC-310 Database Systems
Professor Jianer Chen Room 315C HRBB Lecture #19

2 B+Trees Support fast search Support range search
Support dynamic changes B+tree node of order n: p1 k1 p2 k2 …… pn kn pn+1 Notes #7

3 Search and Range-search in B+tree
p1 k1 p2 k2 …… pn kn pn+1 A tree node Search(ptr, k); \\ search a record of key value k in the subtree rooted at ptr \\ assume the B+tree is a dense index of order n Case 1. ptr is a leaf \\ pn+1 is the sequence pointer IF (k = ki) for a key ki in *ptr THEN return(pi); ELSE return(Null); Case 2. ptr is not a leaf find a key ki in *ptr such that ki-1 ≤ k < ki; return(Search(pi, k)); Range-Search(ptr, k1, k2) Call Search(ptr, k1); Follow the sequence pointers until the search key value is larger than k2. Notes #7

4 Insert into B+tree Basic idea:
Find the leaf L where the record r should be inserted; If L has further room, then insert r into L, and return; If L is full, spilt L plus r into two leaves (each is about half full): this causes an additional child for the parent P of L, thus we need to add a child to P; If P is already full, then we have to split P and add an additional child to P’s parent … (recursively) Notes #7

5 Insert into B+tree Simple case (space available for new child)
Leaf overflow Non-leaf overflow New root Notes #7

6 Insert into B+tree Simple case (space available for new child)
Leaf overflow Non-leaf overflow New root Notes #7

7 New root: Insert key 45 (order n = 3)
Notes #7

8 New root: Insert key 45 (order n = 3)
10 20 30 45 1 2 3 10 12 20 25 30 32 40 Notes #7

9 New root: Insert key 45 (order n = 3)
10 20 30 1 2 3 10 12 20 25 30 32 40 45 30 32 40 45 Notes #7

10 New root: Insert key 45 (order n = 3)
10 20 30 no room 1 2 3 10 12 20 25 30 32 40 45 30 32 40 45 Notes #7

11 New root: Insert key 45 (order n = 3)
10 20 30 10 20 40 40 30 1 2 3 10 12 20 25 30 32 40 45 30 32 40 45 Notes #7

12 New root: Insert key 45 (order n = 3)
30 10 20 30 10 20 40 40 1 2 3 10 12 20 25 30 32 40 45 30 32 40 45 Notes #7

13 New root: Insert key 45 (order n = 3)
30 10 20 40 1 2 3 10 12 20 25 30 32 40 45 Notes #7

14 Pseudo Code for Insertion in B+tree
Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). Notes #7

15 Pseudo Code for Insertion in B+tree
insert in a leaf Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). Notes #7

16 Pseudo Code for Insertion in B+tree
insert in a leaf Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). no overflow Notes #7

17 Pseudo Code for Insertion in B+tree
insert in a leaf Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). no overflow with overflow Notes #7

18 Pseudo Code for Insertion in B+tree
insert in a leaf Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). no overflow new child to parent with overflow Notes #7

19 Pseudo Code for Insertion in B+tree
Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). insert in a nonleaf Notes #7

20 Pseudo Code for Insertion in B+tree
Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). insert in a nonleaf recursion Notes #7

21 Pseudo Code for Insertion in B+tree
Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). insert in a nonleaf recursion no insert Notes #7

22 Pseudo Code for Insertion in B+tree
Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). insert in a nonleaf recursion no overflow no insert Notes #7

23 Pseudo Code for Insertion in B+tree
Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). insert in a nonleaf recursion no overflow with overflow no insert Notes #7

24 Pseudo Code for Insertion in B+tree
Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). insert in a nonleaf recursion no overflow with overflow no insert new child to parent Notes #7

25 Pseudo Code for Insertion in B+tree
Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). new root Notes #7

26 Delete in B+tree Basic idea: Notes #7

27 Delete in B+tree Basic idea:
Find the leaf L where the record r should be deleted; Notes #7

28 Delete in B+tree Basic idea:
Find the leaf L where the record r should be deleted; If L remains at least half-full after deleting r, then delete r, and return; Notes #7

29 Delete in B+tree Basic idea:
Find the leaf L where the record r should be deleted; If L remains at least half-full after deleting r, then delete r, and return; Else consider the sibling L’ of L; Notes #7

30 Delete in B+tree Basic idea:
Find the leaf L where the record r should be deleted; If L remains at least half-full after deleting r, then delete r, and return; Else consider the sibling L’ of L; If L’ is more than half-full, then move a record from L’ to L, and return; Notes #7

31 Delete in B+tree Basic idea:
Find the leaf L where the record r should be deleted; If L remains at least half-full after deleting r, then delete r, and return; Else consider the sibling L’ of L; If L’ is more than half-full, then move a record from L’ to L, and return; Else combine L and L’ into a single leaf; Notes #7

32 Delete in B+tree Basic idea:
Find the leaf L where the record r should be deleted; If L remains at least half-full after deleting r, then delete r, and return; Else consider the sibling L’ of L; If L’ is more than half-full, then move a record from L’ to L, and return; Else combine L and L’ into a single leaf; But now you need to consider the case of deleting a child from L’s parent … (recursively) Notes #7

33 Delete in B+tree Simple case: the node remains at least half-full after deletion. Re-distribute keys among siblings Coalesce with a sibling and delete a pointer from its father Notes #7

34 Delete in B+tree Simple case: the node remains at least half-full after deletion. Re-distribute keys among siblings Coalesce with a sibling and delete a pointer from its father Notes #7

35 Simple case: Delete key 35
order n=3 Notes #7

36 Simple case: Delete key 35
order n=3 Delete(prt, 35) 105 10 40 3 5 8 10 20 35 40 50 Notes #7

37 Simple case: Delete key 35
order n=3 Delete(prt, 35) 105 10 40 3 5 8 10 20 35 40 50 Delete 35 Notes #7

38 After deletion, the node still keeps at least half-full
Simple case: Delete key 35 order n=3 Delete(prt, 35) 105 10 40 3 5 8 10 20 35 40 50 After deletion, the node still keeps at least half-full Notes #7

39 Simple case: Delete key 35
order n=3 105 10 40 3 5 8 10 20 40 50 Notes #7

40 Delete in B+tree Simple case: the node remains at least half-full after deletion. Re-distribute keys among siblings Coalesce with a sibling and delete a pointer from its father Notes #7


Download ppt "CPSC-310 Database Systems"

Similar presentations


Ads by Google