Query Processing-Database System Concepts-Lecture 28 Slides-Computer Science, Slides of Database Management Systems (DBMS)

The first half of the course will emphasize taxonomies of databases. The second half will delve deeper into database theory and examine topics in theoretical and applied databases. Query Processing, Fast Joins, Parsing and Translation, Optimization, Evaluation, Query Cost, Selection Operation, Sorting, Sort-merge, Join Operation, Nested-loop, Merge-join

Typology: Slides

2011/2012

Uploaded on 01/31/2012

beatryx
beatryx 🇺🇸

4.6

(16)

289 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Computing & Information Sciences
Kansas State University
Monday, 06 Nov 2006CIS 560: Database System Concepts
Lecture 31 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:
Second half of Chapter 13, Silberschatz et al., 5th edition
Fast Joins (Continued)
Discussion: DB Impl., Normalization Review
Computing & Information Sciences
Kansas State University
Monday, 06 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
pfe
pff

Partial preview of the text

Download Query Processing-Database System Concepts-Lecture 28 Slides-Computer Science and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

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

Lecture 31 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: Second half of Chapter 13, Silberschatz et al. , 5 th^ edition

Fast Joins (Continued)

Discussion: DB Impl., Normalization Review

Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 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 Monday, 06 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 Monday, 06 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 Monday, 06 Nov 2006 Kansas State University

z A2 (binary search). Applicable if selection is an

equality comparison on the attribute on which

file is ordered.

’ Assume that the blocks of a relation are stored contiguously ’ Cost estimate (number of disk blocks to be scanned): Ö cost of locating the first tuple by a binary search on the blocks ‹ ⎡ log 2 ( br ) ⎤ *** (** tT + tS ) Ö 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

Select [2]: Binary Search

Review

Select [2]: Binary Search

Review

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

Selections Using IndicesSelections Using Indices

z Index scan – search algorithms that use an index ’ selection condition must be on search-key of index. z A3 ( primary index on candidate key, equality ). Retrieve a single record that satisfies the corresponding equality condition ’ Cost = ( h (^) i + 1) * ( t (^) T + t (^) S ) z A4 ( primary index on nonkey, equality) Retrieve multiple records. ’ Records will be on consecutive blocks Ö Let b = number of blocks containing matching records ’ Cost = h (^) i ** ( t (^) T + t (^) S ) + tS + t (^) T *** b* z A5 ( equality on search-key of secondary index). ’ Retrieve a single record if the search-key is a candidate key Ö Cost = (hi + 1) * ( tT + tS ) ’ Retrieve multiple records if search-key is not a candidate key Ö each of n matching records may be on a different block Ö Cost = ( hi + *n) ** ( tT + t (^) S ) ‹ Can be very expensive!

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

Selections Involving ComparisonsSelections Involving Comparisons

z Can implement selections of the form σ AV ( r ) or σ AV ( r ) by using ’ a linear file scan or binary search, ’ or by using indices in the following ways: z A6 ( primary index, comparison). (Relation is sorted on A) Ö For σ AV(r) use index to find first tuplev and scan relation sequentially from there Ö For σ AV ( r ) just scan relation sequentially till first tuple > v; do not use index z A7 ( secondary index, comparison ). Ö For σ AV(r) use index to find first index entryv and scan index sequentially from there, to find pointers to records. Ö For σ AV ( r ) just scan leaf pages of index finding pointers to records, till first entry > v Ö In either case, retrieve records that are pointed to ‹ requires an I/O for each record ‹ Linear file scan may be cheaper

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

Implementation of Complex Selections Implementation of Complex Selections

z Conjunction: σ (^) θ 1 ∧ (^) θ 2 ∧... (^) θ n ( r) z A8 ( conjunctive selection using one index). ’ Select a combination of θ i and algorithms A1 through A7 that results in the least cost for σθ i ( r). ’ Test other conditions on tuple after fetching it into memory buffer. z A9 ( conjunctive selection using multiple-key index ). ’ Use appropriate composite (multiple-key) index if available. z A10 ( conjunctive selection by intersection of identifiers). ’ Requires indices with record pointers. ’ Use corresponding index for each condition, and take intersection of all the obtained sets of record pointers. ’ Then fetch records from file ’ If some conditions do not have appropriate indices, apply test in memory.

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

