Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring 2004 ECE569 Lecture 04-2.1 ECE 569 Database System Engineering Spring 2004 Yanyong Zhang www.ece.rutgers.edu/~yyzhangwww.ece.rutgers.edu/~yyzhang.

Similar presentations


Presentation on theme: "Spring 2004 ECE569 Lecture 04-2.1 ECE 569 Database System Engineering Spring 2004 Yanyong Zhang www.ece.rutgers.edu/~yyzhangwww.ece.rutgers.edu/~yyzhang."— Presentation transcript:

1 Spring 2004 ECE569 Lecture 04-2.1 ECE 569 Database System Engineering Spring 2004 Yanyong Zhang www.ece.rutgers.edu/~yyzhangwww.ece.rutgers.edu/~yyzhang Course URL www.ece.rutgers.edu/~yyzhang/spring04

2 Spring 2004 ECE569 Lecture 04-2.2 Associative access  The system is not asked to retrieve tuples based on information about their storage location; rather, it has to find all tuples the attribute values of which fulfill certain conditions – associative access.  Associative access can be realized by sequential scanning, which happens for complicated queries. selectR.x, S.y, fromR,S whereR.k = S.f and R.b < 12;  But for simple selection predicates, this is very slow (even for an in-memory database)

3 Spring 2004 ECE569 Lecture 04-2.3 Access Path  The class of algorithms and data structures designed for translating attribute values into TID, or into other types of internal addresses of tuples having those attribute values, is called access paths.  Depending on what kind of selection predicate is to be supported, the techniques for associative access vary greatly.

4 Spring 2004 ECE569 Lecture 04-2.4 Content addressability techniques  Primary key access. l A tuple of a relation must be retrieved efficiently via the value of its primary (unique) key(s). e.g., key-sequenced files and hased files. l Point query vs. range query  Secondary key access l A set of tuples are produced  Multi-table access l Tuple access is often based on relationships between different tuples. E.g., all orders placed by a given customer have to be found.

5 Spring 2004 ECE569 Lecture 04-2.5 Associative access path techniques  Hashing (key transformation) l Using the primary key value as a parameter to a function, which returns the storage location of the tuple.  Key comparison l Maintaining a dynamic search structure on the set of values in the key attribute. These values can be organized into tables, lists, trees, and so on l e.g, B+ tree

6 Spring 2004 ECE569 Lecture 04-2.6 Operations on files (heap files)  Assumptions l n = number of records in file l R = number of records that can fit in block  Lookup – Given a key find corresponding record l On average, n / (2R) block accesses.  Insertion – add record to file (allows duplicates) l Read last block; it may need to allocate a new block. Approximately, requires 2 accesses  Deletion – delete record l look up record n / (2R) l Write back to disk (1 access) l Reorganize (unpinned) – move tuple from last page to utilize space (2 disk accesses)

7 Spring 2004 ECE569 Lecture 04-2.7 Hashed Files  File is divided into B buckets  Hash function h maps elements of the key space to range [0, B) l Key space is large and unevenly distributed -SSNs as character strings -Each character takes on at most 10 of the possible 256 values Hash function h must map key values evenly among a relatively small number of values.

8 Spring 2004 ECE569 Lecture 04-2.8 Hash-based associative access FOLDING HASHING Range of positive integers tuple address space Range of Potential Key Values (the shaded areas denote used key values)

9 Spring 2004 ECE569 Lecture 04-2.9 Folding  Convert arbitrary data types to a positive integer h can be applied to.  Reduce number of bits so that arithmetic is efficient.  Example: Key is “Keefe” and 16803 l Key value is the concatenation of byte representation of individual fields l Folded value of key is 0x4b 0x65 0x65 0x66 0x65 0x0 0x0 0x0 0x41 0xa3 Partition result into words and combine using XOR 0x4b 0x65 0x65 0x66 0x65 0x0 0x0 0x0 0x41 0xa3 0x0 0x0  0x6f 0xc6 0x65 0x66 = 1875273062

10 Spring 2004 ECE569 Lecture 04-2.10 Hashing  goal of hashing  How to choose hash function if all the key values are uniformly distributed?  The critical issue is to produce 1:1 mapping  Collision: different inputs are mapped to the same output.  The criteria of a good hash function is to keep the collision as small as possible.

11 Spring 2004 ECE569 Lecture 04-2.11 Static Hashing  Input: folded key values  Output: bytes (relative to the beginning of the file), blocks ?? l Bytes are not good because of the varying tuple size. l A block/page is called a bucket.  H: {0 … 2 32 -1} -> {0, B-1} l Continuous allocation l Fixed size: B pages are allocated at file creation time. l Insert -Determine the bucket -Check the bucket ( collision may happen)

