FALL 2004CENG 351 File Structures1 Multilevel Indexing and B+ Trees Reference: Folk et al. Chapters: 9.1, 9.2, 10.1, 10.2,10.3,10.10.

Slides:



Advertisements
Similar presentations
B+-Trees and Hashing Techniques for Storage and Index Structures
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 Tree-Structured Indexes Chapter 9.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Indexing Structures for Files.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
1 Tree-Structured Indexes Module 4, Lecture 4. 2 Introduction As for any index, 3 alternatives for data entries k* : 1. Data record with key value k 2.
Multilevel Indexing and B+ Trees
ICS 421 Spring 2010 Indexing (1) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 02/18/20101Lipyeow Lim.
CS4432: Database Systems II
B+-tree and Hashing.
Tree-Structured Indexes Lecture 5 R & G Chapter 9 “If I had eight hours to chop down a tree, I'd spend six sharpening my ax.” Abraham Lincoln.
Tree-Structured Indexes Lecture 17 R & G Chapter 9 “If I had eight hours to chop down a tree, I'd spend six sharpening my ax.” Abraham Lincoln.
Tree-Structured Indexes. Introduction v As for any index, 3 alternatives for data entries k* : À Data record with key value k Á Â v Choice is orthogonal.
1 Tree-Structured Indexes Yanlei Diao UMass Amherst Feb 20, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
1 Tree-Structured Indexes Chapter Introduction  As for any index, 3 alternatives for data entries k* :  Data record with key value k   Choice.
Tree-Structured Indexes Lecture 5 R & G Chapter 10 “If I had eight hours to chop down a tree, I'd spend six sharpening my ax.” Abraham Lincoln.
1 B+ Trees. 2 Tree-Structured Indices v Tree-structured indexing techniques support both range searches and equality searches. v ISAM : static structure;
CS4432: Database Systems II
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Tree-Structured Indexes Chapter 9.
Tree-Structured Indexes. Range Searches ``Find all students with gpa > 3.0’’ –If data is in sorted file, do binary search to find first such student,
Introduction to Database Systems1 B+-Trees Storage Technology: Topic 5.
 B+ Tree Definition  B+ Tree Properties  B+ Tree Searching  B+ Tree Insertion  B+ Tree Deletion.
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,
Adapted from Mike Franklin
1.1 CS220 Database Systems Tree Based Indexing: B+-tree Slides courtesy G. Kollios Boston University via UC Berkeley.
Tree-Structured Indexes Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Tree-Structured Indexes.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Tree- and Hash-Structured Indexes Selected Sections of Chapters 10 & 11.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Tree-Structured Indexes Chapter 9.
1 Indexing. 2 Motivation Sells(bar,beer,price )Bars(bar,addr ) Joe’sBud2.50Joe’sMaple St. Joe’sMiller2.75Sue’sRiver Rd. Sue’sBud2.50 Sue’sCoors3.00 Query:
B+ Tree Index tuning--. overview B + -Tree Scalability Typical order: 100. Typical fill-factor: 67%. –average fanout = 133 Typical capacities (root at.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 B+-Tree Index Chapter 10 Modified by Donghui Zhang Nov 9, 2005.
Storage and Indexing. How do we store efficiently large amounts of data? The appropriate storage depends on what kind of accesses we expect to have to.
CPSC 404, Laks V.S. Lakshmanan1 Tree-Structured Indexes (Brass Tacks) Chapter 10 Ramakrishnan and Gehrke (Sections )
Data on External Storage – File Organization and Indexing – Cluster Indexes - Primary and Secondary Indexes – Index data Structures – Hash Based Indexing.
1 Tree-Structured Indexes Chapter Introduction  As for any index, 3 alternatives for data entries k* :  Data record with key value k   Choice.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Tree-Structured Indexes Content based on Chapter 10 Database Management Systems, (3 rd.
I/O Cost Model, Tree Indexes CS634 Lecture 5, Feb 12, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Tree-Structured Indexes Chapter 10
CENG 3511 Indexed File Organization. CENG 3512 Indexing-1 Indexing allows access to records based on a key, on which the file is stored and accessed.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Tree-Structured Indexes Chapter 10.
Tree-Structured Indexes R & G Chapter 10 “If I had eight hours to chop down a tree, I'd spend six sharpening my ax.” Abraham Lincoln.
Database Applications (15-415) DBMS Internals- Part III Lecture 13, March 06, 2016 Mohammad Hammoud.
Tree-Structured Indexes. Introduction As for any index, 3 alternatives for data entries k*: – Data record with key value k –  Choice is orthogonal to.
Multilevel Indexing and B+ Trees
Multilevel Indexing and B+ Trees
Tree-Structured Indexes
Tree-Structured Indexes
COP Introduction to Database Structures
Indexing CENG 351 File Structures.
B+-Trees and Static Hashing
Tree-Structured Indexes
CS222/CS122C: Principles of Data Management Notes #07 B+ Trees
Introduction to Database Systems Tree Based Indexing: B+-tree
Tree-Structured Indexes
Tree-Structured Indexes
B+Trees The slides for this text are organized into chapters. This lecture covers Chapter 9. Chapter 1: Introduction to Database Systems Chapter 2: The.
Tree-Structured Indexes
Adapted from Mike Franklin
Tree-Structured Indexes
Indexing 1.
Database Systems (資料庫系統)
General External Merge Sort
Tree-Structured Indexes
Tree-Structured Indexes
Indexing February 28th, 2003 Lecture 20.
Tree-Structured Indexes
Tree-Structured Indexes
Tree-Structured Indexes
CS222/CS122C: Principles of Data Management UCI, Fall 2018 Notes #06 B+ trees Instructor: Chen Li.
CS222P: Principles of Data Management UCI, Fall Notes #06 B+ trees
Presentation transcript:

FALL 2004CENG 351 File Structures1 Multilevel Indexing and B+ Trees Reference: Folk et al. Chapters: 9.1, 9.2, 10.1, 10.2,10.3,10.10

FALL 2004CENG 351 File Structures2 Problems with simple indexes If index does not fit in memory: 1.Seeking the index is slow (binary search): –We don’t want more than 3 or 4 seeks for a search. 2.Insertions and deletions take O(N) disk accesses. NLog(N+1) 15 keys4 1000~10 100,000~17 1,000,000~20

FALL 2004CENG 351 File Structures3 Multilevel Indexing Build an index of an index file: –Build a simple index for the file sorting keys using external sorting. –Build an index for this index. –Build another index for the previous index, and so on. Note: the index of an index stores the largest key in the record it is pointing to. Insertion and deletion are still problematic.

FALL 2004CENG 351 File Structures4 Indexed Sequential Files Provide a choice between two alternative views of a file: 1.Indexed: the file can be seen as a set of records that is indexed by key; or 2.Sequential: the file can be accessed sequentially (physically contiguous records), returning records in order by key.

FALL 2004CENG 351 File Structures5 Example of applications Student record system in a university: –Indexed view: access to individual records –Sequential view: batch processing when posting grades Credit card system: –Indexed view: interactive check of accounts –Sequential view: batch processing of payments

FALL 2004CENG 351 File Structures6 The initial idea Maintain a sequence set: –Group the records into blocks in a sorted way. –Maintain the order in the blocks as records are added or deleted through splitting, concatenation, and redistribution. Construct a simple, single level index for these blocks. –Choose to build an index that contain the key for the last record in each block.

FALL 2004CENG 351 File Structures7 Maintaining a Sequence Set Sorting and re-organizing after insertions and deletions is out of question. We organize the sequence set in the following way: –Records are grouped in blocks. –Blocks should be at least half full. –Link fields are used to point to the preceding block and the following block (similar to doubly linked lists) –Changes (insertion/deletion) are localized into blocks by performing: Block splitting when insertion causes overflow Block merging or redistribution when deletion causes underflow.

FALL 2004CENG 351 File Structures8 Example: insertion Block size = 4 Key : Last name ADAMS …BIXBY …CARSON …COLE … Block 1 Insert “BAIRD …”: ADAMS …BAIRD …BIXBY … CARSON..COLE … Block 1 Block 2

FALL 2004CENG 351 File Structures9 Example: deletion ADAMS …BAIRD …BIXBY …BOONE … Block 1 BYNUM…CARSON..CARTER.. DENVER…ELLIS … Block 2 Block 3 Delete “DAVIS”, “BYNUM”, “CARTER”, COLE…DAVISBlock 4

FALL 2004CENG 351 File Structures10 Add an Index set KeyBlock BERNE1 CAGE2 DUTTON3 EVANS4 FOLK5 GADDIS6

FALL 2004CENG 351 File Structures11 Tree indexes This simple scheme is nice if the index fits in memory. If index doesn’t fit in memory: –Divide the index structure into blocks, –Organize these blocks similarly building a tree structure. Tree indexes: –B Trees –B+ Trees –Simple prefix B+ Trees –…

FALL 2004CENG 351 File Structures12 Separators BlockRange of KeysSeparator 1ADAMS-BERNE BOLEN 2BOLEN-CAGE CAMP 3CAMP-DUTTON EMBRY 4EMBRY-EVANS FABER 5FABER-FOLK FOLKS 6FOLKS-GADDIS

FALL 2004CENG 351 File Structures13 ADAMS-BERNE BOLEN CAMP EMBRY FABER FOLKS Index set root

FALL 2004CENG 351 File Structures14 B Trees B-tree is one of the most important data structures in computer science. What does B stand for? (Not binary!) B-tree is a multiway search tree. Several versions of B-trees have been proposed, but only B+ Trees has been used with large files. A B+tree is a B-tree in which data records are in leaf nodes, and faster sequential access is possible.

FALL 2004CENG 351 File Structures15 Formal definition of B+ Tree Properties Properties of a B+ Tree of order v : –All internal nodes (except root) has at least v keys and at most 2v keys. –The root has at least 2 children unless it ’ s a leaf.. –All leaves are on the same level. –An internal node with k keys has k+1 children

FALL 2004CENG 351 File Structures16 B+ tree: Internal/root node structure P 0 K 1 P 1 K 2 ……………… P n-1 K n P n  Requirements:  K 1 < K 2 < … < K n  For any search key value K in the subtree pointed by Pi, If Pi = P0, we require K < K1 If Pi = Pn, Kn  K If Pi = P1, …, Pn-1, Ki  K < Ki+1 Each P i is a pointer to a child node; each K i is a search key value # of search key values = n, # of pointers = n+1

FALL 2004CENG 351 File Structures17  Pointer L points to the left neighbor; R points to the right neighbor  K 1 < K 2 < … < K n  v  n  2v (v is the order of this B+ tree)  We will use K i * for the pair and omit L and R for simplicity B+ tree: leaf node structure L K 1 r 1 K 2 ……………… K n r n R

FALL 2004CENG 351 File Structures18 Example: B+ tree with order of 1 Each node must hold at least 1 entry, and at most 2 entries 10*15*20*27*33*37*40* 46* 51* 55* 63* 97* Root

FALL 2004CENG 351 File Structures19 Example: Search in a B+ tree order 2 Search: how to find the records with a given search key value? –Begin at root, and use key comparisons to go to leaf Examples: search for 5*, 16*, all data entries >= 24*... –The last one is a range search, we need to do the sequential scan, starting from the first leaf containing a value >= 24. Root * 3*5* 7*14*15* 19*20*22*24*27* 29*33*34* 38* 39* 13

FALL 2004CENG 351 File Structures20 Cost for searching a value in B+ tree Typically, a node is a page (block or cluster) Let H be the height of the B+ tree: we need to read H+1 pages to reach a leaf node Let F be the (average) number of pointers in a node (for internal node, called fanout ) –Level 1 = 1 page = F 0 page –Level 2 = F pages = F 1 pages –Level 3 = F * F pages = F 2 pages –Level H+1 = …….. = F H pages (i.e., leaf nodes) –Suppose there are D data entries. So there are D/(F-1) leaf nodes –D/(F-1) = F H. That is, H = log F ( ) 1212 level Height 0 2

FALL 2004CENG 351 File Structures21 B+ Trees in Practice Typical order: 100. Typical fill-factor: 67%. – average fanout = 133 (i.e, # of pointers in internal node) 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 Suppose there are 1,000,000,000 data entries. – H = log 133 ( /132) < 4 – The cost is 5 pages read

FALL 2004CENG 351 File Structures22 How to Insert a Data Entry into a B+ Tree? Let’s look at several examples first.

FALL 2004CENG 351 File Structures23 Inserting 16*, 8* into Example B+ tree Root * 3*5* 7* 8* 2* 5*7* 3* * You overflow One new child (leaf node) generated; must add one more pointer to its parent, thus one more key value as well. 14* 15* 16*

FALL 2004CENG 351 File Structures24 Inserting 8* (cont.) Copy up the middle value (leaf split) 2* 3*5* 7* 8* 5 Entry to be inserted in parent node. (Note that 5 is continues to appear in the leaf.) s copied up and You overflow!

FALL 2004CENG 351 File Structures25 (Note that 17 is pushed up and only appears once in the index. Contrast Entry to be inserted in parent node. this with a leaf split.) Insertion into B+ tree (cont.) Understand difference between copy-up and push-up Observe how minimum occupancy is guaranteed in both leaf and index pg splits. We split this node, redistribute entries evenly, and push up middle key. 

FALL 2004CENG 351 File Structures26 Example B+ Tree After Inserting 8* Notice that root was split, leading to increase in height. 2*3* Root *15* 19*20*22*24*27* 29*33*34* 38* 39* 135 7*5*8*

FALL 2004CENG 351 File Structures27 Inserting a Data Entry into a B+ Tree: Summary 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, put middle key in L2 copy up middle key. Insert index entry pointing to L2 into parent of L. This can happen recursively – To split index node, redistribute entries evenly, but push up middle key. (Contrast with leaf splits.) Splits “grow” tree; root split increases height. – Tree growth: gets wider or one level taller at top.

FALL 2004CENG 351 File Structures28 Deleting a Data Entry from a B+ Tree Examine examples first …

FALL 2004CENG 351 File Structures29 Delete 19* and 20* 2*3* Root *16* 19*20*22*24*27* 29*33*34* 38* 39* 135 7*5*8* 22* 27* 29* 22* 24* You underflow Have we still forgot something?

FALL 2004CENG 351 File Structures30 Deleting 19* and 20* (cont.) Notice how 27 is copied up. But can we move it up? Now we want to delete 24 Underflow again! But can we redistribute this time? 2*3* Root *16* 33*34* 38* 39* 135 7*5*8*22*24* 27 27*29*

FALL 2004CENG 351 File Structures31 Deleting 24* Observe the two leaf nodes are merged, and 27 is discarded from their parent, but … Observe `pull down’ of index entry (below) *27* 29*33*34* 38* 39* 2* 3* 7* 14*16* 22* 27* 29* 33*34* 38* 39* 5*8* You underflow Merge with sibling! New root

FALL 2004CENG 351 File Structures32 Deleting a Data Entry from a B+ Tree: Summary 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). If 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.

FALL 2004CENG 351 File Structures33 Example of Non-leaf Re-distribution Tree is shown below during deletion of 24*. (What could be a possible initial tree?) In contrast to previous example, can re-distribute entry from left child of root to right child. Root *16* 17*18* 20*33*34* 38* 39* 22*27*29*21* 7*5*8* 3*2*

FALL 2004CENG 351 File Structures34 After Re-distribution Intuitively, entries are re-distributed by `pushing through’ the splitting entry in the parent node. It suffices to re-distribute index entry with key 20; we’ve re-distributed 17 as well for illustration. 14*16* 33*34* 38* 39* 22*27*29* 17*18* 20*21* 7*5*8* 2*3* Root

FALL 2004CENG 351 File Structures35 Terminology Bucket Factor: the number of records which can fit in a leaf node. Fan-out : the average number of children of an internal node. A B+tree index can be used either as a primary index or a secondary index. –Primary index: determines the way the records are actually stored (also called a sparse index) –Secondary index: the records in the file are not grouped in buckets according to keys of secondary indexes (also called a dense index)

FALL 2004CENG 351 File Structures36 Summary Tree-structured indexes are ideal for range- searches, also good for equality searches. B+ tree is a dynamic structure. – Inserts/deletes leave tree height-balanced; High fanout (F) means depth rarely more than 3 or 4. – Almost always better than maintaining a sorted file. – Typically, 67% occupancy on average. – If data entries are data records, splits can change rids! Most widely used index in database management systems because of its versatility. One of the most optimized components of a DBMS.