CPSC-310 Database Systems

Slides:



Advertisements
Similar presentations
Dr. Kalpakis CMSC 661, Principles of Database Systems Index Structures [13]
Advertisements

CPSC-608 Database Systems Fall 2010 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #7.
Indexes. Primary Indexes Dense Indexes Pointer to every record of a sequential file, (ordered by search key). Can make sense because records may be much.
COMP 451/651 Indexes Chapter 1.
CS4432: Database Systems II
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #10.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #7.
B + Trees Dale-Marie Wilson, Ph.D.. B + Trees Search Tree Used to guide search for a record, given the value of one of its fields Two types of Nodes Internal.
B-Tree B-Tree is an m-way search tree with the following properties:
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Part B Part A:  Index Definition in SQL  Ordered Indices  Index Sequential.
B + -Trees (Part 1) Lecture 20 COMP171 Fall 2006.
1 Database indices Database Systems manage very large amounts of data. –Examples: student database for NWU Social Security database To facilitate queries,
B + -Trees (Part 1). Motivation AVL tree with N nodes is an excellent data structure for searching, indexing, etc. –The Big-Oh analysis shows most operations.
B+ - Tree & B - Tree By Phi Thong Ho.
1 Indexing Structures for Files. 2 Basic Concepts  Indexing mechanisms used to speed up access to desired data without having to scan entire.
1 Database Tuning Rasmus Pagh and S. Srinivasa Rao IT University of Copenhagen Spring 2007 February 8, 2007 Tree Indexes Lecture based on [RG, Chapter.
Homework #3 Due Thursday, April 17 Problems: –Chapter 11: 11.6, –Chapter 12: 12.1, 12.2, 12.3, 12.4, 12.5, 12.7.
CS 255: Database System Principles slides: B-trees
1 CS 728 Advanced Database Systems Chapter 17 Database File Indexing Techniques, B- Trees, and B + -Trees.
CS4432: Database Systems II
Indexing. Goals: Store large files Support multiple search keys Support efficient insert, delete, and range queries.
Index Structures for Files Indexes speed up the retrieval of records under certain search conditions Indexes called secondary access paths do not affect.
B+ Trees COMP
Database Management 8. course. Query types Equality query – Each field has to be equal to a constant Range query – Not all the fields have to be equal.
1 B Trees - Motivation Recall our discussion on AVL-trees –The maximum height of an AVL-tree with n-nodes is log 2 (n) since the branching factor (degree,
CSE373: Data Structures & Algorithms Lecture 15: B-Trees Linda Shapiro Winter 2015.
COSC 2007 Data Structures II Chapter 15 External Methods.
12.1 Chapter 12: Indexing and Hashing Spring 2009 Sections , , Problems , 12.7, 12.8, 12.13, 12.15,
B + -Trees. Motivation An AVL tree with N nodes is an excellent data structure for searching, indexing, etc. The Big-Oh analysis shows that most operations.
DBMS 2001Notes 4.1: B-Trees1 Principles of Database Management Systems 4.1: B-Trees Pekka Kilpeläinen (after Stanford CS245 slide originals by Hector Garcia-Molina,
Starting at Binary Trees
Indexing and hashing Azita Keshmiri CS 157B. Basic concept An index for a file in a database system works the same way as the index in text book. For.
CompSci 100E 39.1 Memory Model  For this course: Assume Uniform Access Time  All elements in an array accessible with same time cost  Reality is somewhat.
CS 405G: Introduction to Database Systems 22 Index Chen Qian University of Kentucky.
Indexes. Primary Indexes Dense Indexes Pointer to every record of a sequential file, (ordered by search key). Can make sense because records may be much.
B+ tree & B tree Extracted from Garcia Molina
1 Chapter 12: Indexing and Hashing Indexing Indexing Basic Concepts Basic Concepts Ordered Indices Ordered Indices B+-Tree Index Files B+-Tree Index Files.
1 CSCE 520 Test 2 Info Indexing Modified from slides of Hector Garcia-Molina and Jeff Ullman.
1 Query Processing Part 3: B+Trees. 2 Dense and Sparse Indexes Advantage: - Simple - Index is sequential file good for scans Disadvantage: - Insertions.
CS 405G: Introduction to Database Systems 12. Index.
1 Ullman et al. : Database System Principles Notes 4: Indexing.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: TEL 3049.
CS422 Principles of Database Systems Indexes Chengyu Sun California State University, Los Angeles.
CS422 Principles of Database Systems Indexes
Multiway Search Trees Data may not fit into main memory
CS 728 Advanced Database Systems Chapter 18
Azita Keshmiri CS 157B Ch 12 indexing and hashing
Indexing ? Why ? Need to locate the actual records on disk without having to read the entire table into memory.
Extra: B+ Trees CS1: Java Programming Colorado State University
B+-Trees.
B+-Trees.
CSE373: Data Structures & Algorithms Lecture 15: B-Trees
CPSC-629 Analysis of Algorithms
Chapter Trees and B-Trees
Chapter Trees and B-Trees
CPSC-310 Database Systems
Lecture 26 Multiway Search Trees Chapter 11 of textbook
CPSC-310 Database Systems
(Slides by Hector Garcia-Molina,
CPSC-310 Database Systems
Indexing and Hashing Basic Concepts Ordered Indices
Random inserting into a B+ Tree
B+Tree Example n=3 Root
Indexing and Hashing B.Ramamurthy Chapter 11 2/5/2019 B.Ramamurthy.
Chapter 11 Indexing And Hashing (1)
Database Design and Programming
CPSC-608 Database Systems
CPSC-608 Database Systems
CPSC-608 Database Systems
Index Structures Chapter 13 of GUW September 16, 2019
Presentation transcript:

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

B+Trees Support fast search Support range search Support dynamic changes Could be either dense or sparse * dense: pointers to all records * sparse: one pointer per block Notes #7

B+Trees A B+tree node of order n How big is n? where ph are pointers (disk addresses) and kh are search-keys (values of the attributes in the index) How big is n? Basically we want each B+tree node to fit in a disk block so that a B+tree node can be read/written by a single disk I/O. Typically, n ~ 100-200. p1 k1 p2 k2 …… pn kn pn+1 Notes #7

B+Tree Example order n = 3 root 100 30 120 150 180 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 Notes #7

Sample non-leaf order n = 3 57 81 95 To keys k < 57 To keys 57 k<81 To keys 81 k<95 To keys k  95 Notes #7

Sample leaf node order n = 3 From non-leaf node To next leaf in sequence 57 81 95 To record with key 57 To record with key 81 To record with key 95 Notes #7

A B+Tree of order n Each node has: n keys and n+1 pointers These are fixed To keep the nodes not too empty, also for the operations to be applied efficiently: * Non-leaf: at least (n+1)/2 pointers (to children) * Leaf: at least (n+1)/2 pointers to data (plus a “sequence pointer” to the next leaf) Basically: use at least one half of the pointers Notes #7

Example (B+ tree of order n=3) Full node Min. node 120 150 180 30 Non-leaf 3 5 11 30 35 Leaf Notes #7

B+tree rules Notes #7

B+tree rules Rule 1. All leaves are at same lowest level (balanced tree) Notes #7

B+tree rules Rule 1. All leaves are at same lowest level (balanced tree) Rule 2. Pointers in leaves point to records except for “sequence pointer” Notes #7

B+tree rules Rule 1. All leaves are at same lowest level (balanced tree) Rule 2. Pointers in leaves point to records except for “sequence pointer” Rule 3. Number of keys/pointers in nodes: Max. # pointers Max. # keys Min. # keys Non-leaf n+1 n (n+1)/2 (n+1)/2 1 Leaf (n+1)/2 + 1 (n+1)/2 Root 2 1 Notes #7

B+tree rules Rule 1. All leaves are at same lowest level (balanced tree) Rule 2. Pointers in leaves point to records except for “sequence pointer” Rule 3. Number of keys/pointers in nodes: Max. # pointers Max. # keys Min. # keys Non-leaf n+1 n (n+1)/2 (n+1)/2 1 Leaf (n+1)/2 + 1 (n+1)/2 Root 2 1 could be 1 Notes #7

Search in a B+tree Notes #7

Search in a B+tree Start from the root Search in a leaf block May not have to go to the data file 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)); Notes #7

Search in a B+tree Start from the root Search in a leaf block May not have to go to the data file 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)); Notes #7