12 Spring 2004 ECE569 Lecture 04-2.12 How to find a good hash function  Division / remainder (Congruential hashing) H(K b ) = k b mod B where k b is folded key value and B is the number of buckets.  Nth power l Compute k b N, and from the resulting bit string (n x 31 bits) take log 2 B bits from the middle.  Base transformation  Polynomial division  Numerical analysis  encryption

13 Spring 2004 ECE569 Lecture 04-2.13 Performance  Assumption l Perfect hash function (tuples are uniformly distributed over B buckets)  Lookup l  ½  n/R  1/B  To finish first match l  n/R  1/B  If tuple does not exist  Insertion l  n/R  1/B  + 1Test for duplicates l 1Otherwise  Deletion l  ½  n/R  1/B  delete first match

14 Spring 2004 ECE569 Lecture 04-2.14 Collision  Two keys collide if they hash to same value  A bucket with room for R tuples can accommodate R – 1 collisions before it overflows l Internal resolution: Place overflow blocks in another bucket -(h(K) + 1) mod Blinear probing -(h2(h1(K))multiple hashing

15 Spring 2004 ECE569 Lecture 04-2.15 Collision - continued l External resolution: Allocation overflow block, link to overflow chain bucketsOverflow pages

16 Spring 2004 ECE569 Lecture 04-2.16 Discussion  What are the disadvantages of static hashing?  How do you limit the number of pages accessed when retrieving a tuple, for both external and internal resolution?

17 Spring 2004 ECE569 Lecture 04-2.17 Trie  The buckets will dynamically grow/shrink/balance  Fundamental: trie A B C 0 0 1 1 (a) A B C 0 0 1 1 0 1 D (b) 000 001 010 011 100 101 110 111 A D B C (c)

18 Spring 2004 ECE569 Lecture 04-2.18 Dynamic hashing function  We need a hash function whose range of values can change dynamically  One such hash function can be constructed using a series of functions h i (k), i = 0, 1, …., such that for any k, either h i (k) = h i-1 (k) or h i (k) = h i-1 (k)+2 i-1.  Choose H(k), which maps the key space into random bit patterns of length m, for m sufficiently large. Then h i (k) may be defined as the integers formed by the last i bits of H(k).

19 Spring 2004 ECE569 Lecture 04-2.19 Extendible Hashing  The number of buckets can grow/shrink.  An intermediate data structure translates the hash results into page addresses. This data structure needs to be as compact as possible. l Hashes into an array of pointer to buckets (directory). l The array is small enough to be kept in memory.

20 Spring 2004 ECE569 Lecture 04-2.20 Directory Growth To adapt to dynamically varying size of hash file- modify directory size Assume a hash function h(K b ) that produces a bit string s. The directory is of size 2 d. d is called the global depth and is initially 0. Use least significant d bits of s to determine bucket to access Each bucket has a corresponding local depth in the range [0, d] which indicates the difference between all the records in this bucket

21 Spring 2004 ECE569 Lecture 04-2.21 Example  Insert 0x13, 0x10, 0x07, 0x00, 0x1f  Each page can contain no more than 2 tuples

22 Spring 2004 ECE569 Lecture 04-2.22 Example – insert 0x1f local degree = global degree – log2(# of arrows pointing to this bucket)

23 Spring 2004 ECE569 Lecture 04-2.23 Performance  2 steps for retrieving a tuple  If we can keep the directory in memory, each retrieval is one page access  Assuming 4 bytes per entry, 4KB pages, 1GB hash files, and we want to keep the entire directory in memory, what is the minimum buffer size?

24 Spring 2004 ECE569 Lecture 04-2.24 Discussion  How easy is it to keep the directory in the memory?  How do we reduce the structure when the file shrinks?  How do you make the directory small, and increase space utilization? (deferred splitting)

25 Spring 2004 ECE569 Lecture 04-2.25 Linear hashing (a) 00011011 (b) 000 011011 w 100 (c) 000001 1011 w 100101 abcd abcd abcd x (c) 000001 010 11 w 100101 abcd x 110 y (d) 000001 010011 w 100101 abcd x 110 y 111 d = 2 d = 3

26 Spring 2004 ECE569 Lecture 04-2.26 Addressing in linear hashing  Which hash function should be used? l d is the degree, p is the address of the next page to split l The algorithm is as follows: begin if (h d (k) >= p) then page:= h d (k) else page := h d+1 (k) if necessary, chase the overflow chain end

27 Spring 2004 ECE569 Lecture 04-2.27 Discuss  Where to store the overflow?


Download ppt "Spring 2004 ECE569 Lecture 04-2.1 ECE 569 Database System Engineering Spring 2004 Yanyong Zhang www.ece.rutgers.edu/~yyzhangwww.ece.rutgers.edu/~yyzhang."

Similar presentations


Ads by Google