CS 728 Advanced Database Systems Chapter 18

Slides:



Advertisements
Similar presentations
Disk Storage, Basic File Structures, and Hashing
Advertisements

Chapter 7 Indexing Structures for Files Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
CpSc 3220 File and Database Processing Lecture 17 Indexed Files.
B+-Trees and Hashing Techniques for Storage and Index Structures
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 12: Indexing and.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Chapter 14 Indexing Structures for Files Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
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
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.
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.
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Part B Part A:  Index Definition in SQL  Ordered Indices  Index Sequential.
1 Indexing Structures for Files. 2 Basic Concepts  Indexing mechanisms used to speed up access to desired data without having to scan entire.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Indexing Structures for Files.
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.
1 CS 728 Advanced Database Systems Chapter 17 Database File Indexing Techniques, B- Trees, and B + -Trees.
CS4432: Database Systems II
Indexing dww-database System.
Chapter 61 Chapter 6 Index Structures for Files. Chapter 62 Indexes Indexes are additional auxiliary access structures with typically provide either faster.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 File Organizations and Indexing Chapter 5, 6 of Elmasri “ How index-learning turns no student.
Chapter 14-1 Chapter Outline Types of Single-level Ordered Indexes –Primary Indexes –Clustering Indexes –Secondary Indexes Multilevel Indexes Dynamic Multilevel.
Index Structures for Files Indexes speed up the retrieval of records under certain search conditions Indexes called secondary access paths do not affect.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
1 Index Structures. 2 Chapter : Objectives Types of Single-level Ordered Indexes Primary Indexes Clustering Indexes Secondary Indexes Multilevel Indexes.
METU Department of Computer Eng Ceng 302 Introduction to DBMS Indexing Structures for Files by Pinar Senkul resources: mostly froom Elmasri, Navathe and.
Chapter 9 Disk Storage and Indexing Structures for Files Copyright © 2004 Pearson Education, Inc.
Indexing Structures for Files
1 Chapter 2 Indexing Structures for Files Adapted from the slides of “Fundamentals of Database Systems” (Elmasri et al., 2003)
12.1 Chapter 12: Indexing and Hashing Spring 2009 Sections , , Problems , 12.7, 12.8, 12.13, 12.15,
Nimesh Shah (nimesh.s) , Amit Bhawnani (amit.b)
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture17.
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.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 13 Disk Storage, Basic File Structures, 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.
Indexing Database Management Systems. Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B + -Tree Index Files File Organization 2.
Chapter 6 Index Structures for Files 1 Indexes as Access Paths 2 Types of Single-level Indexes 2.1Primary Indexes 2.2Clustering Indexes 2.3Secondary Indexes.
Indexing and B+-Trees By Kenneth Cheung CS 157B TR 07:30-08:45 Professor Lee.
Chapter 14 Indexing Structures for Files Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Indexing Structures for Files.
Indexing Structures Database System Implementation CSE 507 Some slides adapted from R. Elmasri and S. Navathe, Fundamentals of Database Systems, Sixth.
10/3/2017 Chapter 6 Index Structures.
Indexing Structures for Files
Indexing Structures for Files
Chapter Outline Indexes as additional auxiliary access structure
Indexing Structures for Files
Chapter # 14 Indexing Structures for Files
Indexing and hashing.
Multiway Search Trees Data may not fit into main memory
Azita Keshmiri CS 157B Ch 12 indexing and hashing
Tree Indices Chapter 11.
Database System Implementation CSE 507
Lecture 20: Indexing Structures
Extra: B+ Trees CS1: Java Programming Colorado State University
B+ Tree.
Chapter 11: Indexing and Hashing
11/14/2018.
Advance Database System
Chapter 11 Indexing And Hashing (1)
Indexing 1.
Storage and Indexing.
General External Merge Sort
Indexing February 28th, 2003 Lecture 20.
CS4433 Database Systems Indexing.
Indexing Structures for Files
Advance Database System
8/31/2019.
Lec 6 Indexing Structures for Files
Presentation transcript:

CS 728 Advanced Database Systems Chapter 18 Database File Indexing Techniques, B-Trees, and B+-Trees

Indexes as Access Paths A single-level index is an auxiliary file that makes it more efficient to search for a record in the data file. The index is usually specified on one field of the file (although it could be specified on several fields) One form of an index is a file of entries <field value, pointer to record>, which is ordered by field value The index is called an access path on the field.

Indexes as Access Paths (cont.) The index file usually occupies considerably less disk blocks than the data file because its entries are much smaller A binary search on the index yields a pointer to the file record Indexes can also be characterized as dense or sparse dense index has index entry for every record in the file. sparse (nondense) index has index entries for only some of the search-key values.

Sparse Vs. Dense Indices Id Name Dept Sparse primary index sorted on Id Ordered file sorted on Id Dense secondary index sorted on Name

