Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree...

63
Database System Concepts Basic Concepts B + -Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Database System Concepts Chapter 12: Indexing and Hashing Departamento de Engenharia Inform´ atica Instituto Superior T´ ecnico 1 st Semester 2007/2008 Slides (fortemente) baseados nos slides oficiais do livro “Database System Concepts” c Silberschatz, Korth and Sudarshan.

Transcript of Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree...

Page 1: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

Database System ConceptsChapter 12: Indexing and Hashing

Departamento de Engenharia InformaticaInstituto Superior Tecnico

1st Semester2007/2008

Slides (fortemente) baseados nos slides oficiais do livro“Database System Concepts”c©Silberschatz, Korth and Sudarshan.

Page 2: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

Outline

1 Basic Concepts

2 B+-Tree IndicesDefinitionB+-Tree NodesQueries on B+-TreesUpdates on B+-TreesB+-Tree File Organization

3 Hash IndicesStatic HashingDynamic Hashing

4 Indices on Multiple Keys

5 Indices and SQL

Page 3: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

Outline

1 Basic Concepts

2 B+-Tree Indices

3 Hash Indices

4 Indices on Multiple Keys

5 Indices and SQL

Page 4: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

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

Two basic kinds of indices:

Ordered indices: search keys are stored in sorted orderHash indices: search keys are distributed uniformly across“buckets” using a “hash function”

Page 5: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

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

Page 6: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

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.

Secondary index: an index whose search key specifies anorder different from the sequential order of the file

Also called non-clustering index

Index-sequential file: ordered sequential file with a primaryindex

Page 7: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

Dense Index Files

Dense index: index record appears for every search-keyvalue in the file

Page 8: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

Sparse Index Files

Sparse Index: contains index records for only somesearch-key values

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 < K

Search 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

Page 9: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

Sparse Index Files (cont.)

Example of a sparse index file:

Page 10: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

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

Page 11: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

Multilevel Index (cont.)

Example of a multilevel index:

Page 12: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

Index Update: Deletion

If deleted record was the only record in the file with itsparticular search-key value, the search-key is deleted fromthe index also

Single-level index deletion:

Dense indices: deletion of search-key is similar to filerecord deletionSparse indices:

if an entry for the search key exists in the index, it isdeleted by replacing the entry in the index with the nextsearch-key value in the file (in search-key order)If the next search-key value already has an index entry,the entry is deleted instead of being replaced

Page 13: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

Index Update: Insertion

Single-level index insertion:

Perform a lookup using the search-key value appearing inthe record to be insertedDense indices - if the search-key value does not appear inthe index, insert itSparse indices - if index stores an entry for each block ofthe file, no change needs to be made to the index unless anew block is created

If a new block is created, the first search-key valueappearing in the new block is inserted into the index

Multilevel insertion (as well as deletion) algorithms aresimple extensions of the single-level algorithms

Page 14: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

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

Page 15: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

Secondary Indices (cont.)

Secondary index on balance field of account

Secondary indices have to be dense

Page 16: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

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

Page 17: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

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 Indices and SQL

Page 18: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

B+-Tree Index Files

B+-tree indices are an alternative to indexed-sequential files

Disadvantage of indexed-sequential files:

Performance degrades as file grows, since many overflowblocks get createdPeriodic 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

Page 19: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

B+-Tree Index Files (cont.)

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

Page 20: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

Example of a B+-Tree

B+-tree for account file (n = 3)

B+-tree for account file (n = 5)

Page 21: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

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

Page 22: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

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

Page 23: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

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

Page 24: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

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

Page 25: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

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

Page 26: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

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 ⌈log⌈n/2⌉(K )⌉

A 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 free 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, costing around 20 milliseconds!

Page 27: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

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)

Page 28: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

Splitting a Node

To split a node:

take the n (search-key,pointer) pairs (including the onebeing inserted) in sorted orderplace the first ⌈n/2⌉ in the original node, and the rest in anew nodelet the new node be p, and let k be the smallest key valuein p

Insert (k,p) in the parent of the node being splitIf the parent is full, split it and propagate the split furtherup

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

Page 29: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

Insertion Example

B+-Tree before and after insertion of Clearview

Page 30: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

Splitting a Node

