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.

Slides:



Advertisements
Similar presentations
CpSc 3220 File and Database Processing Lecture 17 Indexed Files.
Advertisements

Quick Review of Apr 10 material B+-Tree File Organization –similar to B+-tree index –leaf nodes store records, not pointers to records stored in an original.
CM20145 Indexing and Hashing
CIS552Indexing and Hashing1 Cost estimation Basic Concepts Ordered Indices B + - Tree Index Files B - Tree Index Files Static Hashing Dynamic Hashing Comparison.
Index Basic Concepts Indexing mechanisms used to speed up access to desired data. E.g., author catalog in library Search Key - attribute to set of attributes.
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B+-Tree Index Files B-Tree.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 12: Indexing and.
Data Organization - B-trees. 11.2Database System Concepts A simple index Brighton A Downtown A Downtown A Mianus A Perry.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
Dr. Kalpakis CMSC 661, Principles of Database Systems Index Structures [13]
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. Indexing and Hashing Database Management Systems I Alex Coman, Winter 2006.
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.
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.
B+-tree and Hashing.
1 Indexing and Hashing Indexing and Hashing Basic Concepts Dense and Sparse Indices B+Trees, B-trees Dynamic Hashing Comparison of Ordered Indexing and.
B+-tree and Hash Indexes
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.
Database Management Systems I Alex Coman, Winter 2006
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Part B Part A:  Index Definition in SQL  Ordered Indices  Index Sequential.
Indexing and Hashing.
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.
Multimedia Information Systems CS Outlines Introduction to DMBS Relational database and SQL B + - tree index structure.
1 Indexing Structures for Files. 2 Basic Concepts  Indexing mechanisms used to speed up access to desired data without having to scan entire.
Primary Indexes Dense Indexes
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.
Ch12: Indexing and Hashing  Basic Concepts  Ordered Indices B+-Tree Index Files B+-Tree Index Files B-Tree Index Files B-Tree Index Files  Hashing Static.
1 CS 728 Advanced Database Systems Chapter 17 Database File Indexing Techniques, B- Trees, and B + -Trees.
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,
Indexing. Goals: Store large files Support multiple search keys Support efficient insert, delete, and range queries.
©Silberschatz, Korth and Sudarshan12.1Database System Concepts B + -Tree Index Files Indexing mechanisms used to speed up access to desired data.  E.g.,
Chapter 12: Indexing and Hashing
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B+-Tree Index Files B-Tree.
12.1 Chapter 12: Indexing and Hashing Spring 2009 Sections , , Problems , 12.7, 12.8, 12.13, 12.15,
Basic Concepts Indexing mechanisms used to speed up access to desired data. E.g., author catalog in library Search Key - attribute to set of attributes.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture17.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Tree-Structured Indexes.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Tree-Structured Indexes Chapter 9.
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.
Computing & Information Sciences Kansas State University Wednesday, 22 Oct 2008CIS 560: Database System Concepts Lecture 22 of 42 Wednesday, 22 October.
Indexing and Hashing By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan Chapter 12: Indexing and Hashing.
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.
Marwan Al-Namari Hassan Al-Mathami. Indexing What is Indexing? Indexing is a mechanisms. Why we need to use Indexing? We used indexing to speed up access.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 B+-Tree Index Chapter 10 Modified by Donghui Zhang Nov 9, 2005.
Indexing Database Management Systems. Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B + -Tree Index Files File Organization 2.
Indexing and B+-Trees By Kenneth Cheung CS 157B TR 07:30-08:45 Professor Lee.
Computing & Information Sciences Kansas State University Monday, 31 Mar 2008CIS 560: Database System Concepts Lecture 25 of 42 Monday, 31 March 2008 William.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 12: Indexing and.
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.
CS 405G: Introduction to Database Systems 12. Index.
Chapter 11 Indexing And Hashing (1) Yonsei University 1 st Semester, 2016 Sanghyun Park.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Tree-Structured Indexes Chapter 10.
Database Applications (15-415) DBMS Internals- Part III Lecture 13, March 06, 2016 Mohammad Hammoud.
CS 728 Advanced Database Systems Chapter 18
Azita Keshmiri CS 157B Ch 12 indexing and hashing
Tree-Structured Indexes
Tree Indices Chapter 11.
Extra: B+ Trees CS1: Java Programming Colorado State University
Chapter 11: Indexing and Hashing
Indexing and Hashing Basic Concepts Ordered Indices
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.
Chapter 11 Indexing And Hashing (1)
Credit for some of the slides in this lecture goes to
Tree-Structured Indexes
Presentation transcript:

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

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

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

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

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)

Indexed Sequential Access Method Data Page 1 Data Page 2 Data Page 3 :: Data Page N-1 Data Page N :: 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)

Indexed Sequential Access Method Data Page 1 Data Page 2 Data Page 3 :: Data Page N-1 Data Page N :: 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.

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 :: p p p p p p Index File Data Files

Indexed Sequential Access Method Data Page 1 Data Page 2 Data Page 3 :: Data Page N-1 Data Page N :: 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

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

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

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.

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:

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

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:

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– or greater or less or less

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.

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)

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

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.

2*3* Root *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

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!

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.

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

2*3* 19*20* *14*24*27*40*41* *7* 22* 28*45*77* Updates on B + -Trees: Insertion Insert 8 2*3* 19*20* *14*24*27*40*41* * 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…

Updates on B + -Trees: Insertion 2*3* 19*20* *14*24*27*40*41* * 28*45*77*5*7*8* 5 2*3* 19*20* *14*24*27*40*41* 22* 28*45*77*5*7*8* 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.

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

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

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

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.

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.

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”

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

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

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* *14*24*27*40*41* *7* 21* 28*45*77* 22* Insert 22

Bulk Loading

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