Transcript Indexing Structure for File
Chapter 18 Indexing Structures for Files
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
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.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
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 A
dense index
has an index entry for every search key value (and hence every record) in the data file. A
sparse (or nondense) index
, on the other hand, has index entries for only some of the search values Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Indexes as Access Paths (cont.)
Example: Given the following data file EMPLOYEE(NAME, SSN, ADDRESS, JOB, SAL, ... ) Suppose that: record size R=100 bytes records block size B=1024bytes r=30000 Then, we get: blocking factor Bfr= B div R= 1024 / 150= 10 records/block number of file blocks b= (r/Bfr)= (30000/10)= 3000 blocks For an index on the SSN field, assume the field size V SSN =9 bytes, assume the record pointer size P R =6 bytes. Then: index entry size R I =(V SSN + P R )=(9+6)=15 bytes index blocking factor Bfr I = B div R I = 1024 / 15= 68 entries/block number of index blocks b= (r/ Bfr I )= (30000/68)= 45 blocks binary search needs log 2 bI= log 245 = 6 block accesses We need one more block access to the data file. Total we need 7 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 3000= 12 block accesses Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Types of Single-Level Indexes
Primary Index 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.
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.
A major problem with a primary index —as with any ordered file —is insertion and deletion of records.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Primary Index on the Ordering Key Field
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Types of Single-Level Indexes
Clustering Index Defined on an ordered data file The data file is ordered on a which requires that the ordering field of the data file have a distinct value for each record.
non-key field
unlike primary index, Includes one index entry index entry points to the first data block that contains records with that field value.
for each distinct value
of the field; the A clustering index is another example of a nondense index because it has an entry for every distinct value of the indexing field Record insertion and deletion still cause problems because the data records are physically ordered. To alleviate the problem of insertion, it is common to reserve a whole block (or a cluster of contiguous blocks) for each value of the clustering field; all records with that value are placed in the block (or block cluster). This makes insertion and deletion relatively straightforward.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
A Clustering Index Example
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Another Clustering Index Example
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Types of Single-Level Indexes
Secondary Index A secondary index provides a secondary means of accessing a file for which some primary access already exists.
The secondary index may be on a field which is a candidate key and has a unique value in every record, or a non-key with duplicate values.
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.
There can be
many
secondary indexes (and hence, indexing fields) for the same file.
Includes one entry
for each record
is a
dense index
in the data file; hence, it Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Example of a Dense Secondary Index
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Example of a Secondary Index
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Properties of Index Types
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
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
the index to the index is called the
second-level index
.
and 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 The value bfr i is called the fan-out of the multilevel index, and usually is referred by the symbol fo.
Each level reduces the number of entries at the previous level by a factor of fo.
a multilevel index with r1 first-level entries will have approximately t levels, where t = ⎡ (log fo (r1)) ⎤ .
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
A Two-Level Primary Index
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
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
.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
A Node in a Search Tree with Pointers to Subtrees Below It
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
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 Copyright © 2011 Ramez Elmasri and Shamkant Navathe
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 Copyright © 2011 Ramez Elmasri and Shamkant Navathe
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 Copyright © 2011 Ramez Elmasri and Shamkant Navathe
B-Trees.
The B-tree is always balanced and that the space wasted by deletion, if any, never becomes excessive.
A B-tree of order p, when used as an access structure on a key field to search for records in a data file, can be defined as follows: 1 . Each internal node in the B-tree is of the form
2. Within each node, K1 < K2 < ... < Kq−1.
3. For all search key field values X in the subtree pointed at by Pi (the ith sub-tree) we have: Ki –1 < X < Ki for 1 < i < q; X < Ki for i = 1; and Ki–1 < X for i = q.
4. Each node has at most p tree pointers.
5. Each node, except the root and leaf nodes, has at least ⎡ (p/2) ⎤ has at least two tree pointers unless it is the only node in the tree.
tree pointers. The root node 6. A node with q tree pointers, q ≤ p, has q – 1 search key field values (and hence has q – 1 data pointers).
7. All leaf nodes are at the same level. Leaf nodes have the same structure as internal nodes except that all of their tree pointers Pi are N U LL.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
B-tree Structures
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
B+-Trees
The structure of the internal nodes of a B+-tree of order p is as follows: 1 . Each internal node is of the form
2. Within each internal node, K1 < K2 < ... < Kq−1.
3. For all search field values X in the subtree pointed at by Pi, we have K i−1 < X≤ K i for 1 < i < q; X ≤ K i for i = 1; and K i−1 < X for i = q 4. Each internal node has at most p tree pointers.
5. Each internal node, except the root, has at least ⎡ (p/2) ⎤ least two tree pointers if it is an internal node.
tree pointers. The root node has at 6. An internal node with q pointers, q ≤ p, has q − 1 search field values.
The structure of the leaf nodes of a B+-tree of order p is as follows: 1 . Each leaf node is of the form <
2. Within each leaf node, K1 ≤ K2 ... , Kq−1, q ≤ p.
3. Each Pri is a data pointer that points to the record whose search field value is Ki or to a file block containing the record (or to a block of record pointers that point to records whose search field value is Ki if the search field is not a key).
4. Each leaf node has at least ⎡ (p/2) ⎤ values.
5. All leaf nodes are at the same level.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The Nodes of a B+-tree
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Example B+ Tree
Search begins at root, and key comparisons direct it to a leaf.
Search for 5*, 15*, all data entries >= 24* ...
Root 13 17 24 30 2* 3* 5* 13* 14* 16* 17* 19* 20* 24* 25* 27* 30* 33* 34* 38* 39*
*
Based on the search for 15*, we know it is not in the tree!
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Inserting a Data Entry into a B+ Tree
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,
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.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Example of an Insertion in a B+-tree
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Deleting a Data Entry from a B+ Tree
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
sibling) from parent of
L
.
or Merge could propagate to root, decreasing height.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Example of a Deletion in a B+-tree
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
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.
Root Sorted pages of data entries; not yet in B+ tree 3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Bulk Loading (Contd.)
Index entries for leaf pages always entered into right most index page just above leaf level. When this fills up, it
3* 4*
splits. (Split may go up right-most path to the root.) Much faster than repeated inserts, especially when one considers locking!
6 6 6* 9* Root 10 10 12 10* 11* 12* 13* 20*22* 23* 31* 35*36* 38*41* 44* Root 12 20 20 23 23 35 35 38 Data entry pages not yet in B+ tree Data entry pages not yet in B+ tree 3* 4* 6* 9*
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
10* 11* 12* 13* 20*22* 23* 31* 35*36* 38*41* 44*
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.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe