1 / 45

Hash-Based Indexes

Hash-Based Indexes. Static Hash indexes Dynamic Hash indexes Extendible, Linear indexes. Static Hashing. A bucket is a unit of storage containing one or more records (a bucket is typically a disk block).

gasha
Download Presentation

Hash-Based Indexes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Hash-Based Indexes Static Hash indexes Dynamic Hash indexes Extendible, Linear indexes

  2. Static Hashing • A bucket is a unit of storage containing one or more records (a bucket is typically a disk block). • In a hash file organization we obtain the bucket of a record directly from its search-key value using a hashfunction. • Hash function h is a function from the set of all search-key values K to the set of all bucket addresses B. • Hash function is used to locate records for access, insertion as well as deletion. • Records with different search-key values may be mapped to the same bucket; thus entire bucket has to be searched sequentially to locate a record.

  3. Static Hashing • # primary pages fixed, allocated sequentially, never de-allocated; overflow pages if needed. • h(k) mod M = bucket to which data entry withkey k belongs. (M = # of buckets) 0 h(key) mod N 2 key h N-1 Primary bucket pages Overflow pages

  4. Static Hashing (Contd.) • Buckets contain data entries. • Hash fn works on search key field of record r. Must distribute values over range 0 ... M-1. • h(key) = (a * key + b) usually works well. • a and b are constants; lots known about how to tune h. • Long overflow chains can develop and degrade performance. • Extendible and LinearHashing: Dynamic techniques to fix this problem.

  5. Example of Hash File Organization Hash file organization of instructor file, using dept_name as key (See figure in next slide.) • There are 8 buckets, • The binary representation of the ith character is assumed to be the integer i. • The hash function returns the sum of the binary representations of the characters modulo 8 • E.g. h(Music) = 1 h(History) = 2 h(Physics) = 3 h(Elec. Eng.) = 3

  6. Example of Hash File Organization Hash file organization of instructor file, using dept_name as key (see previous slide for details).

  7. Hash Functions • Worst hash function maps all search-key values to the same bucket; this makes access time proportional to the number of search-key values in the file. • An ideal hash function is uniform, i.e., each bucket is assigned the same number of search-key values from the set of all possible values. • Ideal hash function is random, so each bucket will have the same number of records assigned to it irrespective of the actual distribution of search-key values in the file. • Typical hash functions perform computation on the internal binary representation of the search-key. • For example, for a string search-key, the binary representations of all the characters in the string could be added and the sum modulo the number of buckets could be returned. .

  8. Handling of Bucket Overflows • Bucket overflow can occur because of • Insufficient buckets • Skew in distribution of records. This can occur due to two reasons: • multiple records have same search-key value • chosen hash function produces non-uniform distribution of key values • Although the probability of bucket overflow can be reduced, it cannot be eliminated; it is handled by using overflow buckets.

  9. Handling of Bucket Overflows (Cont.) • Overflow chaining – the overflow buckets of a given bucket are chained together in a linked list. • Above scheme is called closed hashing. • An alternative, called open hashing, which does not use overflow buckets, is not suitable for database applications.

  10. Hash Indices • Hashing can be used not only for file organization, but also for index-structure creation. • A hash index organizes the search keys, with their associated record pointers, into a hash file structure. • Strictly speaking, hash indices are always secondary indices • if the file itself is organized using hashing, a separate primary hash index on it using the same search-key is unnecessary. • However, we use the term hash index to refer to both secondary index structures and hash organized files.

  11. Example of Hash Index hash index on instructor, on attribute ID

  12. Deficiencies of Static Hashing • In static hashing, function h maps search-key values to a fixed set of B of bucket addresses. Databases grow or shrink with time. • If initial number of buckets is too small, and file grows, performance will degrade due to too much overflows. • If space is allocated for anticipated growth, a significant amount of space will be wasted initially (and buckets will be underfull). • If database shrinks, again space will be wasted. • One solution: periodic re-organization of the file with a new hash function • Expensive, disrupts normal operations • Better solution: allow the number of buckets to be modified dynamically.

  13. 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 = 32. • At any time use only a prefix of the hash function to index into a table of bucket addresses. • Let the length of the prefix be i bits, 0  i  32. • Bucket address table size = 2i.Initially i = 0 • Value of i grows and shrinks as the size of the database grows and shrinks. • Multiple entries in the bucket address table may point to a bucket (why?) • Thus, actual number of buckets is < 2i • The number of buckets also changes dynamically due to coalescing and splitting of buckets.

  14. General Extendable Hash Structure In this structure, i2 = i3 = i, whereas i1 = i – 1 (see next slide for details)

  15. Extendible Hashing • Situation: Bucket (primary page) becomes full. Why not re-organize file by doubling # of buckets? • Reading and writing all pages is expensive! • Idea: Use directory of pointers to buckets, double # of buckets by doubling the directory, splitting just the bucket that overflowed! • Directory much smaller than file, so doubling it is much cheaper. Only one page of data entries is split. Nooverflowpage! • Trick lies in how hash function is adjusted!

  16. LOCAL DEPTH 2 Example Bucket A 16* 4* 12* 32* GLOBAL DEPTH 2 2 Bucket B 00 5* 1* 21* 13* 01 • Directory is array of size 4. • To find bucket for r, take last `global depth’ # bits of h(r); we denote r by h(r). • If h(r) = 5 = binary 101, it is in bucket pointed to by 01. 2 10 Bucket C 10* 11 2 DIRECTORY Bucket D 15* 7* 19* DATA PAGES • Insert: If bucket is full, splitit (allocate new page, re-distribute). • If necessary, double the directory. (As we will see, splitting a • bucket does not always require doubling; we can tell by • comparing global depth with local depth for the split bucket.)

  17. Insert h(r)=20 (Causes Doubling) 2 LOCAL DEPTH 3 LOCAL DEPTH Bucket A 16* 32* 32* 16* GLOBAL DEPTH Bucket A GLOBAL DEPTH 2 2 2 3 Bucket B 5* 21* 13* 1* 00 1* 5* 21* 13* 000 Bucket B 01 001 2 10 2 010 Bucket C 10* 11 10* Bucket C 011 100 2 2 DIRECTORY 101 Bucket D 15* 7* 19* 15* 7* 19* Bucket D 110 111 2 3 Bucket A2 4* 12* 20* DIRECTORY 12* 20* Bucket A2 4* (`split image' of Bucket A) (`split image' of Bucket A)

  18. Points to Note • 20 = binary 10100. Last 2 bits (00) tell us r belongs in A or A2. Last 3 bits needed to tell which. • Global depth of directory: Max # of bits needed to tell which bucket an entry belongs to. • Local depth of a bucket: # of bits used to determine if an entry belongs to this bucket. • When does bucket split cause directory doubling? • Before insert, local depth of bucket = global depth. Insert causes local depth to become > global depth; directory is doubled by copying it over and `fixing’ pointer to split image page. (Use of least significant bits enables efficient doubling via copying of directory!)

  19. Comments on Extendible Hashing • If directory fits in memory, equality search answered with one disk access; else two. • 100MB file, 100 bytes/rec, 4K pages contains 1,000,000 records (as data entries) and 25,000 directory elements; chances are high that directory will fit in memory. • Directory grows in spurts, and, if the distribution of hash values is skewed, directory can grow large. • Multiple entries with same hash value cause problems! • Delete: If removal of data entry makes bucket empty, can be merged with `split image’. If each directory element points to same bucket as its split image, can halve directory.

  20. Use of Extendable Hash Structure • Each bucket j stores a value ij • All the entries that point to the same bucket have the same values on the first ij bits. • To locate the bucket containing search-key Kj: 1. Compute h(Kj) = X 2. Use the first i high order bits of X as a displacement into bucket address table, and follow the pointer to appropriate bucket • 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 bucket. • Else the bucket must be split and insertion re-attempted (next slide.) • Overflow buckets used instead in some cases (will see shortly)

  21. Insertion in Extendable Hash Structure (Cont) To split a bucket j when inserting record with search-key value Kj: • If i > ij (more than one pointer to bucket j) • allocate a new bucket z, and set ij= iz = (ij + 1) • Update the second half of the bucket address table entries originally pointing to j, to point to z • remove each record in bucket j and reinsert (in j or z) • recompute new bucket for Kjand insert record in the bucket (further splitting is required if the bucket is still full) • If i = ij(only one pointer to bucket j) • If i reaches some limit b, or too many splits have happened in this insertion, create an overflow bucket • Else • increment i and double the size of the bucket address table. • replace each entry in the table by two entries that point to the same bucket. • recompute new bucket address table entry for KjNow i > ij so use the first case above.

  22. Deletion in Extendable Hash Structure • To delete a key value, • locate it in its bucket and remove it. • The bucket itself can be removed if it becomes empty (with appropriate updates to the bucket address table). • Coalescing of buckets can be done (can coalesce only with a “buddy” bucket having same value of ij and same ij –1 prefix, if it is present) • Decreasing bucket address table size is also possible • Note: decreasing bucket address table size is an expensive operation and should be done only if number of buckets becomes much smaller than the size of the table

  23. Use of Extendable Hash Structure: Example

  24. Example (Cont.) • Initial Hash structure; bucket size = 2

  25. Example (Cont.) • Hash structure after insertion of “Mozart”, “Srinivasan”, and “Wu” records

  26. Example (Cont.) • Hash structure after insertion of Einstein record

  27. Example (Cont.) • Hash structure after insertion of Gold and El Said records

  28. Example (Cont.) • Hash structure after insertion of Katz record

  29. Example (Cont.) And after insertion of eleven records

  30. Example (Cont.) And after insertion of Kim record in previous hash structure

  31. Extendable Hashing vs. Other Schemes • Benefits of extendable hashing: • Hash performance does not degrade with growth of file • Minimal space overhead • Disadvantages of extendable hashing • Extra level of indirection to find desired record • Bucket address table may itself become very big (larger than memory) • Cannot allocate very large contiguous areas on disk either • Solution: B+-tree structure to locate desired record in bucket address table • Changing size of bucket address table is an expensive operation • Linear hashingis an alternative mechanism • Allows incremental growth of its directory (equivalent to bucket address table) • At the cost of more bucket overflows

  32. Linear Hashing • This is another dynamic hashing scheme, an alternative to Extendible Hashing. • LH handles the problem of long overflow chains without using a directory, and handles duplicates. • Idea: Use a family of hash functions h0, h1, h2, ... • hi(key) = h(key) mod(2iN); N = initial # buckets • h is some hash function (range is not 0 to N-1) • If N = 2d0, for some d0, hi consists of applying h and looking at the last di bits, where di = d0 + i. • hi+1 doubles the range of hi (similar to directory doubling)

  33. Linear Hashing (Contd.) • Directory avoided in LH by using overflow pages, and choosing bucket to split round-robin. • Splitting proceeds in `rounds’. Round ends when all NRinitial (for round R) buckets are split. Buckets 0 to Next-1 have been split; Next to NR yet to be split. • Current round number is Level. • Search:To find bucket for data entry r, findhLevel(r): • If hLevel(r) in range `Next to NR’, r belongs here. • Else, r could belong to bucket hLevel(r) or bucket hLevel(r) + NR; must apply hLevel+1(r) to find out.

  34. Overview of LH File • In the middle of a round. Buckets split in this round: Bucket to be split If ( h search key value ) Level Next is in this range, must use h ( search key value ) Level+1 Buckets that existed at the to decide if entry is in beginning of this round: `split image' bucket. this is the range of h Level `split image' buckets: created (through splitting of other buckets) in this round

  35. Linear Hashing (Contd.) • Insert: Find bucket by applying hLevel / hLevel+1: • If bucket to insert into is full: • Add overflow page and insert data entry. • (Maybe) Split Next bucket and increment Next. • Can choose any criterion to `trigger’ split. • Since buckets are split round-robin, long overflow chains don’t develop! • Doubling of directory in Extendible Hashing is similar; switching of hash functions is implicit in how the # of bits examined is increased.

  36. Example of Linear Hashing • On split, hLevel+1 is used to re-distribute entries. (insert 43) Level=0, N=4 Level=0 PRIMARY h h OVERFLOW h h PRIMARY PAGES 0 0 1 1 PAGES PAGES Next=0 32* 32* 44* 36* 000 00 000 00 Next=1 Data entry r 9* 5* 9* 5* 25* 25* with h(r)=5 001 001 01 01 30* 30* 10* 10* 14* 18* 14* 18* Primary 10 10 010 010 bucket page 31* 35* 7* 31* 35* 7* 11* 11* 43* 011 011 11 11 (This info is for illustration only!) (The actual contents of the linear hashed file) 100 44* 36* 00

  37. Summary • Hash-based indexes: best for equality searches, cannot support range searches. • Static Hashing can lead to long overflow chains. • Extendible Hashing avoids overflow pages by splitting a full bucket when a new data entry is to be added to it. • Directory to keep track of buckets, doubles periodically. • Can get large with skewed data; additional I/O if this does not fit in main memory.

  38. Summary (Contd.) • Linear Hashing avoids directory by splitting buckets round-robin, and using overflow pages. • Overflow pages not likely to be long. • Duplicates handled easily. • Space utilization could be lower than Extendible Hashing, since splits not concentrated on `dense’ data areas. • Can tune criterion for triggering splits to trade-off slightly longer chains for better space utilization. • For hash-based indexes, a skewed data distribution is one in which the hash values of data entries are not uniformly distributed!

  39. Comparison of Ordered Indexing and Hashing • Cost of periodic re-organization • Relative frequency of insertions and deletions • Is it desirable to optimize average access time at the expense of worst-case access time? • Expected type of queries: • Hashing is generally better at retrieving records having a specified value of the key. • If range queries are common, ordered indices are to be preferred • In practice: • PostgreSQL supports hash indices, but discourages use due to poor performance • Oracle supports static hash organization, but not hash indices • SQLServer supports only B+-trees

  40. Bitmap Indices • Bitmap indices are a special type of index designed for efficient querying on multiple keys • Records in a relation are assumed to be numbered sequentially from, say, 0 • Given a number n it must be easy to retrieve record n • Particularly easy if records are of fixed size • Applicable on attributes that take on a relatively small number of distinct values • E.g. gender, country, state, … • E.g. income-level (income broken up into a small number of levels such as 0-9999, 10000-19999, 20000-50000, 50000- infinity) • A bitmap is simply an array of bits

  41. Bitmap Indices (Cont.) • In its simplest form a bitmap index on an attribute has a bitmap for each value of the attribute • Bitmap has as many bits as records • In a bitmap for value v, the bit for a record is 1 if the record has the value v for the attribute, and is 0 otherwise

  42. Bitmap Indices (Cont.) • Bitmap indices are useful for queries on multiple attributes • not particularly useful for single attribute queries • Queries are answered using bitmap operations • Intersection (and) • Union (or) • Complementation (not) • Each operation takes two bitmaps of the same size and applies the operation on corresponding bits to get the result bitmap • E.g. 100110 AND 110011 = 100010 100110 OR 110011 = 110111 NOT 100110 = 011001 • Males with income level L1: 10010 AND 10100 = 10000 • Can then retrieve required tuples. • Counting number of matching tuples is even faster

  43. Bitmap Indices (Cont.) • Bitmap indices generally very small compared with relation size • E.g. if record is 100 bytes, space for a single bitmap is 1/800 of space used by relation. • If number of distinct attribute values is 8, bitmap is only 1% of relation size • Deletion needs to be handled properly • Existence bitmapto note if there is a valid record at a record location • Needed for complementation • not(A=v): (NOT bitmap-A-v) AND ExistenceBitmap • Should keep bitmaps for all values, even null value • To correctly handle SQL null semantics for NOT(A=v): • intersect above result with (NOT bitmap-A-Null)

  44. Efficient Implementation of Bitmap Operations • Bitmaps are packed into words; a single word and (a basic CPU instruction) computes and of 32 or 64 bits at once • E.g. 1-million-bit maps can be and-ed with just 31,250 instruction • Counting number of 1s can be done fast by a trick: • Use each byte to index into a precomputed array of 256 elements each storing the count of 1s in the binary representation • Can use pairs of bytes to speed up further at a higher memory cost • Add up the retrieved counts • Bitmaps can be used instead of Tuple-ID lists at leaf levels of B+-trees, for values that have a large number of matching records • Worthwhile if > 1/64 of the records have that value, assuming a tuple-id is 64 bits • Above technique merges benefits of bitmap and B+-tree indices

  45. Index Definition in SQL • Create an index create index <index-name> on <relation-name> (<attribute-list>) E.g.: create index b-index on branch(branch_name) • Use create unique index to indirectly specify and enforce the condition that the search key is a candidate key is a candidate key. • Not really required if SQL unique integrity constraint is supported • To drop an index drop index <index-name> • Most database systems allow specification of type of index, and clustering.

More Related