External Sort-MergeExternal Sort-Merge

1. Create sorted runs. Let i be 0 initially.

Repeatedly do the following till the end of the relation:

(a) Read M blocks 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)…..

Let M denote memory size (in pages).

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

External Sort-Merge (Cont.) 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. 3. until all input buffer pages are empty:

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

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

z If N ≥ M , several merge passes are required.

’ In each pass, contiguous groups of M - 1 runs are merged. ’ A pass reduces the number of runs by a factor of M -1, and creates runs longer by the same factor. Ö E.g. If M=11, and there are 90 runs, one pass reduces the number of runs to 9, each 10 times the size of the initial runs ’ Repeated passes are performed till all runs have been merged into one.

Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 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 Monday, 06 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 Monday, 06 Nov 2006 Kansas State University

Nested-Loop JoinNested-Loop Join

z To compute the 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 end z r is called the outer relation and s the inner relation of the join. z Requires no indices and can be used with any kind of join condition. z Expensive since it examines every pair of tuples in the two relations.

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

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

z In the worst case, if there is enough memory only to hold one block of each relation, the estimated cost is n (^) rb (^) s + b (^) r block transfers, plus n (^) r + b (^) r seeks z If the smaller relation fits entirely in memory, use that as the inner relation. ’ Reduces cost to b (^) r + b (^) s block transfers and 2 seeks z Assuming worst case memory availability cost estimate is ’ with depositor as outer relation: Ö 5000 ∗ 400 + 100 = 2,000,100 block transfers, Ö 5000 + 100 = 5100 seeks ’ with customer as the outer relation Ö 10000 ∗ 100 + 400 = 1,000,400 block transfers and 10,400 seeks z If smaller relation ( depositor) fits entirely in memory, the cost estimate will be 500 block transfers. z Block nested-loops algorithm (next slide) is preferable.

Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 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 Monday, 06 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 Monday, 06 Nov 2006 Kansas State University

Merge-JoinMerge-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
    3. Detailed algorithm in book

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

Merge-Join (Cont.) Merge-Join (Cont.)

z Can be used only for equi-joins and natural joins z Each block needs to be read only once (assuming all tuples for any given value of the join attributes fit in memory z Thus the cost of merge join is: b (^) r + b (^) s block transfers + ⎡ b (^) r / b (^) b ⎤ + ⎡ b (^) s / b (^) b ⎤ seeks ’ + the cost of sorting if relations are unsorted. z hybrid merge-join: If one relation is sorted, and the other has a secondary B+^ -tree index on the join attribute ’ Merge the sorted relation with the leaf entries of the B+-tree. ’ Sort the result on the addresses of the unsorted relation’s tuples ’ Scan the unsorted relation in physical address order and merge with previous result, to replace addresses by the actual tuples Ö Sequential scan more efficient than random lookup

Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 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 Monday, 06 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 Monday, 06 Nov 2006 Kansas State University

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

z The value n and the hash function h is chosen such that each si should fit in memory. ’ Typically n is chosen as ⎡bs /M⎤ * f where f is a “fudge factor”, typically around 1. ’ The probe relation partitions s (^) i need not fit in memory z Recursive partitioning required if number of partitions n is greater than number of pages M of memory. ’ instead of partitioning n ways, use M – 1 partitions for s ’ Further partition the M – 1 partitions using a different hash function ’ Use same partitioning method on r ’ Rarely required: e.g., recursive partitioning not needed for relations of 1GB or less with memory size of 2MB, with block size of 4KB.

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

Handling of OverflowsHandling of Overflows

z Partitioning is said to be skewed if some partitions have significantly more tuples than some others z Hash-table overflow occurs in partition si if si does not fit in memory. Reasons could be ’ Many tuples in s with same value for join attributes ’ Bad hash function z Overflow resolution can be done in build phase ’ Partition s (^) i is further partitioned using different hash function. ’ Partition r (^) i must be similarly partitioned. z Overflow avoidance performs partitioning carefully to avoid overflows during build phase ’ E.g. partition build relation into many partitions, then combine them z Both approaches fail with large numbers of duplicates ’ Fallback option: use block nested loops join on overflowed partitions

Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 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.