Search in B+tree Search(ptr, k); p1 k1 p2 k2 …… pn kn pn+1 A tree node \\ 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)); Search in B+tree p1 k1 p2 k2 …… pn kn pn+1 A tree node Notes #7

Search in B+tree Search(ptr, k); p1 k1 p2 k2 …… pn kn pn+1 A tree node \\ 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)); Search in B+tree p1 k1 p2 k2 …… pn kn pn+1 A tree node root 100 30 120 150 180 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 Notes #7

Search in B+tree Search(ptr, k); p1 k1 p2 k2 …… pn kn pn+1 A tree node \\ 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)); Search in B+tree p1 k1 p2 k2 …… pn kn pn+1 A tree node Search(*prt, 130) root 100 30 120 150 180 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 Notes #7

Search in B+tree Search(ptr, k); p1 k1 p2 k2 …… pn kn pn+1 A tree node \\ 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)); Search in B+tree p1 k1 p2 k2 …… pn kn pn+1 A tree node Search(*prt, 130) root 100 30 120 150 180 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 Notes #7

Search in B+tree Search(ptr, k); p1 k1 p2 k2 …… pn kn pn+1 A tree node \\ 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)); Search in B+tree p1 k1 p2 k2 …… pn kn pn+1 A tree node Search(*prt, 130) root 100 100  130 30 120 150 180 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 Notes #7

