Cost Analysis of Database Query Processing: Sort-Merge, Indexed Join, Merge-Join, and Hash, Slides of Database Management Systems (DBMS)

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

2011/2012

Uploaded on 01/31/2012

beatryx
beatryx 🇺🇸

4.6

(16)

289 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Computing & Information Sciences
Kansas State University
Wednesday, 08 Nov 2006CIS 560: Database System Concepts
Lecture 32 of 42
Monday, 06 November 2006
William H. Hsu
Department of Computing and Information Sciences, KSU
KSOL course page: http://snipurl.com/va60
Course web site: http://www.kddresearch.org/Courses/Fall-2006/CIS560
Instructor home page: http://www.cis.ksu.edu/~bhsu
Reading for Next Class:
MySQL 5.1 documentation
MySQL Primer 1 of 2, Fast Joins Concluded
Discussion: DB Norm., Practical DBs
Computing & Information Sciences
Kansas State University
Wednesday, 08 Nov 2006CIS 560: Database System Concepts
Chapter 13: Query Processing
Chapter 13: Query Processing
zOverview
zMeasures of Query Cost
zSelection Operation
zSorting
zJoin Operation
zOther Operations
zEvaluation of Expressions
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Cost Analysis of Database Query Processing: Sort-Merge, Indexed Join, Merge-Join, and Hash and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University

Lecture 32 of 42

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

MySQL Primer 1 of 2, Fast Joins Concluded

Discussion: DB Norm., Practical DBs

Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University

Chapter 13: Query ProcessingChapter 13: Query Processing

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

Basic Steps in Query Processing Basic Steps in Query Processing

  1. Parsing and translation
  2. Optimization
  3. Evaluation

Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University

Measures of Query Cost:

Review

Measures of Query Cost:

Review

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

Example: External Sorting Using Sort-Merge Example: External Sorting Using Sort-Merge

Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University

External Merge Sort (Cont.) External Merge Sort (Cont.)

z Cost analysis:

’ 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

External Merge Sort (Cont.) External Merge Sort (Cont.)

z Cost of seeks ’ During run generation: one seek to read each run and one seek to write each run Ö 2b (^) r / M ⎤ ’ During the merge phase Ö Buffer size: b (^) b (read/write b (^) b blocks at a time) Ö Need 2b (^) r / bb ⎤ seeks for each merge pass ‹ except the final one which does not require a write Ö Total number of seeks: 2b (^) 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

Join OperationJoin Operation

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

Block Nested-Loop JoinBlock Nested-Loop Join

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 trts to the result. end end end end

Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University

Block Nested-Loop Join (Cont.) Block Nested-Loop Join (Cont.)

z Worst case estimate: b (^) rb (^) 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 + 2br / (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

Indexed Nested-Loop JoinIndexed Nested-Loop Join

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 (^) rc ’ 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

Example of Nested-Loop Join Costs Example of Nested-Loop Join Costs

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

Hash-Join Hash-Join

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 (^) rr is put in partition ri where i = h(tr [JoinAttrs]). ’ r 0 ,, r 1.. ., rn denotes partitions of s tuples Ö Each tuple t (^) ss 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

Hash-Join (Cont.)Hash-Join (Cont.)

Computing & Information Sciences CIS 560: Database System Concepts Wednesday, 08 Nov 2006 Kansas State University

Hash-Join (Cont.)Hash-Join (Cont.)

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

Hash-Join Algorithm Hash-Join Algorithm

  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 r similarly.
  3. For each i: (a) Load s (^) i 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 r (^) i from the disk one by one. For each tuple t (^) r locate each matching tuple t (^) s in s (^) i using the in-memory hash index. Output the concatenation of their attributes.

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

Cost of Hash-Join Cost of Hash-Join

z If recursive partitioning is not required: cost of hash join is 3( b (^) r + bs) +4 ∗ n (^) h block transfers +

2 ( ⎡ b r / b b ⎤ + ⎡ b s / b b ⎤) seeks

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 + bslogM– 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.