Docsity

Join Algorithms and Efficient Query Processing in Database Systems, Study notes of Database Management Systems (DBMS)

Toggle sidebar
bg1
CS143: Query processing and join algorithms
Book Chapters
Book Chapter 13.1-13.6
Things to Learn
•Join algorithms
Motivation
Student(sid, name, addr, age, GPA)
Enroll(sid, dept, cnum, sec)
B+tree index on sid, age of Student table
•Q: How do we process SELECT * FROM Student WHERE sid > 30?
•Q: How do we process SELECT * FROM Student WHERE sid > 30 AND age > 19?
•Q: How do we process SELECT * FROM Student S, Enroll E WHERE S.sid = E.sid?
•Joins can be very expensive (maybe ≈ |R|×|S|). How can we perform joins efficiently?
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Join Algorithms and Efficient Query Processing in Database Systems and more Study notes Database Management Systems (DBMS) in PDF only on Docsity!

CS143: Query processing and join algorithms

Book Chapters

Book Chapter 13.1-13.

Things to Learn

  • Join algorithms

Motivation

Student(sid, name, addr, age, GPA) Enroll(sid, dept, cnum, sec) B+tree index on sid, age of Student table

  • Q: How do we process SELECT * FROM Student WHERE sid > 30?
  • Q: How do we process SELECT * FROM Student WHERE sid > 30 AND age > 19?
  • Q: How do we process SELECT * FROM Student S, Enroll E WHERE S.sid = E.sid?
  • Joins can be very expensive (maybe ≈ |R| × |S|). How can we perform joins efficiently?

Join algorithms

(R and S example slide)

  • Q: How to join R and S? What is the simplest algorithm? What if we have an index? Any other ideas that we can use? - Four join algorithms ∗ Nested-loop join ∗ Index join ∗ Sort-merge join ∗ Hash join - We now learn how they work
  1. Nested-Loop Join: (nested-loop-join slide)

For each r in R do For each s in S do if r.C = s.C then output r,s pair

  • Q: If R has 100,000 tuples, how many times the entire S table is scanned?
  • The simplest algorithm. It works, but may not be efficient.
  1. Index Join: (index-join slide)

For each r in R do X <- index-lookup(S.C, r.C) For each s in X do output (r,s)

  • Look up index to find matching tuples from S.
  • Q: Benefit of index join compared to nested-loop join?
  1. Sort-Merge Join: (Sort-merge-join slide)
  • Main idea: If tables have been sorted by the join attribute, we need to scan each table only once.
  • Maintain one cursor per table and move the cursor forward.
  • Sort tables and join them.

(sort-merge algorithm slide)

Cost model

  • The ultimate bottom-line:
    • How long does it take for each algorithm to finish for a particular data?
  • Need of cost model
    • We need a “cost model” to estimate the performance of different algorithms
  • Our cost model: Total number of disk blocks that have been read/written
    • Not very realistic ∗ Ignore random, sequential IO issues, CPU cost, etc.
    • Yet simple to analyze and doable in class ∗ More sophisticated models are too complex to analyze in class
    • Good approximation given that disk IOs dominate the cost ∗ Most algorithms that we will study do mostly sequential scan
    • A better algorithm = smaller number of disk block access
    • Ignore the last IOs for result writing (the same for every algorithm)

Example to use

  • Two tables R, S
  • |R| = 1, 000 tuples, |S| = 10, 000 tuples, 10 tuples/block
  • bR = 100 blocks, bS = 1,000 blocks
  • Memory buffer for 22 blocks

Cost Formula (if bR < bS ) Nested Loop Sort Merge Hash Index

Cost of join stage of sort-merge join

  • Usage of main memory blocks for join
    1. Available memory buffers. Disk blocks of each table

22 blocks

.... (^) R 100 blocks

... (^) S 1000 blocks

Memory

...

...

  1. We need to read R table, S table and write the output.
    • Disk transfer unit is one block → At least one memory buffer block to read R, read S and write output. → Three memory blocks used for these tasks.

R

Memory output (^) R

S (^) S

  1. We sequentially read R and S blocks one block at a time, and join them (using the join algrothm)
  • Q: How many disk IOs (block reads/writes) for R and S during join stage?
  • Q: Under our cost metric, can we make it more efficient by allocating more buffers for reading R and S? For example,

