Chapter 13 Query Processing Database System Concepts 5
Chapter 13: Query Processing Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See www. db-book. com for conditions on re-use
Chapter 13: Query Processing n Overview n Measures of Query Cost n Selection Operation n Sorting n Join Operation n Other Operations n Evaluation of Expressions Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 2 ©Silberschatz, Korth and Sudarshan
Basic Steps in Query Processing 1. Parsing and translation 2. Optimization 3. Evaluation Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 3 ©Silberschatz, Korth and Sudarshan
Basic Steps in Query Processing (Cont. ) n Parsing and translation l translate the query into its internal form. 4 This l is then translated into relational algebra. Parser checks syntax, 4 verifies relations n Evaluation l The query-execution engine takes a query-evaluation plan, 4 executes 4 returns that plan, and the answers to the query. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 4 ©Silberschatz, Korth and Sudarshan
Basic Steps … : Optimization n A relational algebra expression may have many equivalent expressions l E. g. , balance 2500( balance(account)) 4 is equivalent to: balance( balance 2500(account)) n Each relational algebra operation can be evaluated using: 4 one l of several different algorithms Correspondingly, a relational-algebra expression: 4 can be evaluated in many ways. n Annotated expression specifying detailed evaluation strategy: 4 is l called an evaluation-plan. E. g. , can use an index on balance to 4 find l accounts with balance < 2500, or can perform complete relation scan and: 4 discard accounts with balance 2500 Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 5 ©Silberschatz, Korth and Sudarshan
Basic Steps: Optimization (Cont. ) n Query Optimization: Amongst all equivalent evaluation plans: l choose the one with lowest cost. l Cost is estimated using statistical information from: 4 the 4 e. g. database catalog number of tuples in each relation, size of tuples, etc. n In this chapter we study: l How to measure query costs l Algorithms for evaluating relational algebra operations l How to combine algorithms for individual operations in order to: 4 evaluate a complete expression n In Chapter 14: l We study how to optimize queries, that is: 4 how to find an evaluation plan with lowest estimated cost Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 6 ©Silberschatz, Korth and Sudarshan
Measures of Query Cost n Cost is generally measured as: 4 total l elapsed time for answering query Many factors contribute to time cost: 4 disk accesses, CPU, or even network communication n Typically disk access is the predominant cost, and 4 is l also relatively easy to estimate. Measured by taking into account: 4 Number of seeks * average-seek-cost 4 Number of blocks read * average-block-read-cost 4 Number of blocks written * average-block-write-cost 4 Cost to write a block is greater than cost to read a block – data is read back after being written: » to ensure that the write was successful Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 7 ©Silberschatz, Korth and Sudarshan
Measures of Query Cost (Cont. ) n As the cost measures: l for simplicity we just use: 4 the number of block transfers from disk and 4 the number of seeks l t. T – time to transfer one block l t. S – time for one seek l Cost for b block transfers plus S seeks: Cost = b * t. T + S * t. S n We ignore CPU costs for simplicity l Real systems do take CPU cost into account Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 8 ©Silberschatz, Korth and Sudarshan
Measures of Query Cost (Cont. ) n We do not include cost to writing output to disk in our cost formulae n Several algorithms can reduce disk I/O by: 4 using l extra buffer space Amount of real memory available to buffer depends on: 4 other concurrent queries and OS processes, – known only during execution 4 We often use worst case estimates, assuming: – only the minimum amount of memory » needed for the operation is available n Required data may be buffer resident already, 4 avoiding l disk I/O But hard to take into account for cost estimation Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 9 ©Silberschatz, Korth and Sudarshan
Selection Operation n File scan algorithms: 4 search algorithms that locate and retrieve records – that fulfill a selection condition. (No Index use!) n Algorithm A 1 (linear search): Scan each file block and 4 test l all records to see whether they satisfy the selection condition. Cost estimate = br block transfers + 1 seek 4 br denotes number of blocks containing records from relation r l l If selection is on a key attribute: 4 can stop on finding record 4 cost = (br /2) block transfers + 1 seek Linear search can be applied regardless of : 4 selection condition or 4 ordering of records in the file, or 4 availability of indices Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 10 ©Silberschatz, Korth and Sudarshan
Selection Operation (Cont. ) n A 2 (binary search). Applicable if selection is: 4 an equality comparison : 4 on the attribute on which file is ordered. l Assume that the blocks of a relation are stored contiguously l Cost estimate (number of disk blocks to be scanned): 4 cost of locating the first tuple by a binary search on the blocks – log 2(br) * (t. T + t. S) 4 If there are multiple records satisfying selection – Add transfer cost of the number of blocks containing records that satisfy selection condition – Will see how to estimate this cost in Chapter 14 Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 11 ©Silberschatz, Korth and Sudarshan
Selections Using Indices n Index scan algorithms: search algorithms that use an index 4 So, selection condition must be on search-key of an index. n A 3 (primary index on candidate key, equality). l Retrieve a single record that satisfies the corresponding equality condition 4 Cost = (hi + 1) * (t. T + t. S) n A 4 (primary index on nonkey, equality) : 4 Retrieve l multiple records. Records will be on consecutive blocks 4 Let b = number of blocks containing matching records 4 Cost = hi * (t. T + t. S) + t. S + t. T * b Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 12 ©Silberschatz, Korth and Sudarshan
Selections Using Indices n A 5 (equality on search-key of secondary index). l Retrieve a single record if the search-key is a candidate key 4 Cost l = (hi + 1) * (t. T + t. S) Retrieve multiple records if search-key is not a candidate key 4 each of n matching records may be on a different block 4 Cost = (hi + n) * (t. T + t. S) – Can be very expensive! Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 13 ©Silberschatz, Korth and Sudarshan
Selections Involving Comparisons n Can implement selections of the form A V (r) or A V (r) by using: l a linear file scan or binary search, l or by using indices in the following ways: n A 6 (primary index, comparison). (Relation is sorted on A) 4 For A V(r) use index to find first tuple v and – then scan relation sequentially from there 4 For A V (r) just scan relation sequentially till first tuple > v; – do not use index Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 14 ©Silberschatz, Korth and Sudarshan
Selections Involving Comparisons n A 7 (secondary index, comparison). 4 For A V(r) use index to find first index entry v and – then scan index sequentially from there, » 4 For to find pointers to records. A V (r) just scan leaf pages of index finding pointers to records, – till first entry > v 4 In either case, retrieve records that are pointed to, – requires an I/O for each record ! – Linear file scan may be cheaper ! Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 15 ©Silberschatz, Korth and Sudarshan
Implementation of Complex Selections n Conjunction: 1 2. . . n(r) n A 8 (conjunctive selection using one index). l Select a combination of i and algorithms A 1 through A 7 that: 4 results l in the least cost for i (r). Test other conditions on tuple after fetching it into memory buffer. n A 9 (conjunctive selection using multiple-key index). l Use appropriate composite (multiple-key) index if available(? ). Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 16 ©Silberschatz, Korth and Sudarshan
Implementation of Complex Selections n Conjunction: 1 2. . . n(r) n A 10 (conjunctive selection by intersection of identifiers). l Requires indices with record pointers. l Use corresponding index for each condition, and 4 take intersection of all the obtained sets of record pointers. l Then fetch records from file l If some conditions do not have appropriate indices, 4 apply test in memory. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 17 ©Silberschatz, Korth and Sudarshan
Algorithms for Complex Selections n Disjunction: 1 2 . . . n (r). n A 11 (disjunctive selection by union of identifiers). l Applicable iff all conditions have available indices. 4 Otherwise l Use corresponding index for each condition, and 4 take l use linear scan. union of all the obtained sets of record pointers. Then fetch records from file (!) n Negation: (r) l Use linear scan on file l Iff very few records satisfy , and an index is applicable to 4 Find satisfying record pointers using index scan (!) and 4 fetch records from file Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 18 ©Silberschatz, Korth and Sudarshan
Sorting n We may build an index on the relation, and 4 then l use the index to read the relation in sorted order. May lead to one disk block access for each tuple ! n For relations that fit in memory, 4 techniques like quick-sort can be used. n For relations that don’t fit in memory, 4 external sort-merge is a good choice. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 19 ©Silberschatz, Korth and Sudarshan
External Sort-Merge Let M denote memory size (in pages). n Create sorted runs. Let i be 0 initially. 1. Repeatedly do the following till the end of the relation: (a) Read M blocks (=pages) of relation into memory (b) Sort the in-memory blocks (c) Write sorted data to run Ri; increment i. Let the final value of i be N 2. Merge the runs (next slide)…. . Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 20 ©Silberschatz, Korth and Sudarshan
External Sort-Merge (Cont. ) 2. Merge the runs (N-way merge). We assume (for now) that N < M. 1. Use N blocks of memory to buffer input runs, and 1 block to buffer output. Read the first block of each run into its buffer page 2. repeat 1. Select the first record (in sort order) among all buffer pages 2. Write the record to the output buffer. If the output buffer is full write it to disk. 3. Delete the record from its input buffer page. If the buffer page becomes empty then read the next block (if any) of the run into the buffer. until all input buffer pages are empty: Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 21 ©Silberschatz, Korth and Sudarshan
External Sort-Merge (Cont. ) n If N M, 4 several l merge passes are required. In each pass, 4 contiguous l groups of M - 1 runs are merged. A pass reduces the number of runs by a factor of M -1, and 4 creates 4 E. g. runs longer by the same factor. If M=11, and there are 90 runs, – one pass reduces the number of runs to 9, » l Each: 10 times the size of the initial runs Repeated passes are performed 4 till all runs have been merged into one. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 22 ©Silberschatz, Korth and Sudarshan
Example: External Sorting Using Sort-Merge Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 23 ©Silberschatz, Korth and Sudarshan
External Merge Sort (Cont. ) n Cost analysis: l Total number of merge passes required: log. M– 1(br /M) (where br /M is the number of runs) l Block transfers for initial run creation (as well as in each pass) is: 2 br 4 for final pass, we don’t count write cost – we ignore final write cost for all operations : » 4 Thus since the output of an operation may be sent to the parent operation without being written to disk total number of block transfers for external sorting: br ( 2 log. M– 1(br / M) + 1) l Seeks: next slide Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 24 ©Silberschatz, Korth and Sudarshan
External Merge Sort (Cont. ) n Cost of seeks l During run generation: 4 one 4 seek to read each run and one seek to write each run 2 br / M l During the merge phase 4 Buffer 4 Need size: bb (read/write bb blocks at a time) 2 br / bb seeks for each merge pass – except the final one which does not require a write 4 Total number of seeks: 2 br / M + br / bb (2 log. M– 1(br / M) -1) Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 25 ©Silberschatz, Korth and Sudarshan
Join Operation n Several different algorithms to implement joins: l Nested-loop join l Block nested-loop join l Indexed nested-loop join l Merge-join l Hash-join n Choice based on cost estimate n Examples use the following information l Number of records of customer: 10, 000 depositor: 5000 l Number of blocks of customer: depositor: 100 Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 26 400 ©Silberschatz, Korth and Sudarshan
Nested-Loop Join n To compute theta join r s for each tuple tr in r do begin for each tuple ts in s do begin test pair (tr , ts) to see if they satisfy the join condition if they do, add tr • ts to the result. end l r is called the outer relation and l s the inner relation of the join. n Requires no indices and can be used with any kind of join condition. n Expensive since it examines every pair of tuples in the two relations. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 27 ©Silberschatz, Korth and Sudarshan
Nested-Loop Join (Cont. ) n In the worst case, l if there is enough memory only to hold one block of each relation, the estimated cost is: block transfers = nr bs + br plus: seeks = nr + br n If the smaller relation fits entirely in memory, 4 use l that as the inner relation. Reduces the cost to: 4 br + bs block transfers 4 and 2 seeks Database System Concepts - 5 th Edition, Aug 27, 2005. (why? ) 13. 28 ©Silberschatz, Korth and Sudarshan
Nested-Loop Join (Cont. ) n Assuming worst case memory availability, cost estimate is: l l with depositor as outer relation: 4 5000 400 + 100 = 2, 000, 100 block transfers, 4 5000 + 100 = 5100 seeks with customer as the outer relation: 4 10000 100 + 400 = 1, 000, 400 block transfers and 4 10000 + 400 = 10, 400 seeks (which one is better? ) n If smaller relation (depositor) fits entirely in memory, 4 the cost estimate will be 500 block transfers and 2 seeks. n Block nested-loops algorithm (next slide) is preferable. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 29 ©Silberschatz, Korth and Sudarshan
Block Nested-Loop Join n Variant of nested-loop join in which: l every block of inner relation is paired 4 with every block of outer relation. for each block Br of r do begin for each block Bs of s do begin for each tuple tr in Br do begin for each tuple ts in Bs do begin Check if (tr , ts) satisfy the join condition if they do, add tr • ts to the result. end end Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 30 ©Silberschatz, Korth and Sudarshan
Block Nested-Loop Join (Cont. ) n Worst case estimate: 4 br 4+ l bs + br block transfers 2 * br seeks Each block in the inner relation s is: 4 read once for each block in the outer relation 4 (instead of once for each tuple in the outer relation) n Best case: 4 br 4+ + bs block transfers 2 seeks. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 31 ©Silberschatz, Korth and Sudarshan
Block Nested-Loop Join (Cont. ) n Improvements to nested loop and block nested loop algorithms: l In block nested-loop, 4 use M - 2 disk blocks as blocking unit of outer relations, – where M = memory size in blocks (=pages); 4 use 4 l remaining two blocks : to buffer inner relation and output Cost = br / (M-2) bs + br block transfers + 2 br / (M-2) seeks If equi-join attribute forms a key for inner relation, 4 stop l Scan inner loop forward and backward alternately, 4 to l inner loop on first match make use of the blocks remaining in buffer (with LRU replacement) Use index on inner relation if available (next slide) Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 32 ©Silberschatz, Korth and Sudarshan
Indexed Nested-Loop Join n Index lookups can replace file scans if: 4 join 4 an l is an equi-join or natural join and index is available on the inner relation’s join attribute Can also construct an index just to compute a join. n For each tuple tr in the outer relation r, 4 use the index to look up tuples in s that: – satisfy the join condition with tuple tr. n Worst case: buffer has space for only one page of r, and, 4 for each tuple in r, – we perform an index lookup on s. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 33 ©Silberschatz, Korth and Sudarshan
Indexed Nested-Loop Join n Cost of the join: 4 br l Where c is the cost of traversing index and 4 l (t. T + t. S) + nr c fetching all matching s tuples for one tuple of r c can be estimated as: 4 cost of a single selection on s using the join condition. n If indices are available on join attributes of both r and s, l use the relation with fewer tuples as : 4 the outer relation. – i. e. smaller nr Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 34 ©Silberschatz, Korth and Sudarshan
Example of Nested-Loop Join Costs n Compute depositor 4 with customer, depositor as the outer relation. n Let customer have : 4 a primary B+-tree index on the join attribute customer-name, – which contains 20 entries in each index node. n Since customer has 10, 000 tuples, 4 the height of the tree is 4, 4 and one more access is needed to find the actual data n depositor has 5000 tuples Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 35 ©Silberschatz, Korth and Sudarshan
Example of Nested-Loop Join Costs n Cost of block nested loops join: l 400*100 + 100 = 40, 100 block transfers + 2 * 100 = 200 seeks ! 4 assuming 4 may worst case memory ! be significantly less with more memory n Cost of indexed nested loops join l 100 + 5000 * 5 = 25, 100 block transfers and seeks ! l CPU cost likely to be less than that of block nested loops join Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 36 ©Silberschatz, Korth and Sudarshan
Merge-Join 1. Sort both relations on their join attribute Ø (if not already sorted on the join attributes). 2. Merge the sorted relations to join them: 1. Join step is similar to the merge stage of the sort-merge algorithm. 2. Main difference is handling of duplicate values in join attribute: Ø Ø every pair with same value on join attribute must be matched Detailed algorithm in book Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 37 ©Silberschatz, Korth and Sudarshan
Merge-Join (Cont. ) n Can be used only for equi-joins and natural joins n Each block needs to be read only once 4 assuming all tuples for any given value of the join attributes fit in memory n Thus the cost of merge join is: 4 br l + bs block transfers + br / bb + bs / bb seeks + the cost of sorting 4 if relations are unsorted. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 38 ©Silberschatz, Korth and Sudarshan
Merge-Join (Cont. ) n hybrid merge-join: 4 Iff one relation (R) is sorted, 4 and the other (S) has a secondary B+-tree index on the join attribute l Merge the sorted relation (R) with the leaf entries of the B+-tree (S). l Sort the result on the addresses of the unsorted relation’s tuples (S) l Scan the unsorted relation (S) in physical address order and 4 merge with previous result, to replace addresses by the actual tuples 4 Sequential scan more efficient than random lookup Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 39 ©Silberschatz, Korth and Sudarshan
Hash-Join n Applicable for equi-joins and natural joins. n A hash function h is used to partition tuples of both relations n h maps Join. Attrs values to {0, 1, . . . , n}, 4 where Join. Attrs denotes : – the common attributes of r and s used in the natural join. l r 0, r 1, . . . , rn denote partitions of r tuples 4 Each l tuple tr r is put in partition ri where i = h(tr [Join. Attrs]). r 0, , r 1. . . , rn denotes partitions of s tuples 4 Each tuple ts s is put in partition si, where i = h(ts [Join. Attrs]). n Note: In book, ri is denoted as Hri , si is denoted as Hsi and n is denoted as nh. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 40 ©Silberschatz, Korth and Sudarshan
Hash-Join (Cont. ) Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 41 ©Silberschatz, Korth and Sudarshan
Hash-Join (Cont. ) n r tuples in ri : 4 need only to be compared with s tuples in si 4 Need not be compared with s tuples in any other partition, n since: l an r tuple and an s tuple that satisfy the join condition : 4 will l have the same value for the join attributes. If that value is hashed to some value i, 4 the r tuple has to be in ri and 4 the s tuple in si. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 42 ©Silberschatz, Korth and Sudarshan
Hash-Join Algorithm The hash-join of r and s is computed as follows. 1. Partition the relation s using hashing function h. When partitioning a relation: Ø one block of memory is reserved as the output buffer for each partition. 2. Partition the relation r similarly. 3. For each i: (a) Load si into memory and build an in-memory hash index on it: Ø using the join attribute. Ø This hash index uses a different hash function than the earlier one h. (b) Read the tuples in ri from the disk one by one. For each tuple tr : Ø locate each matching tuple ts in si : § using the in-memory hash index. § Output the concatenation of their attributes. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 43 ©Silberschatz, Korth and Sudarshan
Hash-Join algorithm (Cont. ) n Relation s is called the build input and r is called the probe input. n The value n and the hash function h is chosen such that: 4 l each si should fit in memory. Typically n is chosen as bs/M * f where: 4 f is a “fudge factor”, typically around 1. 2 4 (20% l more partitions; i. e. , each partition 20% free) The probe relation partitions ri 4 need not fit in memory Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 44 ©Silberschatz, Korth and Sudarshan
Hash-Join algorithm (Cont. ) n Recursive partitioning required 4 if number of partitions n is – greater than number of pages M of memory. l instead of partitioning n ways, 4 use l M – 1 (bigger) partitions for s Further partition the M – 1 (big) partitions 4 using a different hash function l Use same partitioning method on r l Rarely required: 4 e. g. , recursive partitioning not needed for : – relations of 1 GB (or less) – with memory size of 2 MB, – with block size of 4 KB. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 45 ©Silberschatz, Korth and Sudarshan
Handling of Overflows n Partitioning is said to be skewed : 4 if some partitions have : – significantly more tuples than some others n Hash-table overflow occurs in partition si : l if si does not fit in memory. n Reasons could be: l Too Many tuples in s with same value for join attributes l Bad hash function Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 46 ©Silberschatz, Korth and Sudarshan
Handling of Overflows n Overflow resolution can be done in build phase l Partition si is further partitioned 4 using l different hash function. Partition ri must be similarly partitioned. n Overflow avoidance performs partitioning carefully l to avoid overflows during build phase l E. g. partition build relation into many partitions, 4 then combine them n Both approaches fail with: l l large numbers of duplicates Fallback option: 4 use block nested loops join on overflowed partitions Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 47 ©Silberschatz, Korth and Sudarshan
Cost of Hash-Join n If recursive partitioning is not required: 4 cost of hash join is: 3(br + bs) + 4 nh block transfers + 2( br / bb + bs / bb ) + 2 nh seeks n If recursive partitioning required: l number of passes required for partitioning build relation s is: 4 log. M– 1(bs) – 1 l best to choose the smaller relation as the build relation. l Total cost estimate is: 2(br + bs ) log. M– 1(bs) – 1 + br + bs block transfers + 2( br / bb + bs / bb ) log. M– 1(bs) – 1 + 2 nh seeks n If the entire build input can be kept in main memory : l no partitioning is required l Cost estimate goes down to br + bs block transfers and 2 seeks. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 48 ©Silberschatz, Korth and Sudarshan
Example of Cost of Hash-Join customer depositor n Assume that memory size is 20 blocks n bdepositor= 100 and bcustomer = 400. n depositor is to be used as build input. 4 Partition it into five partitions, each of size 20 blocks. – This partitioning can be done in one pass. 4 Similarly, partition customer into five partitions, each of size 80. – This is also done in one pass. n Therefore total cost, ignoring cost of writing partially filled blocks: 4 3(100 + 400) = 1500 block transfers + 2( 100/3 + 400/3 ) +2*5 = 336 +10 seeks Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 49 ©Silberschatz, Korth and Sudarshan
Hybrid Hash–Join n Useful when: 4 memory 4 and size are relatively large, the build input is bigger than memory. n Main feature of hybrid hash-join: 4 Keep the first partition of the build relation (Hs 0) in memory. n E. g. With memory size of 25 blocks, 4 depositor 4 each l can be partitioned into five partitions, of size 20 blocks. Division of memory: 4 The first partition occupies 20 blocks of memory 41 block is used for input, and 41 block each for buffering the other 4 partitions. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 50 ©Silberschatz, Korth and Sudarshan
Hybrid Hash–Join n customer is similarly partitioned into five partitions each of size 80 l the first (Hr 0) is used right away for probing, 4 instead of being written out n Cost of 3(80 + 320) + 20 +80 = 1300 block transfers for 4 hybrid hash-join, – instead of 1500 with plain hash-join. n Hybrid hash-join most useful if M >> Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 51 ©Silberschatz, Korth and Sudarshan
Complex Joins n Join with a conjunctive condition: r l 1 2. . . n s Either use nested loops / block nested loops, or l Compute the result of one of the simpler joins r i s 4 final result comprises those tuples in the intermediate result that satisfy the remaining conditions 1 . . . i – 1 i +1 . . . n n Join with a disjunctive condition r l 1 2 . . . n s Either use nested loops / block nested loops, or l Compute as the union of the records in individual joins r (r 1 s) (r Database System Concepts - 5 th Edition, Aug 27, 2005. 2 s) . . . (r 13. 52 n i s: s) ©Silberschatz, Korth and Sudarshan
Other Operations n Duplicate elimination can be 4 l implemented via hashing or sorting. On sorting: duplicates will come adjacent to each other, and – all but one set of duplicates can be deleted. l Optimization: duplicates can be deleted – during run generation or: – at intermediate merge steps in external sort-merge. l Hashing is similar: 4 duplicates will come into the same bucket. n Projection: l perform projection on each tuple 4 followed by duplicate elimination. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 53 ©Silberschatz, Korth and Sudarshan
Other Operations : Aggregation n Aggregation can be implemented in a manner 4 similar l Sorting or hashing can be used 4 to 4 l to duplicate elimination. bring tuples in the same group together, and then the aggregate functions can be applied on each group. Optimization: combine tuples in the same group 4 during run generation and intermediate merges, – by computing partial aggregate values 4 For count, min, max, sum: – keep aggregate values on tuples found so far in the group. – When combining partial aggregate for count, » 4 For add up the aggregates avg, keep sum and count, and » divide sum by count at the end Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 54 ©Silberschatz, Korth and Sudarshan
Other Operations : Set Operations n Set operations ( , and -): can 4 either use variant of merge-join after sorting, or 4 variant of hash-join. n E. g. , Set operations using hashing: 1. Partition both relations using the same hash function 2. Process each partition i as follows. 1. Using a different hashing function, build an in-memory hash index on ri. 2. Process si as follows: l r s: 1. Add tuples in si to the hash index if they are not already in it. 2. At end of si , add the tuples in the hash index to the result. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 55 ©Silberschatz, Korth and Sudarshan
Other Operations : Set Operations l r s: 1. output tuples in si to the result - if they are already there in the hash index l r – s: 1. for each tuple in si, - if it is there in the hash index, delete it from the index. 2. At end of si , add remaining tuples in the hash index to the result. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 56 ©Silberschatz, Korth and Sudarshan
Other Operations : Outer Join n Outer join can be computed either as l A join followed by addition of null-padded non-participating tuples. l Or, by modifying the join algorithms. n Modifying merge-join to compute r s s, non-participating tuples are those in r – R(r l In r l Modify merge-join to compute r 4 During s) s: merging, – for every tuple tr from r that do not match any tuple in s, » l output tr padded with nulls. Right outer-join and full outer-join can be computed similarly. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 57 ©Silberschatz, Korth and Sudarshan
Other Operations : Outer Join n Modifying hash-join to compute r l If r is probe relation, 4 output l s non-matching r tuples padded with nulls If r is build relation, 4 when 4 At probing keep track of which r tuples matched s tuples. end of si , output non-matched r tuples padded with nulls Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 58 ©Silberschatz, Korth and Sudarshan
Evaluation of Expressions n So far: we have seen algorithms for individual operations n Alternatives for evaluating an entire expression tree: l Materialization: generate results of an expression whose inputs are relations or are already computed, 4 materialize l (store) it on disk. Pipelining: pass on tuples to parent operations 4 even as an operation is being executed n We study above alternatives in more detail Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 59 ©Silberschatz, Korth and Sudarshan
Materialization n Materialized evaluation: l evaluate one operation at a time, starting at the lowest-level. 4 Use intermediate results materialized into temporary relations to evaluate next-level operations. l E. g. , in figure below, 4 compute 4 then 4 and store compute its join with customer, finally compute the projections on customer-name. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 60 ©Silberschatz, Korth and Sudarshan
Materialization (Cont. ) n Materialized evaluation is always applicable n Cost of writing results to disk and reading them back can be quite high l Our cost formulas for operations ignore cost of writing results to disk, so 4 Overall cost = Sum of costs of individual operations + cost of writing intermediate results to disk n Double buffering: l use two output buffers for each operation, l when one is full l 4 write it to disk 4 while the other is getting filled Allows overlap of 4 disk writes with computation and reduces execution time Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 61 ©Silberschatz, Korth and Sudarshan
Pipelining n Pipelined evaluation : 4 evaluate 4 passing several operations simultaneously, the results of one operation on to the next. n E. g. , in previous expression tree, l don’t store result of: 4 instead, l pass tuples directly to the join. . Similarly, don’t store result of join, 4 pass tuples directly to projection. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 62 ©Silberschatz, Korth and Sudarshan
Pipelining n Much cheaper than materialization: 4 no need to store a temporary relation to disk. n Pipelining may not always be possible : 4 e. g. , sort, hash-join. n For pipelining to be effective: 4 use evaluation algorithms that generate output tuples – even as tuples are received for inputs to the operation. n Pipelines can be executed in two ways: l demand driven and l producer driven Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 63 ©Silberschatz, Korth and Sudarshan
Pipelining (Cont. ) n In demand driven or lazy evaluation: l system repeatedly requests next tuple from top level operation l Each operation requests next tuple from children operations as required, 4 in l order to output its next tuple In between calls, 4 operation has to maintain “state” – so it knows what to return next Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 64 ©Silberschatz, Korth and Sudarshan
Pipelining (Cont. ) n In producer driven or eager pipelining l Operators produce tuples eagerly and pass them up to their parents 4 Need buffer maintained between operators, 4 child puts tuples in buffer, 4 parent 4 If removes tuples from buffer is full, – child waits till there is space in the buffer, – and then generates more tuples l System schedules operations that 4 have space in output buffer and can process more input tuples n Alternative name: l pull and push models of pipelining Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 65 ©Silberschatz, Korth and Sudarshan
Pipelining (Cont. ) n Implementation of demand-driven pipelining l Each operation is implemented as an iterator implementing the following operations 4 open() – E. g. file scan: initialize file scan » state: pointer to beginning of file – E. g. merge join: sort relations; » 4 state: pointers to beginning of sorted relations next() – E. g. for file scan: Output next tuple, and advance and store file pointer – E. g. for merge join: continue with merge from earlier state till next output tuple is found. Save pointers as iterator state. 4 close() Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 66 ©Silberschatz, Korth and Sudarshan
Evaluation Algorithms for Pipelining n Some algorithms are not able to output results 4 even l as they get input tuples E. g. merge join, or hash join 4 intermediate results written to disk and then read back n Algorithm variants: l 4 to generate (at least some) results on the fly, 4 as input tuples are read in E. g. hybrid hash join generates output tuples even as probe relation tuples in the in-memory partition (partition Hr 0) are read in. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 67 ©Silberschatz, Korth and Sudarshan
Evaluation Algorithms for Pipelining l Pipelined join technique: 4 Hybrid hash join, 4 modified to buffer partition 0 tuples of both relations in-memory, – reading them as they become available, – and output results of any matches between partition 0 tuples 4 When a new r 0 tuple is found, – match it with existing s 0 tuples, – output matches, and save it in r 0 4 Symmetrically for s 0 tuples Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 68 ©Silberschatz, Korth and Sudarshan
End of Chapter Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See www. db-book. com for conditions on re-use
Figure 13. 2 Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 70 ©Silberschatz, Korth and Sudarshan
Complex Joins n Join involving three relations: loan n Strategy 1. Compute depositor customer; use result to compute loan (depositor customer) n Strategy 2. Computer loan depositor first, and then join the result with customer. n Strategy 3. Perform the pair of joins at once. Build and index on loan for loan-number, and on customer for customer-name. l For each tuple t in depositor, look up the corresponding tuples in customer and the corresponding tuples in loan. l Each tuple of deposit is examined exactly once. n Strategy 3 combines two operations into one special-purpose operation that is more efficient than implementing two joins of two relations. Database System Concepts - 5 th Edition, Aug 27, 2005. 13. 71 ©Silberschatz, Korth and Sudarshan
- Slides: 71