COURSE 9 Tree Structured Files. Database Management Systems2 Binary Tree Organization K Data Pointer Left Pointer Right Heap and sorted files - useful.

Slides:



Advertisements
Similar presentations
State Feedback Controller Design
Advertisements

Indexes An index on a file speeds up selections on the search key fields for the index. Any subset of the fields of a relation can be the search key for.
B+-Trees and Hashing Techniques for Storage and Index Structures
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.
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.
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
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Range Searches  `` Find all students with gpa > 3.0 ’’  If data is in sorted file, do.
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;
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.
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.
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.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.
Index tuning-- B+tree. overview Overview of tree-structured index Indexed sequential access method (ISAM) B+tree.
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.
1 Database Systems ( 資料庫系統 ) November 1, 2004 Lecture #8 By Hao-hua Chu ( 朱浩華 )
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 )
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
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
Database Systems (資料庫系統)
CS522 Advanced database Systems
Tree-Structured Indexes
Tree-Structured Indexes
COP Introduction to Database Structures
Extra: B+ Trees CS1: Java Programming Colorado State University
Database Applications (15-415) DBMS Internals- Part III Lecture 15, March 11, 2018 Mohammad Hammoud.
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
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
Tree-Structured Indexes
Indexing 1.
Database Systems (資料庫系統)
Storage and Indexing.
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:

COURSE 9 Tree Structured Files

Database Management Systems2 Binary Tree Organization K Data Pointer Left Pointer Right Heap and sorted files - useful for static files Binary tree organization: efficient inserting /deleting records uses binary search algorithm Memory structure for a binary tree node: Memory structure for a binary tree file: collection of nodes; root pointer list of empty nodes (linked by Pointer Left )

Database Management Systems3 Binary Tree Organization (cont.) Root – pointer to root Free – pointer to empty nodes list head Conceptual tree: L Data L 2 4D Data D NULL L DQ B BFS F Data F NULL NULLB Data B NULL NULL NULL NULLQ Data Q NULL 5S Data S NULL NULL -9NULL Root = 1 Free = 3

Database Management Systems4 Inserting / Removing Records Insert a record: detect the position of the record store new record in a free node link the node its parent Remove a record: search for the record 3 cases: no children: parents pointer= NULL 1 child: attach child to parent 2 children: replace with the closest neighbor value add node to empty nodes list L DQ B BFS E

Database Management Systems5 Insertion Anomaly in Binary Tree L, D, B, Q, N, F, S, R, T, M, E, G, P, A, C L DQ BBFNSA C E GMPRT

Database Management Systems6 Insertion Anomaly in Binary Tree (cont.) A, T, B, S, C, D, E, F, G, R, Q, P, N, M, L AT BB SCDEFGRQP N ML

Database Management Systems7 Optimal vs. Balanced Binary Trees Drawback: searching time depends on inserting order Optimal tree leaves are positioned on at most 2 levels maintenance is difficult / time consuming Balanced tree for each node the difference between its sub-trees heights is 0, 1 or –1 ( tree height : length of the longest path from root to leaves) lower number of operations for maintenance 6 distinct cases when a tree become unbalanced after insertion

Database Management Systems8 Balanced Trees Maintenance A BB A 1 (h) A 2 (h) A 3 (h) 0 A BB A 1 (h) A 2 (h) A 3 * (h+1) -2 1 A BB A 1 (h) A 2 (h) A 3 * (h+1) 0 0 A BB A 1 (h) A 2 (h) A 3 * (h+1) -2 1 B A A 3 (h) A 1 (h) A 2 (h) 1 0 B A A 3 (h) A 1 * (h+1) A 2 (h) 0 0 (1) (2)

Database Management Systems9 Balanced Trees Maintenance (cont.) A BB C A 1 (h) A 2 (h-1) A 3 (h-1) A 4 (h) 0 0 A BB C A 1 (h) A 2 (h-1) A 3 * (h) A 4 (h) 1 -2 A BB C A 1 (h) A 2 (h-1) A 3 * (h) A 4 (h) A BB C A 1 (h) A 2 (h-1) A 3 * (h) A 4 (h) 0 0 A BB C A 1 (h) A 2 (h-1) A 3 (h-1) A 4 (h) A BB C A 1 (h) A 2 * (h) A 3 (h-1) A 4 (h) 0 (3) (4)

Database Management Systems10 Balanced Trees Maintenance (cont.) A BB C A 1 (h) A 2 (h-1) A 3 (h-1) A 4 (h) A BB C A 1 (h) A 2 * (h) A 3 (h-1) A 4 (h) 0 A BB C A 1 (h) A 2 (h-1) A 3 (h-1) A 4 (h) A BB C A 1 (h) A 2 (h-1) A 3 * (h) A 4 (h) 0 0 (6) (5)