Search in B+tree Search(ptr, k); p1 k1 p2 k2 …… pn kn pn+1 A tree node \\ 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)); Search in B+tree p1 k1 p2 k2 …… pn kn pn+1 A tree node Search(*prt, 130) root 100 100  130 30 120 150 180 120  130 <150 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 Notes #7

Search in B+tree Search(ptr, k); p1 k1 p2 k2 …… pn kn pn+1 A tree node \\ 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)); Search in B+tree p1 k1 p2 k2 …… pn kn pn+1 A tree node Search(*prt, 130) root 100 100  130 30 120 150 180 120  130 <150 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 130 =130 return Notes #7

Range Search in B+tree To research all records whose key values are between k1 and k2: Notes #7

Range Search in B+tree To research all records whose key values are between k1 and k2: Range-Search(ptr, k1, k2) Notes #7

Range Search in B+tree To research all records whose key values are between k1 and k2: Range-Search(ptr, k1, k2) Call Search(ptr, k1); Notes #7

Range Search in B+tree To research all records whose key values are between k1 and k2: Range-Search(ptr, k1, k2) Call Search(ptr, k1); Follow the sequence pointers until the search key value is larger than k2. Notes #7

Range Search in B+tree Search(ptr, k); p1 k1 p2 k2 …… pn kn pn+1 \\ 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 in B+tree p1 k1 p2 k2 …… pn kn pn+1 A tree node Range-Search(ptr, k1, k2) Call Search(ptr, k1); Follow the sequence pointers until the search key value is larger than k2. root 100 30 120 150 180 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 Notes #7

Range Search in B+tree Search(ptr, k); p1 k1 p2 k2 …… pn kn pn+1 \\ 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 in B+tree p1 k1 p2 k2 …… pn kn pn+1 A tree node Range-Search(ptr, k1, k2) Call Search(ptr, k1); Follow the sequence pointers until the search key value is larger than k2. Range-Search(*prt, 50, 125) root 100 30 120 150 180 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 Notes #7