Result of splitting node containing Brighton and Downtown oninserting Clearview

(example)

Page 31: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

Deletion on a B+-Tree

Find the record to be deleted, and remove it from themain file and from the bucket (if present)

Remove (search-key value,pointer) from the leaf node ifthere is no bucket or if the bucket has become empty

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

Insert all the search-key values in the two nodes into asingle node and delete the other node.Delete the pair (Ki−1,Pi ), where Pi is the pointer to thedeleted node, from its parent, recursively using the aboveprocedure.

Page 32: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

Deletion on a B+-Tree(cont.)

If the parent node has too few entries due to the removal,but the entries in the parent and a sibling do not fit into asingle node, then

Redistribute the pointers between the node and a siblingsuch that both have more than the minimum number ofentriesUpdate the corresponding search-key value in the parent ofthe node

The node deletions may cascade upwards till a node whichhas ⌈n/2⌉ or more pointers is found

If the root node has only one pointer after deletion, it isdeleted and the sole child becomes the root

Page 33: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

Deletion Example

B+-Tree before and after deletion of Downtown

Page 34: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

Deletion Example (cont.)

B+-Tree before and after deletion of Perryridge

Page 35: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

Deletion Example (cont.)

B+-Tree before and after deletion of Perryridge

Page 36: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Definition

B+-Tree Nodes

Queries onB+-Trees

Updates onB+-Trees

B+-Tree FileOrganization

Hash Indices

Indices onMultiple Keys

Indices andSQL

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

Page 37: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

Outline

1 Basic Concepts

2 B+-Tree Indices

3 Hash IndicesStatic HashingDynamic Hashing

4 Indices on Multiple Keys

5 Indices and SQL

Page 38: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

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 as deletion

Records with different search-key values may be mappedto the same bucket; thus entire bucket has to be searchedsequentially to locate a record

Page 39: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

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

Page 40: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

Hash Functions

Worst hash function maps all search-key values to thesame bucket

An ideal hash function is uniform, i.e., each bucket isassigned the same number of search-key values from theset of all possible values

Ideal hash function is random, so 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

Page 41: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

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

Page 42: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

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

Page 43: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

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

Page 44: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

Example of Hash Index

Page 45: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

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

Page 46: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

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

Page 47: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

Extendable Hash Structure

Page 48: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

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 j

If 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

Page 49: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

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 z

remove and reinsert each record in bucket j

recompute new bucket for Kj and insert record in thebucket (further splitting is required if the bucket is still full)

Page 50: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

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

Page 51: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

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)

Page 52: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

An Example (cont.)

Hash structure after insertion of one Brighton and twoDowntown records

Page 53: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

An Example (cont.)

Hash structure after insertion of one Mianus record

Page 54: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

An Example (cont.)

Hash structure after insertion of three Perryridge records

Page 55: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

An Example (final)

Hash structure after insertion of Redwood and Round Hill

records

Page 56: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Static Hashing

DynamicHashing

Indices onMultiple Keys

Indices andSQL

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

Page 57: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

Outline

1 Basic Concepts

2 B+-Tree Indices

3 Hash Indices

4 Indices on Multiple Keys

5 Indices and SQL

Page 58: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

Multiple-Key Access

Use multiple indices for certain types of queries

Example

select account number

from account

where 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

Page 59: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

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

Page 60: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

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

Page 61: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

Outline

1 Basic Concepts

2 B+-Tree Indices

3 Hash Indices

4 Indices on Multiple Keys

5 Indices and SQL

Page 62: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

Index Definition in SQL

Create an index

create index <index-name> on <relation-name>(<attribute-list>)

E.g.: create index branch index on branch(branch name)

Use create unique index to indirectly specify and enforcethe condition that the search key is a candidate key

Not really required if SQL unique integrity constraint issupported

To drop an index

drop index <index-name>

Page 63: Database System Concepts - ULisboa · PDF fileDatabase System Concepts Basic Concepts B+-Tree Indices Hash Indices Indices on Multiple Keys Indices and SQL Outline 1 Basic Concepts

DatabaseSystem

Concepts

BasicConcepts

B+-TreeIndices

Hash Indices

Indices onMultiple Keys

Indices andSQL

End of Chapter 12