Sparse Vs. Dense Indices Ashby, 25, 3000 22 Basu, 33, 4003 25 Bristow, 30, 2007 30 Ashby 33 Cass Cass, 50, 5004 Smith Daniels, 22, 6003 40 Jones, 40, 6003 44 Sparse primary index on Name 44 Smith, 44, 3000 50 Tracy, 44, 5004 Dense secondary index on Age Ordered file on Name 13

Sparse Indices Sparse index contains index records for only some search-key values. Some keys in the data file will not have an entry in the index file Applicable when records are sequentially ordered on search-key (ordered files) Normally keeps only one key per data block Less space (can keep more of index in memory) Less maintenance overhead for insertions and deletions.

Sparse Indices What happens when record 35 is inserted? Ordered File 20 10 40 30 60 50 80 70 100 90 Sparse/Primary Index 110 130 150 170 190 210 230 Normally keeps only one key per data block A sparse index uses less space at the expense of somewhat more time to find a record given its key What happens when record 35 is inserted?

Example record size R=150 bytes block size B=512 bytes r=30000 records Given the following data file EMPLOYEE(NAME, SSN, ADDRESS, SAL) Suppose that: record size R=150 bytes block size B=512 bytes r=30000 records blocking factor Bfr= B/R=512/150=3 records/block number of file blocks b=(r/Bfr)=(30000/3)=10000 blocks For an index on the SSN field, assume the field size VSSN=9 bytes, assume the record pointer size PR=7 bytes. Then: index entry size RI=(VSSN+ PR)=(9+7)=16 bytes index blocking factor BfrI= B/RI= 512/16= 32 entries/block number of index blocks b= (r/BfrI)=(30000/32)=938 blocks binary search needs log2bI= log2938= 10 block accesses This is compared to an average linear search cost of: (b/2)= 30000/2= 15000 block accesses If the file records are ordered, the binary search cost would be: log2b= log230000= 15 block accesses

Types of Single-Level Indexes primary index: is specified on the ordering key field of an ordered file, where every record has a unique value for that field. The index has the same ordering as the one of the file. clustering index: is specified on the ordering field of an ordered file. An ordered file can have at most one primary index or one clustering index, but not both. secondary index: is specified on any nonordering field of the file. The index has different ordering than the one of the file. A file can have several secondary indices in addition to its primary/clustering index. 11

Primary Indices Defined on an ordered data file The data file is ordered on a key field Includes one index entry for each block in the data file; the index entry has the key field value for the first record in the block, which is called the block anchor A similar scheme can use the last record in a block. Finding a record is efficient – do a binary search A primary index is a nondense (sparse) index, since it includes an entry for each disk block of the data file and the keys of its anchor record rather than for every search value.

Primary Index on the Ordering Key Field

Clustering Indices Defined on an ordered data file The data file is ordered on a non-key field unlike primary index, which requires that the ordering field of the data file have a distinct value for each record. Includes one index entry for each distinct value of the clustering field (rather than for every record). Sparse index (nondense) The index entry points to the first data block that contains records with that field value. A file can have at most one primary index or one clustering index, but not both.

Clustering Indices A clustering index on the DEPNo ordering nonkey field of an EMPLOYEE file.

Clustering Indices Clustering index with a separate block cluster for each group of records that share the same value for the clustering field.

Secondary Indices Secondary index: is specified on any nonordering field of the file. Non-ordering field can be a key (unique) or a non-key (duplicates) The index has different ordering than the one of the file. A file can have several secondary indices in addition to its primary index. there is one index entry for each data record index record points either to the block in which the record is stored, or to the record itself Hence, such an index is dense

Secondary Indices A secondary index usually needs more storage space and longer search time than does a primary index. It has larger number of entries. Sequential scan using primary index is efficient, but a sequential scan using a secondary index is expensive each record access may fetch a new block from disk The index is an ordered file with two fields. The first field is of the same data type as some non-ordering field of the data file that is an indexing field. The second field is either a block pointer or a record pointer.

Example of a Dense Secondary Index A dense secondary index (with block pointers) on a nonordering KEY field.

Example of a Secondary Index A dense secondary index (with record pointers) on a non-ordering non-key field.

Index Types and Indexing Fields Data file ordered by indexing field Data file not ordered by indexing field Indexing field is key Primary Index Secondary index (Key) Indexing field is nonkey Clustering Index (NonKey)

Multi-Level Indexes Because a single-level index is an ordered file, we can create a primary index to the index itself; In this case, the original index file is called the first-level index and the index to the index is called the second-level index. We can repeat the process, creating a third, fourth, ..., top level until all entries of the top level fit in one disk block A multi-level index can be created for any type of first-level index (primary, secondary, clustering) as long as the first-level index consists of more than one disk block

A Two-Level Primary Index

Multi-Level Indexes Such a multi-level index is a form of search tree However, insertion and deletion of new index entries is a severe problem because every level of the index is an ordered file. A Node in a search tree with pointers to subtrees below it.

