







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth analysis of various query processing techniques, including external sort-merge, indexed nested-loop join, merge-join, and hash-join. The cost analysis of each method, including the number of block transfers and seeks required. It also discusses the applicability of each method for different types of joins and the impact of memory availability on cost estimates.
Typology: Slides
1 / 13
This page cannot be seen from the preview
Don't miss anything!








Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University
Monday, 06 November 2006
William H. Hsu Department of Computing and Information Sciences, KSU
KSOL course page: http://snipurl.com/va Course web site: http://www.kddresearch.org/Courses/Fall-2006/CIS Instructor home page: http://www.cis.ksu.edu/~bhsu
Reading for Next Class: MySQL 5.1 documentation
Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University
z Overview z Measures of Query Cost z Selection Operation z Sorting z Join Operation z Other Operations z Evaluation of Expressions
Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University
Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University
z Cost is generally measured as total elapsed time for answering query Many factors contribute to time cost Ö disk accesses, CPU , or even network communication z Typically disk access is the predominant cost, and is also relatively easy to estimate. Measured by taking into account Number of seeks * average-seek-cost Number of blocks read * average-block-read-cost Number of blocks written * average-block-write-cost Ö 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
Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University
Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University
Total number of merge passes required: ⎡log M –1( b (^) r /M) ⎤. Block transfers for initial run creation as well as in each pass is 2 b (^) r Ö for final pass, we don’t count write cost we ignore final write cost for all operations since the output of an operation may be sent to the parent operation without being written to disk Ö Thus total number of block transfers for external sorting: br ( 2 ⎡log M –1 ( br / M) ⎤ + 1) Seeks: next slide
Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University
z Cost of seeks During run generation: one seek to read each run and one seek to write each run Ö 2 ⎡ b (^) r / M ⎤ During the merge phase Ö Buffer size: b (^) b (read/write b (^) b blocks at a time) Ö Need 2 ⎡ b (^) r / bb ⎤ seeks for each merge pass except the final one which does not require a write Ö Total number of seeks: 2 ⎡ b (^) r / M ⎤ + ⎡ b (^) r / bb ⎤ ( 2 ⎡log M –1( b (^) r / M) ⎤ -1)
Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University
z Several different algorithms to implement joins Nested-loop join Block nested-loop join Indexed nested-loop join Merge-join Hash-join z Choice based on cost estimate z Examples use the following information Number of records of customer : 10,000 depositor : 5000 Number of blocks of customer : 400 depositor : 100
Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University
z Variant of nested-loop join in which every block of inner relation is paired 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 end end
Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University
z Worst case estimate: b (^) r ∗ b (^) s + b (^) r block transfers + 2 * b (^) r seeks Each block in the inner relation s is read once for each block in the outer relation (instead of once for each tuple in the outer relation z Best case: b (^) r + b (^) s block transfers + 2 seeks. z Improvements to nested loop and block nested loop algorithms: In block nested-loop, use M — 2 disk blocks as blocking unit for outer relations, where M = memory size in blocks; use 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 or inner relation, stop inner loop on first match Scan inner loop forward and backward alternately, to make use of the blocks remaining in buffer (with LRU replacement) Use index on inner relation if available (next slide)
Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University
z Index lookups can replace file scans if join is an equi-join or natural join and an index is available on the inner relation’s join attribute Ö Can construct an index just to compute a join. z For each tuple tr in the outer relation r, use the index to look up tuples in s that satisfy the join condition with tuple tr. z Worst case: buffer has space for only one page of r , and, for each tuple in r , we perform an index lookup on s. z Cost of the join: b (^) r ( tT + tS ) + n (^) r ∗ c Where c is the cost of traversing index and fetching all matching s tuples for one tuple or r c can be estimated as cost of a single selection on s using the join condition. z If indices are available on join attributes of both r and s, use the relation with fewer tuples as the outer relation.
Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University
z Compute depositor customer, with depositor as the outer relation. z Let customer have a primary B+-tree index on the join attribute customer-name, which contains 20 entries in each index node. z Since customer has 10,000 tuples, the height of the tree is 4, and one more access is needed to find the actual data z depositor has 5000 tuples z Cost of block nested loops join 400*100 + 100 = 40,100 block transfers + 2 * 100 = 200 seeks Ö assuming worst case memory Ö may be significantly less with more memory z Cost of indexed nested loops join 100 + 5000 * 5 = 25,100 block transfers and seeks. CPU cost likely to be less than that for block nested loops join
Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University
z Applicable for equi-joins and natural joins. z A hash function h is used to partition tuples of both relations z h maps JoinAttrs values to {0, 1, ..., n }, where JoinAttrs denotes the common attributes of r and s used in the natural join. r 0 , r 1 ,.. ., rn denote partitions of r tuples Ö Each tuple t (^) r ∈ r is put in partition ri where i = h(tr [JoinAttrs]). r 0 ,, r 1.. ., rn denotes partitions of s tuples Ö Each tuple t (^) s ∈ s is put in partition s (^) i , where i = h(ts [JoinAttrs]).
z Note: In book, r (^) i is denoted as H (^) ri, s (^) i is denoted as H (^) s i and n is denoted as n (^) h.
Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University
Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University
z r tuples in ri need only to be compared with s tuples in si Need not be compared with s tuples in any other partition, since: an r tuple and an s tuple that satisfy the join condition will have the same value for the join attributes. If that value is hashed to some value i , the r tuple has to be in r (^) i and the s tuple in si.
Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University
The hash-join of r and s is computed as follows.
Relation s is called the build input and r is called the probe input.
Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University
z If recursive partitioning is not required: cost of hash join is 3( b (^) r + bs) +4 ∗ n (^) h block transfers +
z If recursive partitioning required: number of passes required for partitioning build relation s is ⎡ logM– 1 ( bs ) – 1⎤ best to choose the smaller relation as the build relation. Total cost estimate is: 2 (br + bs ⎡ logM– 1 ( bs ) – 1⎤ + br + bs block transfers + 2 (⎡ br / bb ⎤ + ⎡ bs / bb ⎤) ⎡ logM– 1 ( bs ) – 1⎤ seeks z If the entire build input can be kept in main memory no partitioning is required Cost estimate goes down to b (^) r + bs.