Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 728 Advanced Database Systems Chapter 17 Database File Indexing Techniques, B- Trees, and B + -Trees.

Similar presentations


Presentation on theme: "1 CS 728 Advanced Database Systems Chapter 17 Database File Indexing Techniques, B- Trees, and B + -Trees."— Presentation transcript:

1 1 CS 728 Advanced Database Systems Chapter 17 Database File Indexing Techniques, B- Trees, and B + -Trees

2 2 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, which is ordered by field value  The index is called an access path on the field.

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

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

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

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

7 7 Ordered File 20 10 40 30 60 50 80 70 100 90 Sparse/Primary Index 10 30 50 70 90 110 130 150 170 190 210 230 Sparse Indices

8 8 Example  Given the following data file EMPLOYEE(NAME, SSN, ADDRESS, SAL)  Suppose that:  record size R=150 bytesblock 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 V SSN =9 bytes, assume the record pointer size P R =7 bytes. Then:  index entry size R I =(V SSN + P R )=(9+7)=16 bytes  index blocking factor Bfr I = B/R I = 512/16= 32 entries/block  number of index blocks b= (r/Bfr I )=(30000/32)=938 blocks  binary search needs log 2 bI= log 2 938= 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:  log 2 b= log 2 30000= 15 block accesses

9 9 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.  The index has the same ordering as the one of the 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.

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

11 11 Primary Index on the Ordering Key Field

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

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

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

15 15 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

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

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

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

19 19 Index Types and Indexing Fields Data file ordered by indexing field Data file not ordered by indexing field Indexing field is keyPrimary IndexSecondary index (Key) Indexing field is nonkeyClustering IndexSecondary index (NonKey)

20 20 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

21 21 A Two-Level Primary Index

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

23 23

24 24 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

25 25 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

26 26 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

27 27 B-tree Structures

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

29 29 The Nodes of a B + -tree

30 30 B + -Tree Non-leaf Node Structure  K i are the search-key values, K 1  K 2  K 3  …  K f-1  all keys in the subtree to which P 1 points are  K 1.  all keys in the subtree to which P f points are  K f-1.  for 2  i  f-1, all keys in the subtree to which P i points have values  K i-1 and  K i.  P i are pointers to children nodes (tree nodes).

31 31 B + -Tree Leaf Node Structure  for i = 1, 2, …, f-1, pointer Pr i is a data pointer, that either points to a  file record with search-key value K i, or  block of record pointers that point to records having search-key value K i. (if search-key is not a key)  P next points to next leaf node in search-key order.  Within each leaf node, K 1  K 2  K 3  …  K f-1  If L i, L j are leaf nodes and i  j, then  L i ’s search-key values  L j ’s search-key values

32 32 Sample Leaf Node 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

33 33 Sample Non-Leaf Node to keysto keysto keys to keys  5757  k  8181  k  95  95 57 81 95

34 34 Example of a B + -Tree Rootf=4 35 110 130 179 11 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200

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

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

37 37 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  K i, 1  i  f-1  call Search(a, P i ),  elsecall Search(a, P f ).  Else //T is a leaf node  if no value in T equals a, report not found.  else if K i in T equals a, follow pointer Pr i to read the record/block.

38 38 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 log 50 (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.

39 39 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

40 40 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 P j, where j =  (f+1)/2  are kept, while the j th search value is moved to the parent, no replicated.  A new internal node will hold the entries from P j+1 to the end of the entries in the node.

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

42 42 Insertion – Example 3  Insert key = 31 f=4 3 5 11 30 32 11 32 31

43 43 Insertion – Example 3  Insert key = 7 f=4 3 5 11 30 31 11 31 3535 7 5

44 44 Insertion – Example 3  Insert key = 160 f=4 100 120 140 179 150 156 179 180 200 156 179 160 179

45 45 Insertion – Example 3  New root, insert 45 f=4 3 12 25 123123 10 12 20 25 30 32 40 45 3225 new root

46 46

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

48 48 B + -Trees: Deletion  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.  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.

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

50 50 Merge with Sibling  Delete 45 10 40 50 20 40 45 50 f=4 50

51 51 10 35 50 15 30 35 40 50 35 30 f=4 Redistribute Keys  Delete 40

52 52 40 45 30 37 25 26 20 22 10 14 1313 3 1426 37 30 22 new root f=4 Non-leaf Merging  Delete 37 30

53 53 Example of a Deletion in a B + -tree

54 54 Extra Reading  Read Examples 1 to 7.


Download ppt "1 CS 728 Advanced Database Systems Chapter 17 Database File Indexing Techniques, B- Trees, and B + -Trees."

Similar presentations


Ads by Google