Database Management Systems11 Range Searches `` Find all students with grade > 8.0 If data is in sorted file, do binary search to find first such student, then scan to find others. Cost of binary search can be quite high. Simple idea: Create an `index file. Page 1Page 2Page NPage 3 Data File k2 kN k1 Index File * Can do binary search on (smaller) index file!

Database Management Systems12 ISAM Index file may still be quite large. But we can apply the idea repeatedly! P 0 K 1 P 1 K 2 P 2 K m P m index entry Non-leaf Pages Overflow page Leaf Primary pages

Database Management Systems13 Comments on ISAM File creation : Leaf (data) pages allocated sequentially, sorted by search key; then index pages allocated, then space for overflow pages. Index entries : ; they `direct search for data entries, which are in leaf pages. Search : Start at root; use key comparisons to go to leaf. Cost log F N ; F = number of entries/index pg, N = number of leaf pgs Insert : Find leaf data entry belongs to, and put it there. Delete : Find and remove from leaf; if empty overflow page, de-allocate. Data Pages Index Pages Overflow pages Static tree structure : inserts/deletes affect only leaf pages !

Database Management Systems14 Example ISAM Tree Each node can hold 2 entries; 10*15*20*27*33*37*40* 46* 51* 55* 63* 97* Root

Database Management Systems15 After Inserting 23*, 48*, 41*, 42*... 10*15*20*27*33*37*40* 46* 51* 55* 63* 97* Root 23* 48* 41* 42* Overflow Pages Leaf Index Pages Primary

Database Management Systems16... Then Deleting 42*, 51*, 97* * Note that 51* appears in index levels, but not in leaf! 10*15*20*27*33*37*40* 46*55*63* Root 23* 48* 41*

Database Management Systems17 Advantages & Disadvantages of ISAM Can get out of balance - with lots of inserts & deletes (resulting in non-uniform search time) Overflow records may not be sorted (they could be - theyre usually not) Faster insert & delete (no tree balancing…no I/Os for nodes in the tree) Better concurrent access (tree nodes are never locked) Suitable only for files that arent expected to change much.

Database Management Systems18 B – Trees Organization Files Most popular organization of indexes in DB B – stand for balanced or broad B–Tree – ordered tree; each node has several sub- trees A node contains keys and pointers to its sub-trees Paths from root to leaves have the same length L;Q GNS IA;EMPRT;Z

Database Management Systems19 B-Tree Properties B-Tree of order m : If is not a leaf, the root has at least 2 sub-trees Each internal node has at least [ m/2 ] sub-trees (except root) Each internal node has at most m sub-trees All leaves are on the same level A node with p sub-trees contains p-1 ordered key values (K 1, K 2, … K p ) T 1 contains key values <K 1 T i contains key values between K i-1 and K i T p contains key values > K p K 1 ;K 2 ;…;K p-1 T1T1 T2T2 TpTp...

Database Management Systems20 Memory Structure of B-Trees As binary trees Pointer Left refers first key of its left sub-tree in B-Tree Pointer Right/H refers right neighbor key in B-Tree node refers first key of its right sub-tree in B-Tree (if is last key value in a B-Trees node) Additional flag / signed right pointer GNS IMPR L Q TZAE K Data Pointer Left Pointer Right/H L;Q GNS IA;EMPRT;Z

Database Management Systems21 Memory Structure of B-Trees (cont.) Allocate memory to store m-1 key values per node N – number of stored keys K i – key value, AD i – address of record Point i – refers a sub-tree Use pointers memory space to store key values for leaves m/2 N m-1 – for internal nodes m/2 N t – for terminal nodes additional flag / signed N N K 1 K 2... K m-1 Point 1... Point m K m K m+1... K t

Database Management Systems22 Searching a B-Tree Option of following two paths at each node Example: searching the 15 in a B-Tree

Database Management Systems23 Inserting Records in B-Trees Insertion steps: find proper location (node in which the key should be inserted) insert new key perform balance procedure if there is an overflow Algorithm of the Insertion Procedure 1. Find the insertion point 2. Insert the key 3. If the node is full: A) Create a new node and put in the keys bigger than the median key B) Insert the median key into the parent node C) The right side of the key should reference the new node, the left side references the old one that has the smaller elements 4. If the parent node is full: A) If the parent node is a root then create a new root B) Repeat step 3 with the parent node

Database Management Systems24 Inserting Records in B-Trees (cont.) Insert a record with key

Database Management Systems25 Inserting Records in B-Trees (cont.) … then insert a record with key median

Database Management Systems26 Inserting Records in B-Trees (cont.) … then insert a record with key

Database Management Systems27 Deleting Records from B-Trees Deletion steps: find node which contains the key should be deleted if is internal node, transfer a key from leaves if there is an underflow, perform redistribution or concatenation Algorithm of the Deletion Procedure 1. Search for the key to be removed If the key is located into an internal node: - replace it with its bigger neighbor (i.e. with the leftmost key of the leftmost leaf of its right tree )

Database Management Systems28 Deleting Records from B-Trees (cont.) Algorithm of the Deletion Procedure (cont.) 2. Repeat this step until we fall in A) or B) cases A) If the node which contains the key to be deleted is the root or the number of remaining keys is [m/2]: eliminate the key re-arrange the keys (and pointers) in the node finish the algorithm Key to be deleted

Database Management Systems29 Deleting Records from B-Trees (cont.) Algorithm of the Deletion Procedure (cont.) B) If the number of remaining keys is [m/2] keys redistribution evenly divide the keys of both nodes + separator node from parent chose the median key and put it in the parent finish the algorithm Key to be deleted Separator key

Database Management Systems30 Deleting Records from B-Trees (cont.) Algorithm of the Deletion Procedure (cont.) C) If the sum of keys of node which contains the key to be deleted and of any neighbor < m concatenation merge the nodes + separator node from the parent repeat step 2. for the parent node if parent node is the root and has no keys current node become the root Key to be deleted Separator key

Database Management Systems Deleting Records from B-Trees (cont.) Key to be deleted Bigger neighbor underflow merge

Database Management Systems32 Deleting Records from B-Trees (cont.) redistribution underflow

Database Management Systems33 Deleting Records from B-Trees (cont.)

Database Management Systems34 B+-Trees Combination between B-Tree and ISAM: Search begins at root, and key comparisons direct it to a leaf In a B+-Tree all pointers to data records exist only at the leaf-level nodes A B+-Tree can have less levels (or higher capacity of search values) than the corresponding B-tree 2*3* Root *16* 30*34* 38* 39* 135 7*5*8*22*24* 27 27*29* Leaf Index Pages

Database Management Systems35 Node structure P 0 K 1 P 1 K 2 P 2 K m P m index entry Non-leaf nodes Leaf nodes P 0 K 1 P 1 K 2 P 2 K m P m Next leaf node To keys K 1 k < K 2 record with key K 1 record with key K 2 record with key K 3 To keys < K 1 To keys K m

Database Management Systems36 B+-Trees in Practice Typical order: 200. Typical fill-factor: 67%. Average fan-out (no. of entries/index pg) = 133 Typical capacities: Height 4: = 312,900,700 records Height 3: = 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

Database Management Systems37 Advantages & Disadvantages of B+ Trees Index stays balanced…therefore uniform search time Rarely more than levels…the system often maintains the top levels in memory … thus you can search for a record in 2 or 3 I/Os. Occupancy normally about 67% (thus 150% as much space as you need for the data records) Most widely used index in database management systems because of its versatility. One of the most optimized components of a DBMS. B+ trees can be used for a clustered, sparse index (if the data is sorted) or for an un-clustered, dense index (if not).

Database Management Systems38 Bulk Loading of a B+-Tree If we have a large collection of records, and we want to create a B+-Tree on some field, doing so by repeatedly inserting records is very slow. Bulk Loading can be done much more efficiently. Initialization : Sort all data entries, insert pointer to first (leaf) page in a new (root) page. 3* 4* 6*9*10*11*12*13* 20*22* 23*31* 35* 36*38*41*44* Sorted pages of data entries; not yet in B+ tree Root

Database Management Systems39 Bulk Loading 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! 3* 4* 6*9*10*11*12*13* 20*22* 23*31* 35* 36*38*41*44* Root Data entry pages not yet in B+ tree * 4* 6*9*10*11*12*13* 20*22* 23*31* 35* 36*38*41*44* 6 Root not yet in B+ tree Data entry pages

Database Management Systems40 Summary of Bulk Loading Option 1: multiple inserts. Slow. Does not give sequential storage of leaves. Option 2: Bulk Loading Has advantages for concurrency control. Fewer I/Os during build. Leaves will be stored sequentially (and linked, of course). Can control fill factor on pages.

Database Management Systems41 Prefix B+-Trees (Key Compression) Important to increase fan-out Key values in index entries only `direct traffic; can often compress them. E.g., If we have adjacent index entries with search key values Dan Yogurt, David Smith and Demy Moore, we can abbreviate David Smith to Dav. (The other keys can be compressed too...) Is this correct? Not quite! What if there is a data entry Davey Jones ? (Can only compress David Smith to Davi ) In general, while compressing, must leave each index entry greater than every key value (in any sub-tree) to its left. Insert/delete must be modified accordingly.

Database Management Systems42 B+-Tree order in practice Order concept replaced by physical space criterion in practice (` at least half-full ). Index pages can typically hold many more entries than leaf pages. Variable sized records and search keys mean different nodes will contain different numbers of entries. Even with fixed length fields, multiple records with the same search key value ( duplicates ) can lead to variable- sized data entries (if we use Alternative (3)).

Database Management Systems43 Summary Tree-structured indexes are ideal for range- searches, also good for equality searches. ISAM is a static structure. Only leaf pages modified; overflow pages needed. Overflow chains can degrade performance unless size of data set and data distribution stay constant. B+ tree is a dynamic structure. Inserts/deletes leave tree height-balanced; log F N cost. High fanout ( F ) means depth rarely more than 3 or 4. Almost always better than maintaining a sorted file.

Database Management Systems44 Summary (cont.) Typically, 67% occupancy on average. Usually preferable to ISAM, modulo locking considerations; adjusts to growth gracefully. If data entries are data records, splits can change rids! Key compression increases fan-out, reduces height. Bulk loading can be much faster than repeated inserts for creating a B+ tree on a large data set. Most widely used index in database management systems because of its versatility. One of the most optimized components of a DBMS.