Ch12 Indexing (1)

  • Upload
    432e

  • View
    222

  • Download
    0

Embed Size (px)

Citation preview

  • 8/13/2019 Ch12 Indexing (1)

    1/63

    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

    Semester2008/2009

    Slides (fortemente) baseados nos slides ociais do livroDatabase System Conceptsc Silberschatz, Korth and Sudarshan.

  • 8/13/2019 Ch12 Indexing (1)

    2/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Outline

    1 Basic Concepts

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

    3 Hash Indices

    Static HashingDynamic Hashing

    4 Indices on Multiple Keys

    5 Indices and SQL

  • 8/13/2019 Ch12 Indexing (1)

    3/63

    Database

    SystemConcepts

    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

  • 8/13/2019 Ch12 Indexing (1)

    4/63

    Database

    SystemConcepts

    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 le consists of records (called index entries) of the formsearch-key pointer

    A search key is an attribute or set of attributes used tolook up records in a leIndex les are typically much smaller than the original leTwo 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

  • 8/13/2019 Ch12 Indexing (1)

    5/63

    Database

    SystemConcepts

    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 specied value in the attributeor records with an attribute value falling in a speciedrange of values

    Access timeInsertion time

    Deletion timeSpace overhead

  • 8/13/2019 Ch12 Indexing (1)

    6/63

    Database

    SystemConcepts

    BasicConcepts

    B+ -TreeIndices

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Types of Indices

    Primary index: in a sequentially ordered le, the indexwhose search key species the sequential order of the le

    Also called clustering indexThe search key of a primary index is usually but notnecessarily the primary key.

    Secondary index: an index whose search key species anorder different from the sequential order of the le

    Also called non-clustering index

    Index-sequential le: ordered sequential le with a primaryindex

  • 8/13/2019 Ch12 Indexing (1)

    7/63

    Database

    SystemConcepts

    BasicConcepts

    B+ -TreeIndices

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Dense Index Files

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

  • 8/13/2019 Ch12 Indexing (1)

    8/63

    Database

    SystemConcepts

    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 le sequentially starting at the record to which theindex record points

    Less space and less maintenance overhead for insertions

    and deletionsGenerally slower than dense index for locating recordsGood tradeoff: sparse index with an index entry for everyblock in le, corresponding to least search-key value in theblock

  • 8/13/2019 Ch12 Indexing (1)

    9/63

    Database

    SystemConcepts

    BasicConcepts

    B+ -TreeIndices

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Sparse Index Files (cont.)

    Example of a sparse index le:

  • 8/13/2019 Ch12 Indexing (1)

    10/63

    Database

    SystemConcepts

    BasicConcepts

    B+ -TreeIndices

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Multilevel Index

    If primary index does not t in memory, access becomesexpensiveTo reduce number of disk accesses to index records, treat

    primary index kept on disk as a sequential le andconstruct a sparse index on itouter index - a sparse index of primary indexinner index - the primary index le

    If even outer index is too large to t in main memory, yetanother level of index can be created, and so onIndices at all levels must be updated on insertion ordeletion from the le

  • 8/13/2019 Ch12 Indexing (1)

    11/63

    Database

    SystemConcepts

    BasicConcepts

    B+ -TreeIndices

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Multilevel Index (cont.)

    Example of a multilevel index:

  • 8/13/2019 Ch12 Indexing (1)

    12/63

    Database

    SystemConcepts

    BasicConcepts

    B+ -TreeIndices

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Index Update: Deletion

    If deleted record was the only record in the le 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 lerecord deletionSparse indices:

    if an entry for the search key exists in the index, it is

    deleted by replacing the entry in the index with the nextsearch-key value in the le (in search-key order)If the next search-key value already has an index entry,the entry is deleted instead of being replaced

  • 8/13/2019 Ch12 Indexing (1)

    13/63

    Database

    SystemConcepts

    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 of the le, no change needs to be made to the index unless anew block is created

    If a new block is created, the rst search-key value

    appearing in the new block is inserted into the indexMultilevel insertion (as well as deletion) algorithms aresimple extensions of the single-level algorithms

  • 8/13/2019 Ch12 Indexing (1)

    14/63

    Database

    SystemConcepts

    BasicConcepts

    B+ -TreeIndices

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Secondary Indices

    Frequently, one wants to nd all the records whose valuesin a certain eld (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 nd all accounts in aparticular branchExample 2: as above, but where we want to nd allaccounts with a specied 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

  • 8/13/2019 Ch12 Indexing (1)

    15/63

    Database

    SystemConcepts

    BasicConcepts

    B+ -TreeIndices

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Secondary Indices (cont.)

    Secondary index on balance eld of account

    Secondary indices have to be dense

  • 8/13/2019 Ch12 Indexing (1)

    16/63

    Database

    SystemConcepts

    BasicConcepts

    B+ -TreeIndices

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Primary and Secondary Indices

    Indices offer substantial benets when searching for records

    When a le is modied, every index on the le must beupdated, Updating indices imposes overhead on databasemodicationSequential 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

  • 8/13/2019 Ch12 Indexing (1)

    17/63

    Database

    SystemConcepts

    BasicConcepts

    B+ -TreeIndicesDenitionB+ -Tree NodesQueries onB+ -TreesUpdates onB+ -TreesB+ -Tree FileOrganization

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Outline

    1 Basic Concepts

    2 B+ -Tree IndicesDenition

    B+

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

    3 Hash Indices

    4 Indices on Multiple Keys

    5 Indices and SQL

  • 8/13/2019 Ch12 Indexing (1)

    18/63

    Database

    SystemConcepts

    BasicConcepts

    B+ -TreeIndicesDenitionB+ -Tree NodesQueries onB+ -TreesUpdates onB+ -TreesB+ -Tree FileOrganization

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    B+ -Tree Index Files

    B+ -tree indices are an alternative to indexed-sequential lesDisadvantage of indexed-sequential les:

    Performance degrades as le grows, since many overowblocks get createdPeriodic reorganization of entire le is required

    Advantage of B+ -tree index les:Automatically reorganizes itself with small, local, changes,in the face of insertions and deletionsReorganization of entire le is not required to maintainperformance

    Disadvantage of B+ -trees: extra insertion and deletionoverhead, space overheadAdvantages of B+ -trees outweigh disadvantages, and theyare used extensively

  • 8/13/2019 Ch12 Indexing (1)

    19/63

    Database

    SystemConcepts

    BasicConcepts

    B+ -TreeIndicesDenitionB+ -Tree NodesQueries onB+ -TreesUpdates onB+ -TreesB+ -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 childrenA leaf node has between (n 1)/ 2 and n 1 valuesSpecial 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

  • 8/13/2019 Ch12 Indexing (1)

    20/63

    Database

    SystemConcepts

    BasicConcepts

    B+ -TreeIndicesDenitionB+ -Tree NodesQueries onB+ -TreesUpdates onB+ -TreesB+ -Tree FileOrganization

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Example of a B+ -Tree

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

    B+

    -tree for account le (n = 5)

  • 8/13/2019 Ch12 Indexing (1)

    21/63

  • 8/13/2019 Ch12 Indexing (1)

    22/63

    Database

    SystemConcepts

    BasicConcepts

    B+ -TreeIndicesDenitionB+ -Tree NodesQueries onB+ -TreesUpdates onB+ -TreesB+ -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 P i either points to a lerecord with search-key value K i , or to a bucket of pointersto le records, each record having search-key value K i

    Only needs bucket structure if search-key does not form aprimary key

    If Li , L j are leaf nodes and i < j , Li s search-key values areless than L j s search-key valuesP n points to next leaf node in search-key order

  • 8/13/2019 Ch12 Indexing (1)

    23/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndicesDenitionB+ -Tree NodesQueries onB+ -TreesUpdates onB+ -TreesB+ -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 leaf nodesFor a non-leaf node with m pointers:

    All the search-keys in the subtree to which P 1 points areless than K 1For 2 i m 1, all the search-keys in the subtree towhich P i points have values greater than or equal to K i 1and less than K i All the search-keys in the subtree to which P m points aregreater than K m 1

    P 1 K 1 P 2 . . . P m 1 K m 1 P m

  • 8/13/2019 Ch12 Indexing (1)

    24/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndicesDenitionB+ -Tree NodesQueries onB+ -TreesUpdates onB+ -TreesB+ -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 closeThe non-leaf levels of the B+ -tree form a hierarchy of sparse indicesThe B+ -tree contains a relatively small number of levels(logarithmic in the size of the main le), thus searches canbe conducted efficiently

    Insertions and deletions to the main le can be handledefficiently, as the index can be restructured in logarithmictime

  • 8/13/2019 Ch12 Indexing (1)

    25/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndicesDenitionB+ -Tree NodesQueries onB+ -TreesUpdates onB+ -TreesB+ -Tree FileOrganization

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Queries on B+ -Trees

    To nd 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 K i ), then follow P i to

    the child node3 Otherwise follow P m to the child node (k K m 1, wherethere 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 K i = k follow pointer P i to the desired

    record or bucket2 Else no record with search-key value k exists

    +

  • 8/13/2019 Ch12 Indexing (1)

    26/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndicesDenitionB+ -Tree NodesQueries onB+ -TreesUpdates onB+ -TreesB+ -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 nodeIf there are K search-key values in the le, the path is nolonger than logn / 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 valuesaround 20 nodes are accessed in alookupThe above difference is signicant since every node accessmay need a disk I/O, costing around 20 milliseconds!

    +

  • 8/13/2019 Ch12 Indexing (1)

    27/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndicesDenitionB+ -Tree NodesQueries onB+ -TreesUpdates onB+ -TreesB+ -Tree FileOrganization

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Insertion on a B+ -Tree

    Find the leaf node in which the search-key value wouldappearIf the search-key value is already there in the leaf node,

    record is added to le and if necessary a pointer is insertedinto the bucket.If the search-key value is not there, then add the record tothe main le 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)

    S li i N d

  • 8/13/2019 Ch12 Indexing (1)

    28/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndicesDenitionB+ -Tree NodesQueries onB+ -TreesUpdates onB+ -TreesB+ -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 rst 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 foundIn the worst case the root node may be split increasing theheight of the tree by 1

    I i E l

  • 8/13/2019 Ch12 Indexing (1)

    29/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndicesDenitionB+ -Tree NodesQueries onB+ -TreesUpdates onB+ -TreesB+ -Tree FileOrganization

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Insertion Example

    B+

    -Tree before and after insertion of Clearview

    S litti N d

  • 8/13/2019 Ch12 Indexing (1)

    30/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndicesDenitionB+ -Tree NodesQueries onB+ -TreesUpdates onB+ -TreesB+ -Tree FileOrganization

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Splitting a Node

    Result of splitting node containing Brighton and Downtown oninserting Clearview

    (example )

    D l ti B+ T

  • 8/13/2019 Ch12 Indexing (1)

    31/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    DenitionB+ -Tree NodesQueries onB+ -TreesUpdates onB+ -TreesB+ -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 le and from the bucket (if present)Remove (search-key value,pointer) from the leaf node if

    there is no bucket or if the bucket has become emptyIf the node has too few entries due to the removal, andthe entries in the node and a sibling t into a single node,then

    Insert all the search-key values in the two nodes into a

    single node and delete the other node.Delete the pair (K i 1,P i ), where P i is the pointer to thedeleted node, from its parent, recursively using the aboveprocedure.

    Deletion on a B+ Tree(cont )

  • 8/13/2019 Ch12 Indexing (1)

    32/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    DenitionB+ -Tree NodesQueries onB+ -TreesUpdates onB+ -TreesB+ -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 t into asingle node, then

    Redistribute the pointers between the node and a sibling

    such that both have more than the minimum number of entriesUpdate the corresponding search-key value in the parent of the node

    The node deletions may cascade upwards till a node whichhas n/ 2 or more pointers is foundIf the root node has only one pointer after deletion, it isdeleted and the sole child becomes the root

    Deletion Example

  • 8/13/2019 Ch12 Indexing (1)

    33/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    DenitionB+ -Tree NodesQueries onB+ -TreesUpdates onB+ -TreesB+ -Tree FileOrganization

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Deletion Example

    B+

    -Tree before and after deletion of Downtown

    Deletion Example (cont )

  • 8/13/2019 Ch12 Indexing (1)

    34/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    DenitionB+ -Tree NodesQueries onB+ -TreesUpdates onB+ -TreesB+ -Tree FileOrganization

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Deletion Example (cont.)

    B+ -Tree before and after deletion of Perryridge

    Deletion Example (cont )

  • 8/13/2019 Ch12 Indexing (1)

    35/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    DenitionB+ -Tree NodesQueries onB+ -TreesUpdates onB+ -TreesB+ -Tree FileOrganization

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Deletion Example (cont.)

    B+

    -Tree before and after deletion of Perryridge

    B+ -Tree File Organization

  • 8/13/2019 Ch12 Indexing (1)

    36/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    DenitionB+ -Tree NodesQueries onB+ -TreesUpdates onB+ -TreesB+ -Tree FileOrganization

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    B -Tree File Organization

    Index le degradation problem is solved by using B+

    -treeindices. Data le degradation problem is solved by usingB+ -tree File OrganizationThe leaf nodes in a B+ -tree le organization store records,instead of pointersInsertion and deletion are handled in the same way asinsertion and deletion of entries in a B+ -tree index

    Outline

  • 8/13/2019 Ch12 Indexing (1)

    37/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesStatic HashingDynamicHashing

    Indices onMultiple Keys

    Indices and

    SQL

    Outline

    1 Basic Concepts

    2 B+ -Tree Indices

    3 Hash IndicesStatic HashingDynamic Hashing

    4 Indices on Multiple Keys

    5 Indices and SQL

    Static Hashing

  • 8/13/2019 Ch12 Indexing (1)

    38/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesStatic HashingDynamicHashing

    Indices onMultiple Keys

    Indices and

    SQL

    Static Hashing

    A bucket is a unit of storage containing one or morerecords (a bucket is typically a disk block)In a hash le organization we obtain the bucket of a recorddirectly from its search-key value using a hash functionHash 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 deletionRecords with different search-key values may be mappedto the same bucket; thus entire bucket has to be searchedsequentially to locate a record

    Hash File Organization

  • 8/13/2019 Ch12 Indexing (1)

    39/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesStatic HashingDynamicHashing

    Indices onMultiple Keys

    Indices and

    SQL

    Hash File Organization

    Hash le organization of account le, using branch name as key

    There are 10 bucketsThe binary representation of

    the i

    th character is assumedto be the integer i The hash function returnsthe sum of the binaryrepresentations of thecharacters modulo 10E.g. h(Perryridge) = 5,h(Round Hill) = 3,h(Brighton) = 3

    Hash Functions

  • 8/13/2019 Ch12 Indexing (1)

    40/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesStatic HashingDynamicHashing

    Indices onMultiple Keys

    Indices and

    SQL

    Hash Functions

    Worst hash function maps all search-key values to thesame bucketAn ideal hash function is uniform, i.e., each bucket isassigned the same number of search-key values from the

    set of all possible valuesIdeal hash function is random, so each bucket will havethe same number of records assigned to it irrespective of the actual distribution of search-key values in the leTypical 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

    Handling of Bucket Overows

  • 8/13/2019 Ch12 Indexing (1)

    41/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesStatic HashingDynamicHashing

    Indices onMultiple Keys

    Indices andSQL

    a d g o uc et Ove ows

    Bucket overow can occur because of Insufficient bucketsSkew in distribution of records. This can occur due to two

    reasons:multiple records have same search-key valuechosen hash function produces non-uniform distribution of key values

    Although the probability of bucket overow can be

    reduced, it cannot be eliminated; it is handled by usingoverow buckets

    Handling of Bucket Overows (cont.)

  • 8/13/2019 Ch12 Indexing (1)

    42/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesStatic HashingDynamicHashing

    Indices onMultiple Keys

    Indices andSQL

    g ( )

    Overow chaining - the overow 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 overow buckets, is not suitable for database

    applications

    Hash Indices

  • 8/13/2019 Ch12 Indexing (1)

    43/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesStatic HashingDynamicHashing

    Indices onMultiple Keys

    Indices andSQL

    Hashing can be used not only for le organization, butalso for index-structure creationA hash index organizes the search keys, with their

    associated record pointers, into a hash le structureStrictly speaking, hash indices are always secondary indices

    if the le itself is organized using hashing, a separateprimary hash index on it using the same search-key is

    unnecessaryhowever, we use the term hash index to refer to bothsecondary index structures and hash organized les

    Example of Hash Index

  • 8/13/2019 Ch12 Indexing (1)

    44/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesStatic HashingDynamicHashing

    Indices onMultiple Keys

    Indices andSQL

    p

    Deciencies of Static Hashing

  • 8/13/2019 Ch12 Indexing (1)

    45/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesStatic HashingDynamicHashing

    Indices onMultiple Keys

    Indices andSQL

    g

    In static hashing, function h maps search-key values to axed set of B of bucket addressesDatabases grow with time. If initial number of buckets istoo small, performance will degrade due to too muchoverowsIf le size at some point in the future is anticipated andnumber of buckets allocated accordingly, signicantamount of space will be wasted initiallyIf database shrinks, again space will be wastedOne option is periodic re-organization of the le with anew hash function, but it is very expensiveThese problems can be avoided by using techniques thatallow the number of buckets to be modied dynamically

  • 8/13/2019 Ch12 Indexing (1)

    46/63

    Extendable Hash Structure

  • 8/13/2019 Ch12 Indexing (1)

    47/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesStatic HashingDynamicHashing

    Indices onMultiple Keys

    Indices andSQL

    Use of Extendable Hash Structure

  • 8/13/2019 Ch12 Indexing (1)

    48/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesStatic HashingDynamicHashing

    Indices onMultiple Keys

    Indices andSQL

    Each bucket j stores a value i j ; all the entries that point tothe same bucket have the same values on the rst i j bitsTo locate the bucket containing search-key K j :

    1 Compute h(K j ) = X 2 Use the rst 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 K j 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

    Overow buckets used instead in some cases

    Updates in Extendable Hash Structure

  • 8/13/2019 Ch12 Indexing (1)

    49/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesStatic HashingDynamicHashing

    Indices onMultiple Keys

    Indices andSQL

    To split a bucket j when inserting record with search-key valueK j :If i = i j (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 to

    the same bucketrecompute new bucket address table entry for K j Now i > i j so use the following case

    If i > i j (more than one pointer to bucket j )allocate a new bucket z , and set i j and i z to the old i j + 1

    make 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 K j and insert record in thebucket (further splitting is required if the bucket is still full)

    Updates in Extendable Hash Structure (cont.)

  • 8/13/2019 Ch12 Indexing (1)

    50/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesStatic HashingDynamicHashing

    Indices onMultiple Keys

    Indices andSQL

    When inserting a value, if the bucket is full after severalsplits (that is, i reaches some limit b ) create an overowbucket instead of splitting bucket entry table furtherTo 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 i j and same i j 1prex, 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

  • 8/13/2019 Ch12 Indexing (1)

    51/63

    An Example (cont.)

  • 8/13/2019 Ch12 Indexing (1)

    52/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesStatic HashingDynamicHashing

    Indices onMultiple Keys

    Indices andSQL

    Hash structure after insertion of one Brighton and twoDowntown records

    An Example (cont.)

  • 8/13/2019 Ch12 Indexing (1)

    53/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesStatic HashingDynamicHashing

    Indices onMultiple Keys

    Indices andSQL

    Hash structure after insertion of one Mianus record

    An Example (cont.)

  • 8/13/2019 Ch12 Indexing (1)

    54/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesStatic HashingDynamicHashing

    Indices onMultiple Keys

    Indices andSQL

    Hash structure after insertion of three Perryridge records

  • 8/13/2019 Ch12 Indexing (1)

    55/63

    Extendable Hashing vs. Other Schemes

  • 8/13/2019 Ch12 Indexing (1)

    56/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesStatic HashingDynamicHashing

    Indices onMultiple Keys

    Indices andSQL

    Benets of extendable hashing:Hash performance does not degrade with growth of leMinimal space overhead

    Disadvantages of extendable hashingExtra level of indirection to nd desired recordBucket address table may itself become very big (largerthan memory)Changing size of bucket address table is an expensiveoperation

    Outline

  • 8/13/2019 Ch12 Indexing (1)

    57/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    1 Basic Concepts

    2 B+ -Tree Indices

    3 Hash Indices

    4 Indices on Multiple Keys

    5 Indices and SQL

    Multiple-Key Access

  • 8/13/2019 Ch12 Indexing (1)

    58/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash Indices

    Indices onMultiple Keys

    Indices andSQL

    Use multiple indices for certain types of queriesExampleselect 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 ndbranch name = Perryridge; test for balance = $1000

    2 Use index on balance to nd balance = $1000; test forbranch name = Perryridge

    3 Use branch name index to nd pointers to all recordspertaining to the Perryridge branch; similarly, use index onbalance ; take intersection of both sets of pointers obtained

    Indices on Multiple Keys

  • 8/13/2019 Ch12 Indexing (1)

    59/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesIndices onMultiple Keys

    Indices andSQL

    Composite search keys are search keys containing morethan one attribute

    E.g. (branch name , balance )Lexicographic ordering: (a1, a2) < (b 1, b 2) if either

    a1 < b 1, ora1 = b 1 and a2 < b 2

    Examples

  • 8/13/2019 Ch12 Indexing (1)

    60/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesIndices onMultiple Keys

    Indices andSQL

    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 rst but not the

    second condition

    Outline

  • 8/13/2019 Ch12 Indexing (1)

    61/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesIndices onMultiple Keys

    Indices andSQL

    1 Basic Concepts

    2 B+ -Tree Indices

    3 Hash Indices

    4 Indices on Multiple Keys

    5 Indices and SQL

    Index Denition in SQL

  • 8/13/2019 Ch12 Indexing (1)

    62/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesIndices onMultiple Keys

    Indices andSQL

    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>

  • 8/13/2019 Ch12 Indexing (1)

    63/63

    DatabaseSystem

    Concepts

    BasicConcepts

    B+ -TreeIndices

    Hash IndicesIndices onMultiple Keys

    Indices andSQL

    End of Chapter 12