administração e optimização de bases de dados - indexing · pdf...
TRANSCRIPT
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Administracao e Optimizacaode Bases de Dados
Indexing
Departamento de Engenharia InformaticaInstituto Superior Tecnico
2nd Semester2009/2010
Slides baseados nos slides oficiais do livro“Database System Concepts”c©Silberschatz, Korth and Sudarshan.
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Outline
1 Basic Concepts
2 B+-Tree Indices
3 Hash Indices
4 Indices on Multiple Keys
5 Bitmap Indices
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
Indexing
Types of Indices
Updating Indices
SecondaryIndices
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Outline
1 Basic ConceptsIndexingTypes of IndicesUpdating IndicesSecondary Indices
2 B+-Tree Indices
3 Hash Indices
4 Indices on Multiple Keys
5 Bitmap Indices
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
Indexing
Types of Indices
Updating Indices
SecondaryIndices
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Indexing
Indexing mechanisms are used to speed up access todesired data.
For instance, an author catalog in a library
In a database, they serve the same purpose
An index file consists of records (called index entries) ofthe form
search-key pointer
A search key is an attribute or set of attributes used tolook up records in a file
Index files are typically much smaller than the original file
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
Indexing
Types of Indices
Updating Indices
SecondaryIndices
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Index Evaluation Metrics
Indices are chosen according to:
Access types supported efficiently
records with a specified value in the attributeor records with an attribute value falling in a specifiedrange of values
Access time
Insertion time
Deletion time
Space overhead
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
Indexing
Types of Indices
Updating Indices
SecondaryIndices
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Types of Indices
Primary index: in a sequentially ordered file, the indexwhose search key specifies the sequential order of the file
Also called clustering indexThe search key of a primary index is usually but notnecessarily the primary key.Index-sequential file: ordered sequential file with a primaryindex
Secondary index: an index whose search key specifies anorder different from the sequential order of the file
Also called non-clustering index
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
Indexing
Types of Indices
Updating Indices
SecondaryIndices
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Dense Index Files
Dense index: index record appears for every search-keyvalue in the file
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
Indexing
Types of Indices
Updating Indices
SecondaryIndices
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Sparse Index Files
Sparse Index: contains index records for only somesearch-key values
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
Indexing
Types of Indices
Updating Indices
SecondaryIndices
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Sparse Index Files (cont.)
Applicable when records are sequentially ordered onsearch-key
To locate a record with search-key value K we:
Find index record with largest search-key value < KSearch file sequentially starting at the record to which theindex record points
Less space and less maintenance overhead for insertionsand deletions
Generally slower than dense index for locating records
Good tradeoff: sparse index with an index entry for everyblock in file, corresponding to least search-key value in theblock
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
Indexing
Types of Indices
Updating Indices
SecondaryIndices
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Multilevel Index
If primary index does not fit in memory, access becomesexpensive
To reduce number of disk accesses to index records, treatprimary index kept on disk as a sequential file andconstruct a sparse index on it
outer index - a sparse index of primary indexinner index - the primary index file
If even outer index is too large to fit in main memory, yetanother level of index can be created, and so on
Indices at all levels must be updated on insertion ordeletion from the file
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
Indexing
Types of Indices
Updating Indices
SecondaryIndices
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Multilevel Index (cont.)
Example of a multilevel index:
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
Indexing
Types of Indices
Updating Indices
SecondaryIndices
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Dense Indices: Insertion
Insertion procedure
1 Perform a lookup using the search-key value appearing inthe record to be inserted
2 If the search-key value does not appear in the index, insertit
3 Otherwise, if the index record stores a pointer to allrecords, insert the new pointer
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
Indexing
Types of Indices
Updating Indices
SecondaryIndices
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Sparse Indices: Insertion
Insertion procedure
1 Perform a lookup using the search-key value appearing inthe record to be inserted
2 If the index stores an entry for each block of the file, nochange needs to be made to the index unless a new blockis created
3 If a new block is created, the first search-key valueappearing in the new block is inserted into the index
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
Indexing
Types of Indices
Updating Indices
SecondaryIndices
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Dense Indices: Deletion
Deletion procedure
1 Perform a lookup using the search-key value appearing inthe record to be deleted
2 If deleted record was the only record in the file with itsparticular search-key value, the search-key is deleted fromthe index also
3 Otherwise:1 If the index record stores a pointer to all records, delete
the corresponding pointer2 Otherwise, if the deleted record was the first with the
search-key value, update the pointer to the next record
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
Indexing
Types of Indices
Updating Indices
SecondaryIndices
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Sparse Indices: Deletion
Deletion procedure
1 Perform a lookup using the search-key value appearing inthe record to be deleted
2 If an entry for the search key does not exist, do nothing3 Otherwise:
1 If deleted record was the only record in the file with itsparticular search-key value
1 Replace the index record with an index record for the nextsearch-key.
2 If the next search-key already has an index entry, deleteinstead of replacing
2 Otherwise, if the index record points to the deleted record,update pointer to the next record
Note: Multilevel insertion and deletion algorithms are simple extensions of
the single-level algorithms
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
Indexing
Types of Indices
Updating Indices
SecondaryIndices
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Secondary Indices
Frequently, one wants to find all the records whose valuesin a certain field (which is not the search-key of theprimary index) satisfy some condition
Example 1: In the account relation stored sequentially byaccount number, we may want to find all accounts in aparticular branchExample 2: as above, but where we want to find allaccounts with a specified balance or range of balances
We can have a secondary index with an index record foreach search-key value
Index record points to a bucket that contains pointers toall the actual records with that particular search-key value
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
Indexing
Types of Indices
Updating Indices
SecondaryIndices
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Secondary Indices (cont.)
Secondary index on balance field of account
Secondary indices have to be dense
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
Indexing
Types of Indices
Updating Indices
SecondaryIndices
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Primary and Secondary Indices
Indices offer substantial benefits when searching for records
When a file is modified, every index on the file must beupdated, Updating indices imposes overhead on databasemodification
Sequential scan using primary index is efficient, but asequential scan using a secondary index is expensive
each record access may fetch a new block from disk
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
Outline
1 Basic Concepts
2 B+-Tree IndicesDefinitionB+-Tree NodesQueries on B+-TreesUpdates on B+-TreesB+-Tree File Organization
3 Hash Indices
4 Indices on Multiple Keys
5 Bitmap Indices
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
B+-Tree Index Files
B+-tree indices are an alternative to indexed-sequential files
Disadvantage of indexed-sequential files:
Performance degrades as file growsPeriodic reorganization of entire file is required
Advantage of B+-tree index files:
Automatically reorganizes itself with small, local, changes,in the face of insertions and deletionsReorganization of entire file is not required to maintainperformance
Disadvantage of B+-trees: extra insertion and deletionoverhead, space overhead
Advantages of B+-trees outweigh disadvantages, and theyare used extensively
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
Structure of a B+-Tree
A B+-tree is a rooted tree satisfying the following properties:
All paths from root to leaf are of the same length
Each node that is not a root or a leaf has between n/2and n children
A leaf node has between (n − 1)/2 and n − 1 values
Special cases:
If the root is not a leaf, it has at least 2 childrenIf the root is a leaf (that is, there are no other nodes in thetree), it can have between 0 and n − 1 values
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
Example of a B+-Tree
B+-tree for account file (n = 3)
B+-tree for account file (n = 5)
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
B+-Tree Node Structure
Typical node:
P1 K1 P2 . . . Pn−1 Kn−1 Pn
Ki are the search-key valuesPi are pointers to children (for non-leaf nodes) or pointersto records or buckets of records (for leaf nodes)
The search-keys in a node are ordered
K1 < K2 < K3 < · · · < Kn−1
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
Leaf Nodes in B+-Trees
Properties of a leaf node:For i = 1, 2, . . . , n − 1, pointer Pi either points to a filerecord with search-key value Ki , or to a bucket of pointersto file records, each record having search-key value Ki
Only needs bucket structure if search-key does not form aprimary key
If Li , Lj are leaf nodes and i < j , Li ’s search-key values areless than Lj ’s search-key values
Pn points to next leaf node in search-key order
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
Non-Leaf Nodes in B+-Trees
Non leaf nodes form a multi-level sparse index on the leafnodes
For a non-leaf node with m pointers:
All the search-keys in the subtree to which P1 points areless than K1
For 2 ≤ i ≤ m − 1, all the search-keys in the subtree towhich Pi points have values greater than or equal to Ki−1
and less than Ki
All the search-keys in the subtree to which Pm points aregreater than Km−1
P1 K1 P2 . . . Pm−1 Km−1 Pm
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
Observations about B+-trees
Since the inter-node connections are done by pointers,“logically” close blocks need not be “physically” close
The non-leaf levels of the B+-tree form a hierarchy ofsparse indices
The B+-tree contains a relatively small number of levels(logarithmic in the size of the main file), thus searches canbe conducted efficiently
Insertions and deletions to the main file can be handledefficiently, as the index can be restructured in logarithmictime
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
Queries on B+-Trees
To find all records with a search-key value of k :1 Start with the root node
1 Examine the node for the smallest search-key value > k.2 If such a value exists (assume it is Ki ), then follow Pi to
the child node3 Otherwise follow Pm to the child node (k ≥ Km−1, where
there are m pointers in the node)
2 If the node reached by following the pointer above is not aleaf node, repeat step 1 on the node
3 Else we have reached a leaf node1 If for some i , key Ki = k follow pointer Pi to the desired
record or bucket2 Else no record with search-key value k exists
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
Queries on B+-Trees (cont.)
In processing a query, a path is traversed in the tree fromthe root to some leaf node
If there are K search-key values in the file, the path is nolonger than dlogdn/2e(K )eA node is generally the same size as a disk block, typically4 kilobytes, and n is typically around 100 (40 bytes perindex entry)
With 1 million search key values and n = 100, at mostlog50(1 000 000) = 4 nodes are accessed in a lookup
Contrast this with a balanced binary tree with 1 millionsearch key values—around 20 nodes are accessed in alookupThe above difference is significant since every node accessmay need a disk I/O
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
Insertion on a B+-Tree
Find the leaf node in which the search-key value wouldappear
If the search-key value is already there in the leaf node,record is added to file and if necessary a pointer is insertedinto the bucket.
If the search-key value is not there, then add the record tothe main file and create a bucket if necessary. Then:
If there is room in the leaf node, insert (key-value, pointer)pair in the leaf nodeOtherwise, split the node (along with the new (key-value,pointer) entry)
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
Splitting a Node
To split a node:1 take the n (search-key,pointer) pairs (including the one
being inserted) in sorted order2 place the first dn/2e in the original node, and the rest in a
new node3 let the new node be p, and let k be the smallest key value
in p4 Insert (k ,p) in the parent of the node being split5 If the parent is full, split it and propagate the split further
up
When splitting a non-leaf node, the dn/2e-th value ispushed up to the parent and deleted from the node
The splitting of nodes proceeds upwards till a node that isnot full is found
In the worst case the root node may be split increasing theheight of the tree by 1
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
Insertion Example
B+-Tree before and after insertion of Downtown
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
Insertion Example
B+-Tree before and after insertion of Clearview
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
Splitting a Node
Result of splitting node containing Brighton and Downtown oninserting Clearview
(example)
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
Deletion on a B+-Tree
1 Find the record to be deleted, and remove it from themain file and from the bucket (if present)
2 Remove (search-key value,pointer) from the leaf node ifthere is no bucket or if the bucket has become empty
3 If the node has too few entries due to the removal, andthe entries in the node and a sibling fit into a single node,then
1 Insert all the search-key values in the two nodes into asingle node and delete the other node.
2 Delete the pair (Ki−1,Pi ), where Pi is the pointer to thedeleted node, from its parent, recursively using the aboveprocedure.
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
Deletion on a B+-Tree(cont.)
If the node has too few entries due to the removal, but theentries in the node and a sibling do not fit into a singlenode, then
1 Redistribute the pointers between the node and a siblingsuch that both have more than the minimum number ofentries
2 Update the corresponding search-key value in the parent ofthe node
The node deletions may cascade upwards till a node whichhas dn/2e or more pointers is found
If the root node has only one pointer after deletion, it isdeleted and the sole child becomes the root
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
Deletion Example
B+-Tree before and after deletion of Downtown
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
Deletion Example (cont.)
B+-Tree before and after deletion of Perryridge
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
Deletion Example (cont.)
B+-Tree before and after deletion of Perryridge
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Definition
B+-Tree Nodes
Queries onB+-Trees
Updates onB+-Trees
B+-Tree FileOrganization
Hash Indices
Indices onMultiple Keys
BitmapIndices
B+-Tree File Organization
Index file degradation problem is solved by using B+-treeindices. Data file degradation problem is solved by usingB+-tree File Organization
The leaf nodes in a B+-tree file organization store records,instead of pointers
Insertion and deletion are handled in the same way asinsertion and deletion of entries in a B+-tree index
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
Outline
1 Basic Concepts
2 B+-Tree Indices
3 Hash IndicesStatic HashingDynamic HashingHashing vs. Ordered Indices
4 Indices on Multiple Keys
5 Bitmap Indices
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
Static Hashing
A bucket is a unit of storage containing one or morerecords (a bucket is typically a disk block)
In a hash file organization we obtain the bucket of a recorddirectly from its search-key value using a hash function
Hash function h is a function from the set of all search-keyvalues K to the set of all bucket addresses B
Hash function is used to locate records for access,insertion, and deletion
Records with different search-key values may be mappedto the same bucket; thus entire bucket has to be searchedsequentially to locate a record
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
Hash File Organization
Hash file organization of account file, using branch name as key
There are 10 buckets
The binary representation ofthe ith character is assumedto be the integer i
The hash function returnsthe sum of the binaryrepresentations of thecharacters modulo 10
E.g. h(Perryridge) = 5,h(Round Hill) = 3,h(Brighton) = 3
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
Hash Functions
Worst hash function maps all search-key values to thesame bucket
An ideal hash function is uniform: each bucket is assignedthe same number of search-key values from the set of allpossible values
An ideal hash function is random: each bucket will havethe same number of records assigned to it irrespective ofthe actual distribution of search-key values in the file
Typical hash functions perform computation on theinternal binary representation of the search-key.
For example, for a string search-key, the binaryrepresentations of all the characters in the string could beadded and the sum modulo the number of buckets couldbe returned
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
Handling of Bucket Overflows
Bucket overflow can occur because of
Insufficient bucketsSkew in distribution of records. This can occur due to tworeasons:
multiple records have same search-key valuechosen hash function produces non-uniform distribution ofkey values
Although the probability of bucket overflow can bereduced, it cannot be eliminated; it is handled by usingoverflow buckets
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
Handling of Bucket Overflows (cont.)
Overflow chaining - the overflow buckets of a given bucket arechained together in a linked list
Above scheme is called closed hashingAn alternative, called open hashing (linear probing), which doesnot use overflow buckets, is not suitable for databaseapplications
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
Hash Indices
Hashing can be used not only for file organization, butalso for index-structure creation
A hash index organizes the search keys, with theirassociated record pointers, into a hash file structure
Strictly speaking, hash indices are always secondary indices
if the file itself is organized using hashing, a separateprimary hash index on it using the same search-key isunnecessaryhowever, we use the term hash index to refer to bothsecondary index structures and hash organized files
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
Example of Hash Index
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
Deficiencies of Static Hashing
In static hashing, function h maps search-key values to afixed set of B of bucket addresses
Databases grow with time. If initial number of buckets istoo small, performance will degrade due to too muchoverflows
If file size at some point in the future is anticipated andnumber of buckets allocated accordingly, significantamount of space will be wasted initially
If database shrinks, again space will be wasted
One option is periodic re-organization of the file with anew hash function, but it is very expensive
These problems can be avoided by using techniques thatallow the number of buckets to be modified dynamically
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
Dynamic Hashing
Good for database that grows and shrinks in size
Allows the hash function to be modified dynamically
Extendable hashing — one form of dynamic hashing
Hash function generates values over a large range —typically b-bit integers, with b = 32At any time use only a prefix of the hash function to indexinto a table of bucket addressesLet the length of the prefix be i bits, 0 ≤ i ≤ 32Bucket address table size = 2i . Initially i = 0Value of i grows and shrinks as the size of the databasegrows and shrinksMultiple entries in the bucket address table may point to abucketThus, actual number of buckets is < 2i
The number of buckets also changes dynamically due tocoalescing and splitting of buckets
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
Extendable Hash Structure
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
Use of Extendable Hash Structure
Each bucket j stores a value ij ; all the entries that point tothe same bucket have the same values on the first ij bits
To locate the bucket containing search-key Kj :1 Compute h(Kj) = X2 Use the first i high order bits of X as a displacement into
bucket address table, and follow the pointer to appropriatebucket
To insert a record with search-key value Kj
follow same procedure as look-up and locate the bucket,say jIf there is room in the bucket j insert record in the bucketElse the bucket must be split and insertion re-attempted
Overflow buckets used instead in some cases
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
Updates in Extendable Hash Structure
To split a bucket j when inserting record with search-key valueKj :
If i = ij (only one pointer to bucket j)
increment i and double the size of the bucket address tablereplace each entry in the table by two entries that point tothe same bucketrecompute new bucket address table entry for Kj
Now i > ij so use the following case
If i > ij (more than one pointer to bucket j)
allocate a new bucket z , and set ij and iz to the old ij + 1make the second half of the bucket address table entriespointing to j to point to zremove and reinsert each record in bucket jrecompute new bucket for Kj and insert record in thebucket (further splitting is required if the bucket is still full)
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
Updates in Extendable Hash Structure (cont.)
When inserting a value, if the bucket is full after severalsplits (that is, i reaches some limit b) create an overflowbucket instead of splitting bucket entry table further
To delete a key value,
locate it in its bucket and remove itthe bucket itself can be removed if it becomes empty (withappropriate updates to the bucket address table)Coalescing of buckets can be done (can coalesce only witha “buddy” bucket having same value of ij and same ij − 1prefix, if it is present)Decreasing bucket address table size is also possible
Note: decreasing bucket address table size is an expensiveoperation and should be done only if number of bucketsbecomes much smaller than the size of the table
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
An Example
branch name h(branch name)
Brighton 0010 1101 1111 ...Downtown 1010 1100 1010 ...Mianus 1100 1001 1011 ...Perryridge 1111 1001 1011 ...Redwood 0011 1101 1001 ...Round Hill 1101 0001 0011 ...
Initial hash structure (bucket size = 2)
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
An Example (cont.)
Hash structure after insertion of one Brighton and twoDowntown records
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
An Example (cont.)
Hash structure after insertion of one Mianus record
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
An Example (cont.)
Hash structure after insertion of three Perryridge records
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
An Example (final)
Hash structure after insertion of Redwood and Round Hillrecords
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
Extendable Hashing vs. Other Schemes
Benefits of extendable hashing:
Hash performance does not degrade with growth of fileMinimal space overhead
Disadvantages of extendable hashing
Extra level of indirection to find desired recordBucket address table may itself become very big (largerthan memory)Changing size of bucket address table is an expensiveoperation
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Static Hashing
DynamicHashing
Hashing vs.Ordered Indices
Indices onMultiple Keys
BitmapIndices
Hashing vs. Ordered Indices
Several issues must be considered:
Cost of periodic re-organization
Relative frequency of insertions and deletions
Is it desirable to optimize average access time at theexpense of worst-case access time?
Expected type of queries:
Hashing is generally better at retrieving records having aspecified value of the keyIf range queries are common, ordered indices are to bepreferred
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Outline
1 Basic Concepts
2 B+-Tree Indices
3 Hash Indices
4 Indices on Multiple Keys
5 Bitmap Indices
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Multiple-Key Access
Use multiple indices for certain types of queries
Example
select account numberfrom accountwhere branch name = ”Perryridge” and balance = 1000
Possible strategies for processing query using indices onsingle attributes:
1 Use index on branch name to findbranch name = ”Perryridge”; test for balance = $1000
2 Use index on balance to find balance = $1000; test forbranch name = ”Perryridge”
3 Use branch name index to find pointers to all recordspertaining to the Perryridge branch; similarly, use index onbalance; take intersection of both sets of pointers obtained
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Indices on Multiple Keys
Composite search keys are search keys containing morethan one attribute
E.g. (branch name, balance)
Lexicographic ordering: (a1, a2) < (b1, b2) if either
a1 < b1, ora1 = b1 and a2 < b2
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Examples
With the where clause
where branch name = ”Perryridge” and balance = 1000
the index can be used to fetch only records that satisfyboth conditions
Using separate indices in less efficient — we may fetchmany records that satisfy only one of the conditions.
Can also efficiently handle
where branch name = ”Perryridge” and balance < 1000
But cannot efficiently handle
where branch name < ”Perryridge” and balance = 1000
May fetch many records that satisfy the first but not thesecond condition
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Outline
1 Basic Concepts
2 B+-Tree Indices
3 Hash Indices
4 Indices on Multiple Keys
5 Bitmap Indices
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Bitmap Indices
Special type of index designed for efficient querying onmultiple keys
Records in a relation are assumed to be numberedsequentially
Given a number n it must be easy to retrieve record nParticularly easy if records are of fixed size
Applicable on attributes that take on a relatively smallnumber of distinct values
E.g. gender, country, state, . . .E.g. income-level (income broken up into a small numberof levels such as 0–9999, 10000–19999, 20000–50000,50000–∞)
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Bitmap Index Structure
A bitmap is simply an array of bits
A bitmap index on an attribute has a bitmap for eachvalue of the attribute
Bitmap has as many bits as recordsIn a bitmap for value v , the bit for a record is 1 if therecord has the value v for the attribute, and is 0 otherwise
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Using Bitmap Indices
Bitmap indices are useful for queries on multiple attributesnot particularly useful for single attribute queries
Queries are answered using bitmap operationsIntersection (and)Union (or)Complementation (not)
Each operation takes two bitmaps of the same size andapplies the operation on corresponding bits to get theresult bitmap
Example
Males with income level L1:10010 AND 10100 = 10000
Can then retrieve required tuplesCounting number of matching tuples is even faster
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Size of Bitmap Indices
Bitmap indices generally very small compared with relationsize
E.g. if record is 100 bytes, space for a single bitmap is1/800 of space used by relation.If number of distinct attribute values is 8, bitmap is only1% of relation size
Deletion needs to be handled properly
Existence bitmap to note if there is a valid record at arecord locationNeeded for complementation
(bitmap operations) AND ExistenceBitmap
Should keep bitmaps for all values, even null value
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
Implementation of Bitmap Indices
Bitmaps are packed into words; we can compute 32 or 64bit operations at once
E.g. 1-million-bit maps can be andled with just 31, 250instructions
Counting number of 1s can be done fast
Bitmaps can be used instead of Tuple-ID lists at leaf levelsof B+-tree, for values that have a large number ofmatching records
Worthwhile if > 1/64 of the records have that value,assuming a tuple-id is 64 bitsMerges benefits of bitmap and B+-tree indices
Administracaoe Optimizacao
de Bases deDados
BasicConcepts
B+-TreeIndices
Hash Indices
Indices onMultiple Keys
BitmapIndices
End of Chapter 12