Docsity

Query Processing - Database Systems - Lecture Slides, Slides of Database Management Systems (DBMS)

Partial preview of the text

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

Query Processing

Query Execution

One-Pass Algorithms

Overview of Query Processing

2

SQL query

parse query query expression tree select logical query plan

logical query plan tree select physical query plan physical query plan tree execute physical query plan

data

metadata

QueryOptimization

Query Compilation (Ch 16)

Query Execution (Ch 15)

Overview of Query Execution

  • Operations (steps) of query plan are represented using relational algebra (with bag semantics)
  • Describe efficient algorithms to implement the relational algebra operations
  • Major approaches are scanning, hashing, sorting and indexing
  • Algorithms differ depending on how much main memory is available

Relational Algebra Summary

  • Set operations: union U, intersection ∩, difference –
  • projection, PI, π (choose columns/atts)
  • selection, SIGMA, σ (choose rows/tuples)
  • Cartesian product X
  • natural join (bowtie, ) : pair only those tuples that agree in the designated attributes
  • renaming, RHO, ρ
  • duplicate elimination, DELTA, δ
  • grouping and aggregation, GAMMA, γ
  • sorting, TAU, τ

Scan Primitive

  • Reads entire contents of relation R
  • Needed for doing join, union, etc.
  • To find all tuples of R:
    • Table scan: if addresses of blocks containing R are known and contiguous, easy to retrieve the tuples
    • Index scan: if there is an index on any attribute of R, use it to retrieve the tuples

Costs of Scan Operators

  • Table scan:
    • if R is clustered, then number of disk I/Os is approx. B(R).
    • if R is not clustered, number of disk I/Os could be as large as T(R).
  • Index scan: approx. same as for table scan,

since the number of disk I/Os to examine

entire index is usually much much smaller

than B(R).

Costs of Sort-Scan

  • See earlier slide for costs of table and index

scans in case of clustered and unclustered files

  • Cost of secondary sorting algorithm is:
    • approx. 3B disk I/Os if R is clustered
    • approx. T + 2B disk I/Os if R is not

Categorizing Algorithms

  • By general technique
    • sorting-based
    • hash-based
    • index-based
  • By the number of times data is read from disk
    • one-pass
    • two-pass
    • multi-pass (more than 2)
  • By what the operators work on
    • tuple-at-a-time, unary
    • full-relation, unary
    • full-relation, binary

One-Pass, Unary, Full-Relation

  • duplicate elimination (DELTA)
  • Algorithm:
    • keep a main memory search data structure D (use search tree or hash table) to store one copy of each tuple
    • read in each block of R one at a time (use scan)
    • for each tuple check if it appears in D
    • if not then add it to D and to the output buffer
  • Requires 1 buffer to hold current block of R ; remaining M -1 buffers must be able to hold D
  • I/O cost is just that of the scan

One Pass, Unary, Full-Relation

  • grouping (GAMMA)
  • Algorithm:
    • keep a main memory search structure D with one entry for each group containing - values of grouping attributes - accumulated values for the aggregations
    • scan tuples of R , one block at a time
    • for each tuple, update accumulated values
      • MIN/MAX: keep track of smallest/largest seen so far
      • COUNT: increment by 1
      • SUM: add value to accumulated sum
      • AVG: keep sum and count; at the end, divide
    • write result tuple for each group to output buffer

One Pass, Binary Operations

  • Bag union:
    • copy every tuple of R to the output, then copy every tuple of S to the output
    • only needs M ≥ 1
    • disk I/O cost is B(R) + B(S)
  • For set union, set intersection, set difference, bag intersection, bag difference, product, and natural join: - read smaller relation into main memory - use main memory search structure D to allow tuples to be inserted and found quickly - needs approx. min( B(R),B(S )) buffers - disk I/O cost is B(R ) + B(S)

Set Union ( R U S )

  • Assume S fits in M -1 main memory buffers
  • read S into main memory
  • for each tuple of S
    • insert it into a search structure D (key is entire tuple)
    • copy it to output
  • read each block of R into 1 buffer one at a time
  • for each tuple of R
    • if it is not in D (i.e., not in S ) then copy to output

Set Difference ( R - S )

  • Assume S fits in M -1 main memory buffers
  • read S into main memory
  • for each tuple of S
    • insert into a search structure D (key is entire tuple)
  • read each block of R into 1 buffer one at a time
  • for each tuple of R
    • if it is not in D (i.e., not in S ) then copy to output

Additional Binary Operations

  • See text for one-pass algorithms for
    • S - R
    • bag intersection
    • bag difference
    • product
  • Similar to the previous algorithms
  • Require that one of the operands fit in main

memory