Query Processing in Database Systems: Measuring Cost and Processing Steps, Slides of Database Management Systems (DBMS)

A part of the cis 560: database system concepts course materials from kansas state university, dated november 1, 2006. It discusses the basic steps in query processing, focusing on measures of query cost, selection operation, sorting, join operation, and evaluation of expressions. The document also covers the use of indices for selections and complex selections, as well as sorting techniques like external merge sort and indexed nested-loop join.

Typology: Slides

2011/2012

Uploaded on 01/31/2012

beatryx
beatryx 🇺🇸

4.6

(16)

289 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Computing & Information Sciences
Kansas State University
Friday, 01 Nov 2006CIS 560: Database System Concepts
Lecture 29 of 42
Wednesday, 01 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
Query Processing: Fast Joins
Discussion: Exam 2 Review
Computing & Information Sciences
Kansas State University
Friday, 01 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 in Database Systems: Measuring Cost and Processing Steps and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

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

Lecture 29 of 42

Wednesday, 01 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

Query Processing: Fast Joins

Discussion: Exam 2 Review

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

Basic Steps in Query Processing (Cont.) Basic Steps in Query Processing (Cont.)

z Parsing and translation ’ translate the query into its internal form. This is then translated into relational algebra. ’ Parser checks syntax, verifies relations z Evaluation ’ The query-execution engine takes a query-evaluation plan, executes that plan, and returns the answers to the query.

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

Measures of Query CostMeasures of Query Cost

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 Friday, 01 Nov 2006 Kansas State University

Measures of Query Cost (Cont.)Measures of Query Cost (Cont.)

z For simplicity we just use the number of block transfers from disk and the number of seeks as the cost measures ’ t (^) T – time to transfer one block ’ t (^) S – time for one seek ’ Cost for b block transfers plus S seeks b * t (^) T + S * t (^) S z We ignore CPU costs for simplicity ’ Real systems do take CPU cost into account z We do not include cost to writing output to disk in our cost formulae z Several algorithms can reduce disk IO by using extra buffer space ’ Amount of real memory available to buffer depends on other concurrent queries and OS processes, known only during execution Ö We often use worst case estimates, assuming only the minimum amount of memory needed for the operation is available z Required data may be buffer resident already, avoiding disk I/O ’ But hard to take into account for cost estimation

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

Selection OperationSelection Operation

z File scan – search algorithms that locate and retrieve records that fulfill a selection condition. z Algorithm A1 ( linear search ). Scan each file block and test all records to see whether they satisfy the selection condition. ’ Cost estimate = b (^) r block transfers + 1 seek Ö br denotes number of blocks containing records from relation r ’ If selection is on a key attribute, can stop on finding record Ö cost = ( br /2) block transfers + 1 seek ’ Linear search can be applied regardless of Ö selection condition or Ö ordering of records in the file, or Ö availability of indices

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

Selection Operation (Cont.) Selection Operation (Cont.)

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) ⎤ * ( t (^) T + t (^) S ) Ö 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

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

Algorithms for Complex Selections Algorithms for Complex Selections

z Disjunction: σ (^) θ 1 ∨ (^) θ 2 ∨... (^) θ n ( r). z A11 ( disjunctive selection by union of identifiers). ’ Applicable if all conditions have available indices. Ö Otherwise use linear scan. ’ Use corresponding index for each condition, and take union of all the obtained sets of record pointers. ’ Then fetch records from file z Negation: σ¬θ( r) ’ Use linear scan on file ’ If very few records satisfy ¬θ, and an index is applicable to θ Ö Find satisfying records using index and fetch from file

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

SortingSorting

z We may build an index on the relation, and then use the index to read the relation in sorted order. May lead to one disk block access for each tuple. z For relations that fit in memory, techniques like quicksort can be used. For relations that don’t fit in memory, external sort-merge is a good choice.

Computing & Information Sciences CIS 560: Database System Concepts Friday, 01 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 Friday, 01 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 Friday, 01 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 Friday, 01 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 Friday, 01 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 Friday, 01 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 Friday, 01 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 Friday, 01 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 Friday, 01 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