.... R

Memory output (^) R

S

10 blocks per table?

S

Nested-Loop Join

(naive nested-loop join algorithm slide for reminder)

(join diagram)

  • Q: What is the cost?
  • Q: What is general cost for bR, bS and M?
  • Q: What if we read S first? Would it be any different?

→ Use smaller table for the outer loop.

  • Summary
    • Always use block nested loop (not the naive algorithm)
    • Read as many blocks as we can for the left table in one iteration
    • Use the smaller table on the left (or outer loop)

Hash Join

(hash join slide for reminder. two stages: hashing stage and join stage)

  • Hashing stage: Read R (or S) table and hash them into different buckets.

Gi .... ... R

buckets

  • Q: One block for reading R, other blocks for bucketizing. How many buckets?
  • Q: Assuming random hashing, how many blocks per bucket?
  • Q: During bucketizing, R table is read once and written once. How many disk IOs (read or write)?
  • Repeat the same for S
  • Join stage: Join H 1 with G 1

H1 from S

5 blocks 48 blocks

G1 from R

  • Q: 5 blocks for G 1 , 48 blocks for H 1. How should we join G 1 and H 1?
  • Q: How many disk IOs?
  • Q: Total disk IOs?
  • Q: What if R is large and G 1 > 20?

Recursive partitioning

  • Example 2
    • 40 blocks for index ∗ 1 root, 39 leaf
    • On average, 10 matching tuples in S.

Q: How many disk IOs? How should we use memory?

  • General cost: bR + |R| · (C + J)
    • C average index look up cost
    • J matching tuples in S for every R tuple
    • |R| tuples in R
  • Q: How can we compute J?
    • Example: R ./R.C=S.C S. |S| = 10, V (C, R) = 1, 000. Uniform distribution for C values. How many tuples in S with C = c?

Sort-Merge Join

  • Two stage algorithm:
    1. Sort stage: Sort R and S
    2. Merge stage: Merge sorted R and S
  • of disk IOs during merge stage: bR + bS = 100 + 1, 000 = 1, 100.

  • Q: How many disk IOs during sort stage?

Merge sort algorithm

R

100 blocks

  • Q: How many blocks can we sort in main memory?
    • Q: Do we need to allocate one block for output?
  • Q: How many sorted runs after sorting R in chunk of 22 blocks?

R

100 blocks

22 blocks

22 blocks

? ....

sorted runs

  • Q: What should we do with 5 sorted-runs?
  • Q: How many disk IOs?
    • Q: During first-stage sorting?
    • Q: During second-stage merging?

Repeat it for S table of 1,000 blocks. Show that now we need three stages.

  • In general, the number of passes for bR and M : (dlogM − 1 (bR/M )e + 1)
    • Verify it at home on your own.
    • Total # of IOs for sorting: 2 · bR(dlogM − 1 (bR/M )e + 1)
  • In general, for n way joins, (2((nn−−1)!1))! ways.
    • Study why this is the case at home.
    • For n = 3, 4!/2! = 12
    • For n = 5, 8!/4! = 1680
    • For n = 10, 18!/9! = 17 × 109
  • DBMS tries to pick the best based on statistics
    • In reality, picking the best is too difficult ∗ For n = 10, it is clearly impossible to examine all 17 billion plans
    • DBMS tries to avoid “obvious mistakes” using a number of heuristics to examine only the ones that are likely to be reasonable
  • Read the PDF file on databse tuning and optimization
    • For 90% of the time, DBMS picks a good plan
    • To optimize the remaning 10%, companies pay big money to datbase consultants

Statistics collection commands on DBMS

  • DBMS has to collect statistics on tables/indexes for optimal performance
    • Without stats, DBMS does stupid things
  • DB
    • RUNSTATS ON TABLE . AND INDEXES ALL
  • Oracle
    • ANALYZE TABLE COMPUTE STATISTICS
    • ANALYZE TABLE ESTIMATE STATISTICS (cheaper than COMPUTE)
  • Run the command after major update/index construction
  • Does not matter for MySQL. No optimization based on actual data. Only rule-based opti- mizer.