Range Search in B+tree Search(ptr, k); p1 k1 p2 k2 …… pn kn pn+1 \\ 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 in B+tree p1 k1 p2 k2 …… pn kn pn+1 A tree node Range-Search(ptr, k1, k2) Call Search(ptr, k1); Follow the sequence pointers until the search key value is larger than k2. Range-Search(*prt, 50, 125) Search(*prt, 50) root 100 30 120 150 180 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 Notes #7

Range Search in B+tree Search(ptr, k); p1 k1 p2 k2 …… pn kn pn+1 \\ 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 in B+tree p1 k1 p2 k2 …… pn kn pn+1 A tree node Range-Search(ptr, k1, k2) Call Search(ptr, k1); Follow the sequence pointers until the search key value is larger than k2. Range-Search(*prt, 50, 125) Search(*prt, 50) root 100 30 120 150 180 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 Notes #7

Range Search in B+tree Search(ptr, k); p1 k1 p2 k2 …… pn kn pn+1 \\ 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 in B+tree p1 k1 p2 k2 …… pn kn pn+1 A tree node Range-Search(ptr, k1, k2) Call Search(ptr, k1); Follow the sequence pointers until the search key value is larger than k2. Range-Search(*prt, 50, 125) Search(*prt, 50) root 100 50 < 100 30 120 150 180 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 Notes #7

Range Search in B+tree Search(ptr, k); p1 k1 p2 k2 …… pn kn pn+1 \\ 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 in B+tree p1 k1 p2 k2 …… pn kn pn+1 A tree node Range-Search(ptr, k1, k2) Call Search(ptr, k1); Follow the sequence pointers until the search key value is larger than k2. Range-Search(*prt, 50, 125) Search(*prt, 50) root 100 50 < 100 30 120 150 180 30  50 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 Notes #7

Range Search in B+tree Search(ptr, k); p1 k1 p2 k2 …… pn kn pn+1 \\ 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 in B+tree p1 k1 p2 k2 …… pn kn pn+1 A tree node Range-Search(ptr, k1, k2) Call Search(ptr, k1); Follow the sequence pointers until the search key value is larger than k2. Range-Search(*prt, 50, 125) Search(*prt, 50) root 100 50 < 100 30 120 150 180 30  50 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 35 < 50 Not Return Notes #7

Range Search in B+tree Search(ptr, k); p1 k1 p2 k2 …… pn kn pn+1 \\ 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 in B+tree p1 k1 p2 k2 …… pn kn pn+1 A tree node Range-Search(ptr, k1, k2) Call Search(ptr, k1); Follow the sequence pointers until the search key value is larger than k2. Range-Search(*prt, 50, 125) Search(*prt, 50) root 100 50 < 100 30 120 150 180 30  50 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 35 < 50 Not Return Notes #7

Range Search in B+tree Search(ptr, k); p1 k1 p2 k2 …… pn kn pn+1 \\ 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 in B+tree p1 k1 p2 k2 …… pn kn pn+1 A tree node Range-Search(ptr, k1, k2) Call Search(ptr, k1); Follow the sequence pointers until the search key value is larger than k2. Range-Search(*prt, 50, 125) Search(*prt, 50) root 100 50 < 100 30 120 150 180 30  50 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 35 < 50 100  125 110  125 Not Return return return 101  125 return Notes #7

Range Search in B+tree Search(ptr, k); p1 k1 p2 k2 …… pn kn pn+1 \\ 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 in B+tree p1 k1 p2 k2 …… pn kn pn+1 A tree node Range-Search(ptr, k1, k2) Call Search(ptr, k1); Follow the sequence pointers until the search key value is larger than k2. Range-Search(*prt, 50, 125) Search(*prt, 50) root 100 50 < 100 30 120 150 180 30  50 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 35 < 50 100  125 110  125 Not Return return return 101  125 return Notes #7

