Docsity

Two-Pass Algorithms - Database Systems - Lecture Slides, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download Two-Pass Algorithms - Database Systems - Lecture Slides and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Query Processing

Two-Pass Algorithms

Nested-Loop Joins

Another helpful primitive.

Main idea to join relations R and S :

for each tuple s in S do for each tuple r in R do if r and s match then ouput the join of r and s

How to optimize this?

Analysis of Nested-Loop Join

  • Assume S is the smaller relation
  • number of disk I/O's:
    • number of disk I/O's per iteration of outer loop times number of iterations of outer loop
    • this is: (M - 1 + B(R)) * B(S)/(M-1)
  • If B(S) ≤ M-1 , then this is same as one-pass join

algorithm.

Two-Pass Algorithms

  • Used when relations are too large to fit in memory
  • Pieces of the relation are read into memory, processed in some way, and then written back to disk
  • The pieces are then reread from disk to complete the operation
  • Why only two passes? Usually this is enough, but ideas can be extended to more passes.

Duplicate Elimination Using Sorting

  • Create the sorted sublists
  • Read first block of each sublist into memory
  • Look at first unconsidered tuple in each block

and let t be first one in sorted order

  • Copy t to the output and delete all other copies

of t in the blocks in memory

  • If a block is emptied, then bring into its buffer

the next block of that sublist and delete any

copies of t in it

Analysis of Duplicate Elimination

  • B(R) disk I/Os to create sorted sublists
  • B(R) disk I/Os to write sorted sublists to disk
  • B(R) disk I/Os to reread each block from the

sublists

  • Grand total is 3*B(R ) disk I/Os.
  • Remember we need blocks of main

memory.

8

B ( R )

Union Using Sorting

  • Create the sorted sublists for R on disk, using the entire tuple as the sort key
  • Create the sorted sublists for S on disk, using the entire tuple as the sort key
  • Use one main memory buffer for each sublist of R and S
  • Repeatedly find next tuple t among all buffers, copy to output, and remove from the buffers all copies of t (reloading any buffer that is emptied)
  • 3(B(R ) + B(S)) disk I/Os; B(R) + B(S) ≤ M^2

Intersection and Difference Using

Sorting

  • Very similar to Union.
  • Different rules for deciding whether/ how many times

a tuple is output

  • Set Intersection : output t if it appears in both R and S
  • Bag Intersection : output t the minimum number of

times it appears in R and S

  • Difference : see text.
  • 3*(B(R ) + B(S)) disk I/Os; B(R ) + B(S) ≤ M^2

Simple Sort-Based Join (cont'd)

  • Let y be the smaller sort key at the front of the

buffers for R and S

  • If y appears in both relations then
    • load into memory all tuples from R and S with sort key y ; up to M buffers are available for this step (*)
    • output all tuples formed by combining tuples from R and tuples from S with sort key y

More on Sort-Based Join

  • (*) Suppose not all tuples with sort key y fit in

main memory.

  • If all the tuples in one relation with sort key y

do fit, then do one-pass join

  • If all the tuples in neither relation with sort

key y fit, then do basic nested-loop join

Two-Pass Algorithms Using Hashing

  • General idea:
    • Hash the tuples using an appropriate hash key
    • For the common operations, there is a way to choose the hash key so that all tuples that need to be considered together has the same hash value
    • Do the operation working on one bucket at a time

Partitioning by Hashing

initialize M-1 buckets with M-1 empty buffers

for each block b of relation R do read block b into the Mth buffer for each tuple t in b do if the buffer for bucket h(t) is full then copy the buffer to disk initialize a new empty block in that buffer copy t to the buffer for bucket h(t)

for each bucket do if the buffer for this bucket is not empty then write the buffer to disk

Analysis of Duplicate Elimination

Using Hashing

  • Number of disk I/O's: 3*B(R)
  • In order for this to work, we need:
    • hash function h evenly distributes the tuples among the buckets
    • each bucket R (^) i fits in main memory (to allow the one-pass algorithm)
    • i.e., B(R) ≤ M^2

Grouping Using Hashing

  • Hash all the tuples of relation R to M-1 buckets, using a hash function that depends only on the grouping attributes
  • Note: all tuples in the same group end up in the same bucket!
  • Use the one-pass algorithm to process each bucket independently
  • Uses 3*B(R) disk I/O's, requires B(R) ≤ M^2