cs 728 advanced database systems chapter 18
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
1
CS 728 Advanced Database Systems
Chapter 18Database File Indexing Techniques, B-
Trees, and B+-Trees
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 <field value, pointer to record>, which is ordered by field value
The index is called an access path on the field.
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
Sparse Vs. Dense Indices
Sparse primaryindex sorted
on Id
Dense secondaryindex sorted
on Name
Ordered file sorted on Id
Id Name Dept
5
Sparse Vs. Dense Indices
Ashby, 25, 3000
Smith, 44, 3000
Ashby
Cass
Smith
22
25
30
40
44
44
50Sparse primaryindex on Name
Ordered file on NameDense secondary
index on Age
33
Bristow, 30, 2007
Basu, 33, 4003
Cass, 50, 5004
Tracy, 44, 5004
Daniels, 22, 6003
Jones, 40, 6003
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
Ordered File
2010
4030
6050
8070
10090
Sparse/Primary Index
10305070
90110130150
170190210230
Sparse Indices
8
Example 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
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
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
Primary Index on the Ordering Key Field
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
Clustering Indices
A clustering index on the DEPNo ordering nonkey field of an EMPLOYEE file.
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
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
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
Example of a Dense Secondary Index
A dense secondary index (with block pointers) on a nonordering KEY field.
18
Example of a Secondary Index
A dense secondary index (with record pointers) on a non-ordering non-key field.
19
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 Secondary index(NonKey)
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
A Two-Level
Primary Index
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
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
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
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
B-tree Structures
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
The Nodes of a B+-tree
30
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).
31
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
32
Sample Leaf Node
From non-leaf node
to next leafin sequence
57
81
95
To r
eco
rd
wit
h k
ey 5
7
To r
eco
rd
wit
h k
ey 8
1
To r
eco
rd
wit
h k
ey 9
5
33
Sample Non-Leaf Node
to keys to keys to keys to keys 57 57 k 81 81 k 95 95
57
81
95
34
Example of a B+-Tree
Root f=4
35
110
130
179
11
3 5 11
30
35
100
101
110
120
130
150
156
179
180
200
35
Number of pointers/keys for B+-Tree
Full node min. node
Non-leaf
Leaf
f=4
12
01
50
18
0
30
3 5 11
30
35
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
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.
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 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.
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
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.
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
43
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.
44
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.
45
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.
46
Example of a Deletion in a
B+-tree
47
Extra Reading
Read Examples 1 to 7.