Range Search in B+tree Search(ptr, k); p1 k1 p2 k2 …… pn kn pn+1 \\ 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 in B+tree p1 k1 p2 k2 …… pn kn pn+1 A tree node Range-Search(ptr, k1, k2) Call Search(ptr, k1); Follow the sequence pointers until the search key value is larger than k2. Range-Search(*prt, 50, 125) Search(*prt, 50) root 100 50 < 100 30 120 150 180 30  50 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 35 < 50 100  125 110  125 Not Return return return 101  125 120  125 return return Notes #7

Range Search in B+tree Search(ptr, k); p1 k1 p2 k2 …… pn kn pn+1 \\ 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 in B+tree p1 k1 p2 k2 …… pn kn pn+1 A tree node Range-Search(ptr, k1, k2) Call Search(ptr, k1); Follow the sequence pointers until the search key value is larger than k2. Range-Search(*prt, 50, 125) Search(*prt, 50) root 100 50 < 100 30 120 150 180 30  50 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 35 < 50 100  125 110  125 135 > 125 Not Return return return STOP 101  125 120  125 return return Notes #7

Insert into B+tree Notes #7

Insert into B+tree Basic idea: Notes #7

Insert into B+tree Basic idea: Find the leaf L where the record r should be inserted; Notes #7

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; Notes #7

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; Notes #7

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

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

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

I. Simple case: Insert key 32 order n=3 100 30 40 3 5 11 30 31 Notes #7

I. Simple case: Insert key 32 order n=3 Insert(prt, 32) 100 30 40 3 5 11 30 31 Notes #7

I. Simple case: Insert key 32 order n=3 Insert(prt, 32) 100 32 < 100 30 40 30  32 <40 3 5 11 30 31 Notes #7

I. Simple case: Insert key 32 order n=3 Insert(prt, 32) 100 32 < 100 30 40 30  32 <40 3 5 11 30 31 room for 32 Notes #7

I. Simple case: Insert key 32 order n=3 Insert(prt, 32) 100 32 < 100 30 40 30  32 <40 3 5 11 30 31 32 Notes #7

Insert into B+tree Simple case (space available for new child) Leaf overflow Non-leaf overflow New root Idea: when there is no space for a new child (all pointers are in use), split the node into two nodes, with both at least half full. Notes #7

Complication: node overflow Notes #7

Complication: Leaf overflow Notes #7

Complication: Leaf overflow p k p’ p1 k1 … p k p k … … pn kn p* 𝑛+1 /2 𝑛+1 /2 𝑛+1 /2 +1 𝑛+1 /2 +1 pn+1 kn+1 Notes #7

Complication: Leaf overflow p k k p’ p1 k1 … p k --- p** p k … pn kn pn+1 kn+1 --- p* 𝑛+1 /2 𝑛+1 /2 𝑛+1 /2 +1 𝑛+1 /2 +1 p k p’ p1 k1 … p k p k … … pn kn p* 𝑛+1 /2 𝑛+1 /2 𝑛+1 /2 +1 𝑛+1 /2 +1 pn+1 kn+1 Notes #7

Complication: Leaf overflow p k k p’ p1 k1 … p k --- p** p k … pn kn pn+1 kn+1 --- p* 𝑛+1 /2 𝑛+1 /2 𝑛+1 /2 +1 𝑛+1 /2 +1 p k p’ p1 k1 … p k p k … … pn kn p* 𝑛+1 /2 𝑛+1 /2 𝑛+1 /2 +1 𝑛+1 /2 +1 pn+1 kn+1 Notes #7

Complication: Leaf overflow p k k p’ q p1 k1 … p k --- p** p k … pn kn pn+1 kn+1 --- p* 𝑛+1 /2 𝑛+1 /2 𝑛+1 /2 +1 𝑛+1 /2 +1 p k p’ p1 k1 … p k p k … … pn kn p* 𝑛+1 /2 𝑛+1 /2 𝑛+1 /2 +1 𝑛+1 /2 +1 pn+1 kn+1 Notes #7