Dynamic Multilevel Indexes Using B-Trees and B+-Trees Most multi-level indexes use B-tree or B+-tree data structures because of the insertion and deletion problem This leaves space in each tree node (disk block) to allow for new index entries These data structures are variations of search trees that allow efficient insertion and deletion of new search values. In B-Tree and B+-Tree data structures, each node corresponds to a disk block Each node is kept between half-full and completely full

Dynamic Multilevel Indexes Using B-Trees and B+-Trees (cont.) An insertion into a node that is not full is quite efficient If a node is full the insertion causes a split into two nodes Splitting may propagate to other tree levels A deletion is quite efficient if a node does not become less than half full If a deletion causes a node to become less than half full, it must be merged with neighboring nodes

Difference between B-tree and B+-tree In a B-tree, pointers to data records exist at all levels of the tree In a B+-tree, all pointers to data records exists at the leaf-level nodes A B+-tree can have less levels (or higher capacity of search values) than the corresponding B-tree

B-tree Structures

B+-Tree Index A B+-tree, of order f (fan-out --- maximum node capacity), is a rooted tree satisfying the following: All paths from root to leaf are of the same length (balanced tree) Each non-leaf node (except the root) has between f/2 and up to f tree pointers (f-1 key values). A leaf node has between f/2 and f-1 data pointers (plus a pointer for sibling node). 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 f-1 values.

The Nodes of a B+-tree

B+-Tree Non-leaf Node Structure Ki are the search-key values, K1  K2  K3  …  Kf-1 all keys in the subtree to which P1 points are  K1. all keys in the subtree to which Pf points are  Kf-1. for 2  i  f-1, all keys in the subtree to which Pi points have values  Ki-1 and  Ki. Pi are pointers to children nodes (tree nodes).

B+-Tree Leaf Node Structure for i = 1, 2, …, f-1, pointer Pri is a data pointer, that either points to a file record with search-key value Ki, or block of record pointers that point to records having search-key value Ki. (if search-key is not a key) Pnext points to next leaf node in search-key order. Within each leaf node, K1  K2  K3  …  Kf-1 If Li, Lj are leaf nodes and i  j, then Li’s search-key values  Lj’s search-key values

Sample Leaf Node 57 81 95 From non-leaf node to next leaf in sequence with key 57 with key 81 To record with key 95

Sample Non-Leaf Node 57 81 95 to keys to keys to keys to keys  57 57  k  81 81  k  95  95

Example of a B+-Tree Root f=4 35 110 130 179 11 3 5 11 120 130 180 200 100 101 110 150 156 179

Number of pointers/keys for B+-Tree Full node min. node Non-leaf Leaf 120 150 180 30 3 5 11 30 35

Observations about B+-Trees In a B+-tree, data pointers are stored only at the leaf nodes of the tree hence, the structure of leaf nodes differs from the structure of internal nodes. The leaf nodes have an entry for every value of the search field, along with a data pointer to the record. Some search field values from the leaf nodes are repeated in the internal nodes.

B+-Trees: Search Let a be a search key value and T the pointer to the root of the tree that has f pointer. Search(a, T) If T is non-leaf node: for the first i that satisfy a  Ki, 1  i  f-1 call Search(a, Pi), else call Search(a, Pf). Else //T is a leaf node if no value in T equals a, report not found. else if Ki in T equals a, follow pointer Pri to read the record/block.

B+-Trees: Search In processing a query, a path is traversed in the tree from the root to some leaf node. If there are n search-key values in the file, the path is no longer than log f/2(n) (worst case). With 1 million search key values and f = 100, at most log50(1000000) = 4 nodes are accessed in a lookup. Contrast this with a balanced binary tree with 1 million search key values -- around 20 nodes are accessed in a lookup.

B+-Trees: Insertion Find the leaf node in which the search-key value would appear If the search-key value is found in the leaf node, add the record to main file and if necessary add to the block a pointer to the record If the search-key value is not there, add the record to the main file and 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

B+-Trees: Insertion Splitting a node: take the f (search-key value, pointer) pairs (including the one being inserted) in sorted order. place the first (f+1)/2 in the original node x, and the rest in a new node y. let k be the largest key value in x. insert (k, y) in the parent node in their correct sequence. If the parent is full the entries in the parent node up to Pj, where j = (f+1)/2 are kept, while the jth search value is moved to the parent, no replicated. A new internal node will hold the entries from Pj+1 to the end of the entries in the node.

B+-Trees: Insertion 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.

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

B+-Trees: Deletion Delete the pair (Ki-1, Pi), where Pi is the pointer to the deleted node, from its parent, recursively using the above procedure. Otherwise, if the node has too few entries due to the removal, and the entries in the node and a sibling DO NOT 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.

B+-Trees: Deletion The node deletions may cascade upwards till a node which has f/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.

Example of a Deletion in a B+-tree 23

Extra Reading Read Examples 1 to 7.