Handout2 PDF
Handout2 PDF
Handout2 PDF
Artale (1)
Database 2
Lecture II
Alessandro Artale
Faculty of Computer Science – Free University of Bolzano
Room: 221
artale@inf.unibz.it
http://www.inf.unibz.it/ artale/
Summary of Lecture II
Indexing.
– Indexes on Sequential Files: Dense Vs. Sparse Indexes.
– Primary Indexes with Duplicate Keys.
– Secondary Indexes.
– Document Indexing.
– B-Tree Indexes.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (3)
Indexing
Example.
MovieStar(Name,Address,Gender,Birthdate)
SELECT *
FROM MovieStar
WHERE Name = ’Jim Carrey’;
All the blocks for the MovieStar relation should be inspected if there is no
index on Name.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (4)
Index
An Index takes as input a Search Key value and returns the address of the
record(s) (block physical address offset of the record) holding that value.
The Searck Key values stored in the Index are Sorted and a binary search can
be done on the Index.
Index Structures
3. B-Trees;
4. Hash Tables.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (6)
No one technique is the best. Each has to evaluated w.r.t. the following criteria:
Access Type. Finding records either with a particular search key, or with the
search key falling in a given range.
Access Time. The time it takes to find item(s) using the index in question.
Insertion Time. The time to insert an item in the data file, as well as the
time to update the index.
Deletion Time. The time to delete the item from the data file (which include
the time to find the item), and the time to update the index.
Summary
Indexing
– Indexes on Sequential Files: Dense Vs. Sparse Indexes.
– Primary Indexes with Duplicate Keys.
– Secondary Indexes.
– Document Indexing.
– B-Tree Indexes.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (8)
Index on Sequential File, also called Primary Index, when the Index is
associated to a Data File which is in turn sorted with respect to the search
key.
1. A Primary Index forces a sequential file organization on the Data File;
2. Since a Data File can have just one order there can be just one Primary
Index for Data File.
Usually used when the search key is also the primary key of the relation.
Algorithm for Lookup: Searching a data record with a given search key value.
Given a search key , the index is scanned and when is found the
associated pointer to the data file record is followed and the record (block
containing it) is read in main memory.
Dense indexes support also range queries: The minimum value is located
first, if needed, consecutive blocks are loaded in main memory until a search
key greater than the maximum value is found.
keys then at most steps are required to locate a given search key.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (11)
Sparse Indexes
Used when dense indexes are too large: A Sparse Index uses less space at the
expense of more time to find a record given a key.
A sparse index holds one key-pointer pair per data block, usually the first
record on the data block.
With respect to dense indexes we need to start two different binary searches:
the first on the sparse index, and the second on the retrieved data block.
Summary
Indexing
– Indexes on Sequential Files: Dense Vs. Sparse Indexes.
– Primary Indexes with Duplicate Keys.
– Secondary Indexes.
– Document Indexing.
– B-Tree Indexes.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (16)
As usual, the data file should be sorted w.r.t the search key to speak of
primary indexes.
Lookup. Find the search key on the index, read the pointed disk block, possibly
read successive blocks.
Advantages.
Efficient access of tuples with a given search key.
– Very few blocks should be read (also in case of duplicate keys);
– Range Queries – looking for search key values in a certain range – are
answered efficiently.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (20)
Disadvantages.
Expensive maintenance of the physical records storage to maintain the sorted
order.
– Technique used for insertion based on Overflow Blocks.
1. If there is space in the block insert the new record there in the right
place;
2. Otherwise, insert the new record in an Overflow Blocks. In order
to maintain the order, records are linked by means of pointers: The
pointer in each record points to the next record in search-key order.
– In general, performance degrades as far as the relation grows. The file is
reorganized when the system load is low.
– An optimal solution is to implement primary indexes as B-Tree structures
(presented soon).
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (21)
Summary
Indexing
– Indexes on Sequential Files: Dense Vs. Sparse Indexes.
– Primary Indexes with Duplicate Keys.
– Secondary Indexes.
– Document Indexing.
– B-Tree Indexes.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (23)
Secondary Indexes
A primary index is an index on a file sorted w.r.t. the search key. Then a
primary index “controls” the storage of records in the data file.
Indexes on Sequential files and Hash Tables are examples of primary indexes.
Since a file can have at most one physical order then it can have at most one
primary index.
Secondary indexes do not determine the placement of records in the data file.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (24)
Secondary indexes are sorted w.r.t. the search key Binary search.
The Data File IS NOT sorted w.r.t. the Secondary Index Search Key!
More than one data block may be needed for a given search key in general
more disk I/O to answer queries:
– Secondary Indexes are less efficient than Primary Indexes.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (26)
The example shows that 3 data blocks (i.e., 3 disk I/O) are needed to retrieve all
the tuples with search key
using the Secondary Index.
Summary
Indexing
– Indexes on Sequential Files: Dense Vs. Sparse Indexes.
– Primary Indexes with Duplicate Keys.
– Secondary Indexes.
– Document Indexing.
– B-Tree Indexes.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (29)
Problem: Given a set of text documents we need to retrieve that ones where a
particular word(s) occurs.
Given the success of the Web this has become an urgent database problem.
Each attribute has a Boolean value, eg, the value of cat is TRUE if and only
if the word cat appears in the document.
Pointers are stored in a bucket file and consider only the TRUE occurrences
of a search key.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (30)
Summary
Indexing
– Indexes on Sequential Files: Dense Vs. Sparse Indexes.
– Primary Indexes with Duplicate Keys.
– Secondary Indexes.
– Document Indexing.
– B-Tree Indexes.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (33)
B-Trees
Also used to index very-large relations when single-level indexes don’t fit in
main memory;
In the following we will present the structure of so called -Tree – the B
stands for Balanced Tree.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (34)
B-Trees (cont.)
B-tree is usually a 3 levels tree: the root, an intermediate level, the leaves.
All the leaves are at the same level Balanced Tree.
The size of each node of the B-tree is equal to a disk block. All nodes have
the same format: keys and pointers key-pointer pairs plus
extra pointer.
To keys To keys
To keys To keys
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (35)
B-Tree: An Example
Data file where search-keys are all the prime numbers from to .
All the keys appear once (in case of a dense index), and in sorted order at the
leaves.
A pointer points to either a file record (in case of a primary index structure)
or to a bucket of pointers (in case of a secondary index structure).
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (36)
Leaves
To next leaf
in sequence
One pointer to next leaf—used to chain leaf nodes in search-key order for
efficient sequential processing;
Interior Nodes
To keys To keys
To keys To keys
All pointers can be used to point to B-tree nodes of the inferior level;
(round up) pointers must be used, and one more pointer than
At least
key is used.
– Exception: the root may have only 2 children, then one key and two
pointers, regardless of how large is.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (38)
B-Trees as Indexes
B-Trees are useful for various types of indexes:
1. The search key is a primary or candidate key (i.e., no duplicates) of the data
file and the B-Tree is a dense index with a key-pointer pair in a leaf for
every record in the data file. The data file may or may not be sorted by
primary key (primary or secondary index).
2. The search key is not a key (i.e., duplicate values) and the data file is sorted
by this attribute. The B-Tree is a dense primary index with no duplicate
key-pointer pairs: just a single entry for each record in the data file with
search key , and pointers to the first record with search key .
3. The data file is sorted by search-key, and the B-Tree is a sparse primary
index with a key-pointer pair for each data block of the data file.
4. The search key is not a key (i.e., duplicate values) and the data file is NOT
sorted. The B-Tree is a secondary index with indirect bucket: No
duplicate key-pointer pairs, just a single entry for each record in the data file
with a given search key, and pointers to a bucket of pointers.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (39)
Lookup in B-trees
Problem: Given a B-tree (dense) index, find a record with search key .
Recursive search, starting at the root and ending at a leaf:
1. If we are at a leaf then if is among the keys of the leaf follow the associated
pointer to the data file, else fail.
then if then go to the first child, if then go to the
second child, and so on.
Note: B-Trees are useful for queries in which a range of values are asked for:
Range Queries.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (40)
B-Tree Updates
Insertion and Deletion are more complicated that lookup. It may be necessary to
either:
2. Merge nodes (i.e., combine nodes) if a node becomes too small as the result
of a deletion.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (41)
B-Tree Insertion
1. Start a search for the key being inserted. If there is room for another
key-pointer at the reached leaf, insert there;
2. If there is no room in the leaf, split the leaf in two and divide the keys
between the two new nodes (each node is at least half full);
3. The splitting of nodes implies that a new key-pointer pair has to be inserted
at the level above. If necessary the parent node will be split and we proceed
recursively up the tree (including the root).
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (42)
Let be a leaf whose capacity is keys, and we need to insert the
key-pointer pair.
1. Create a new sibling node , to the right of ;
2. The first key-pointer pairs remain with , while the other move to
.
3. The first key of the new node is also inserted at the parent node.
Let be an interior node whose capacity is keys and pointers, and
has been assigned the new pointer because of a node splitting at the
inferior level.
1. Create a new sibling node , to the right of ;
2. Leave at the first pointers, and move the other to
;
3. The first keys stay with , while the last keys move to . Since
there are keys there is one key in the middle (say it ) that doesn’t
is used by the common parent of and to distinguish the search
between those two nodes.
Leaf of insertion
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (45)
Example: Splitting the Leaf for Inserting Key 40
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (46)
Example: Splitting Interior Nodes
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (47)
B-Tree Deletion
Algorithm for deleting a search key in a B-Tree.
1. Start a search for the key being deleted;
2. Delete the record from the data file and the key-pointer pair from the leaf of
the B-tree;
3. If the lower limit of keys and pointers on a leaf is violated then two cases are
possible:
(a) Look for an adjacent sibling that is above lower limit and “steal” a
key-pointer pair from that leaf, keeping the order of keys intact. Make
sure keys for the parent are adjusted to reflect the new situation.
(b) Hard case: no adjacent sibling can provide an extra key. Then there must
be two adjacent siblings leaves, one at minimum, one below minimum
capacity. Just enough to merge nodes deleting one of them. Keys at
the parent should be adjusted, and then delete a key and a pointer. If
the parent is below the minimum capacity then we recursively apply the
deletion algorithm at the parent.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (48)
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (49)
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (50)
Efficiency of B-Trees
B-Trees allow lookup, insertion and deletion of records of very large relations
using few disk I/O.
and merging of nodes is rare.
of bytes, and a pointer be bytes . Suppose that a node
is occupied midway between the minimum ( ) and the maximum, then
each node has pointers Root children leaves
If the root is kept in main memory lookup requires disk I/O for traversing
the tree and disk I/O for accessing the record, if also second level in main
The actual records are stored in the leaf level of the B-Tree.
Insertion and deletion can cause either node splitting or merging – i.e., no
need for overflow blocks.
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (52)
(A-215,Mianus,700) (A-217,Brighton,750)
Free University of Bolzano–Database 2. Lecture II, 2003/2004 – A.Artale (53)
Summary of Lecture II
Indexing
– Indexes on Sequential Files: Dense Vs. Sparse Indexes.
– Primary Indexes with Duplicate Keys.
– Secondary Indexes.
– Document Indexing.
– B-Tree Indexes.