Complication: Leaf overflow p k k p’ ? q What is the key here? p1 k1 … p k --- p** p k … pn kn pn+1 kn+1 --- p* 𝑛+1 /2 𝑛+1 /2 𝑛+1 /2 +1 𝑛+1 /2 +1 p k p’ p1 k1 … p k p k … … pn kn p* 𝑛+1 /2 𝑛+1 /2 𝑛+1 /2 +1 𝑛+1 /2 +1 pn+1 kn+1 Notes #7

Complication: Leaf overflow p k k p’ ? q What is the key here? p1 k1 … p k --- p** p k … pn kn pn+1 kn+1 --- p* 𝑛+1 /2 𝑛+1 /2 𝑛+1 /2 +1 𝑛+1 /2 +1 p k p’ p1 k1 … p k p k … … pn kn p* 𝑛+1 /2 𝑛+1 /2 𝑛+1 /2 +1 𝑛+1 /2 +1 pn+1 kn+1 Notes #7

Complication: Leaf overflow p k k p’ q 𝑛+1 /2 +1 p1 k1 … p k --- p** p k … pn kn pn+1 kn+1 --- p* 𝑛+1 /2 𝑛+1 /2 𝑛+1 /2 +1 𝑛+1 /2 +1 p k p’ p1 k1 … p k p k … … pn kn p* 𝑛+1 /2 𝑛+1 /2 𝑛+1 /2 +1 𝑛+1 /2 +1 pn+1 kn+1 Notes #7

Leaf overflow: Insert key 7 (order n = 3) 100 30 3 5 11 30 31 Notes #7

Leaf overflow: Insert key 7 (order n = 3) 100 30 7 3 5 11 30 31 Notes #7

Leaf overflow: Insert key 7 (order n = 3) 100 30 3 5 7 11 30 31 Notes #7

Leaf overflow: Insert key 7 (order n = 3) 100 30 3 5 7 11 30 31 3 5 11 7 Notes #7

Leaf overflow: Insert key 7 (order n = 3) 100 30 3 5 7 11 30 31 3 5 11 7 Notes #7

Leaf overflow: Insert key 7 (order n = 3) 100 30 3 5 7 11 30 31 3 5 11 7 Notes #7

Leaf overflow: Insert key 7 (order n = 3) 100 30 3 5 7 11 30 31 3 5 11 7 Notes #7

Leaf overflow: Insert key 7 (order n = 3) 100 7 30 3 5 7 11 30 31 3 5 11 7 Notes #7

Complication: nonleaf overflow Notes #7

Complication: nonleaf overflow p k’ p’ kn+1 pn+2 p1 k1 … p k p k p k … pn kn pn+1 (𝑛+1)/2 (𝑛+1)/2 -1 (𝑛+1)/2 +1 (𝑛+1)/2 +1 Notes #7

Complication: nonleaf overflow p k’ p’ k q (𝑛+1)/2 p1 k1 p2 k2 … p k p p k … pn kn pn+1 kn+1 pn+2 (𝑛+1)/2 -1 (𝑛+1)/2 -1 (𝑛+1)/2 (𝑛+1)/2 +1 (𝑛+1)/2 +1 p k’ p’ kn+1 pn+2 p1 k1 … p k p k p k … pn kn pn+1 (𝑛+1)/2 (𝑛+1)/2 -1 (𝑛+1)/2 +1 (𝑛+1)/2 +1 Notes #7

Complication: nonleaf overflow p k’ p’ k q (𝑛+1)/2 p1 k1 p2 k2 … p k p p k … pn kn pn+1 kn+1 pn+2 (𝑛+1)/2 -1 (𝑛+1)/2 -1 (𝑛+1)/2 (𝑛+1)/2 +1 (𝑛+1)/2 +1 p k’ p’ kn+1 pn+2 p1 k1 … p k p k p k … pn kn pn+1 (𝑛+1)/2 (𝑛+1)/2 -1 (𝑛+1)/2 +1 (𝑛+1)/2 +1 Notes #7

Complication: nonleaf overflow p k’ p’ k q (𝑛+1)/2 p1 k1 p2 k2 … p k p p k … pn kn pn+1 kn+1 pn+2 (𝑛+1)/2 -1 (𝑛+1)/2 -1 (𝑛+1)/2 (𝑛+1)/2 +1 (𝑛+1)/2 +1 p k’ p’ kn+1 pn+2 p1 k1 … p k p k p k … pn kn pn+1 (𝑛+1)/2 (𝑛+1)/2 -1 (𝑛+1)/2 +1 (𝑛+1)/2 +1 Notes #7

Complication: nonleaf overflow p k’ p’ k ? q (𝑛+1)/2 What is the key here? p1 k1 p2 k2 … p k p p k … pn kn pn+1 kn+1 pn+2 (𝑛+1)/2 -1 (𝑛+1)/2 -1 (𝑛+1)/2 (𝑛+1)/2 +1 (𝑛+1)/2 +1 p k’ p’ kn+1 pn+2 p1 k1 … p k p k p k … pn kn pn+1 (𝑛+1)/2 (𝑛+1)/2 -1 (𝑛+1)/2 +1 (𝑛+1)/2 +1 Notes #7

Complication: nonleaf overflow p k’ p’ k ? q (𝑛+1)/2 What is the key here? p1 k1 p2 k2 … p k p p k … pn kn pn+1 kn+1 pn+2 (𝑛+1)/2 -1 (𝑛+1)/2 -1 (𝑛+1)/2 (𝑛+1)/2 +1 (𝑛+1)/2 +1 p k’ p’ not used kn+1 pn+2 p1 k1 … p k p k p k … pn kn pn+1 (𝑛+1)/2 (𝑛+1)/2 -1 (𝑛+1)/2 +1 (𝑛+1)/2 +1 Notes #7

Complication: nonleaf overflow p k’ p’ k k q (𝑛+1)/2 (𝑛+1)/2 p1 k1 p2 k2 … p k p p k … pn kn pn+1 kn+1 pn+2 (𝑛+1)/2 -1 (𝑛+1)/2 -1 (𝑛+1)/2 (𝑛+1)/2 +1 (𝑛+1)/2 +1 p k’ p’ kn+1 pn+2 p1 k1 … p k p k p k … pn kn pn+1 (𝑛+1)/2 (𝑛+1)/2 -1 (𝑛+1)/2 +1 (𝑛+1)/2 +1 Notes #7

Nonleaf overflow: Insert key 160 (order n = 3) 100 120 150 180 160 150 156 179 180 210 Notes #7

Nonleaf overflow: Insert key 160 (order n = 3) 100 120 150 180 150 156 160 179 180 210 150 156 179 160 Notes #7

Nonleaf overflow: Insert key 160 (order n = 3) 100 120 150 180 150 156 160 179 180 210 150 156 179 160 Notes #7

Nonleaf overflow: Insert key 160 (order n = 3) 100 120 150 180 160 150 156 160 179 180 210 150 156 179 160 Notes #7

Nonleaf overflow: Insert key 160 (order n = 3) 100 120 150 180 no room! 160 150 156 160 179 180 210 150 156 179 160 Notes #7

Nonleaf overflow: Insert key 160 (order n = 3) 120 150 180 160 100 120 150 180 160 150 156 160 179 180 210 150 156 179 160 Notes #7

Nonleaf overflow: Insert key 160 (order n = 3) 120 150 180 160 100 120 150 180 160 150 156 160 179 180 210 150 156 179 160 Notes #7

Nonleaf overflow: Insert key 160 (order n = 3) 120 150 180 160 100 160 120 150 180 160 150 156 160 179 180 210 150 156 179 160 Notes #7

Nonleaf overflow: Insert key 160 (order n = 3) 100 160 120 150 180 150 156 160 179 180 210 Notes #7