Docsity

Join Algorithms: Iteration Join, Merge Join, Hash Join, Index Join, Cost Analysis, Study notes of Database Management Systems (DBMS)

Toggle sidebar
bg1
1
1
CS143: Join
Algorithms
2
• Iteration Join (conceptual)
For each r ∈R do
For each s ∈S do
if r.C = s.C then
output r,s pair
3
• Merge Join (conceptual)
(1) if R and S not sorted, sort them
(2) i ←1; j ←1;
While (i ≤|R|) ∧(j ≤|S|) do
if R[i].C = S[j].C then
outputTuples
else if R[i].C > S[j].C then
j ←j+1
else if R[i].C < S[j].C then
i ←i+1
4
Output-Tuples (for duplicates)
While (R[i].C = S[j].C) ∧(i ≤|R|) do
jj ←j;
While (R[i].C=S[j].C)∧(jj≤|S|) do
output (R[i], S[j]);
jj ←jj+1;
i ←i+1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Join Algorithms: Iteration Join, Merge Join, Hash Join, Index Join, Cost Analysis and more Study notes Database Management Systems (DBMS) in PDF only on Docsity!

1

CS143: Join

Algorithms

2

  • Iteration Join (conceptual)

For each r ∈ R do

For each s ∈ S do

if r.C = s.C then

output r,s pair

  • Merge Join (conceptual) (1) if R and S not sorted, sort them (2) i ← 1; j ← 1; While (i ≤ |R|) ∧ (j ≤ |S|) do if R[i].C = S[j].C then outputTuples else if R[i].C > S[j].C then j ← j+ else if R[i].C < S[j].C then i ← i+

Output-Tuples (for duplicates)

While (R[i].C = S[j].C) ∧ (i ≤ |R|) do

jj ← j;

While (R[i].C=S[j].C)∧(jj≤|S|) do

output (R[i], S[j]);

jj ← jj+1;

i ← i+

5

Merge Join

  • Both R, S sorted by C

Memory

…..

…..

R S

R S

6

  • Hash Join (conceptual)
    • Hash function h, range 1 → k
    • Buckets for R: G1, ... Gk
    • Buckets for S: H1, ... Hk

Algorithm (1) Hash R tuples into G1,…,Gk buckets (2) Hash S tuples into H1,…,Hk buckets (3) For i = 1 to k do match tuples in Gi, Hi buckets

Hash Join

  • Step (1)
  • Step (2)

... ...

Memory buckets G G Gk

R

S

Gi ... ... memory Hi

R H H

G G G

  • Index Join (conceptual)

(1) If not, create an index for S.C

(2) For each r ∈ R do

X ← index-lookup(S.C, r.C)

For each s ∈ X do

output (r,s)

13

General Rules for

Nested-Loop Join

  • Use block nested-loop join
  • If no table fits into memory
    • Smaller table on the left
  • In each iteration
    • Read as many blocks in the left (outer) table as possible
    • What if a table fits into memory?

^ B R /( M^ −^ 2) ⋅^ BS +^ BR

14

Merge Join

  • Both R, S ordered by C;

relations contiguous

S

…..

…..

R

S

Memory R

  • Merge Join Algorithm i ← 1; j ← 1; While (i ≤ |R|) ∧ (j ≤ |S|) do if R[i].C = S[j].C then outputTuples else if R[i].C > S[j].C then j ← j+ else if R[i].C < S[j].C then i ← i+
  • How many disk IOs if R and S have been already sorted?

Merge Sort Algorithm

(i) For each 102 blk chunk of R:

  • Read chunk
  • Sort in memory
  • Write to disk

R

S ...

Memory

Sorted chunks

102 blocks

(How many?)

17

Merge Sort Algorithm

(2) Merge

  • Read one block from each 102- blk chunk to memory buffer
  • While (some buffer not empty) a) Output the smallest tuple b) If a buffer is empty - Read the next block from the chunk if not EOF

... ... (^) chunksSorted

Memory

Sorted file

How many disk IOs for sorting? (^18)

Merge Join Cost

  • Very efficient if tables are

sorted: B R + B S

  • Cost for sorting R

2 BR ( log M − 1 ( BR / M ) + 1 )

Further Improvement for

Merge Join?

Hint: do we really need fully

sorted tables?

R

R

Join?

sorted buckets

Hash Join

  • Step (1)
  • Step (2)

... ...

Memory buckets G G Gk

R

S

Gi ... ... memory Hi

R H H

G G G

25

Hash Join Cost

  • No recursive partitioning
    • 3 ( BR + BS )
  • Recursive partitioning

     2 ( _BR BS_ )log( _M_ 1 ) _MBR_ 2 +( _BR_ + _BS_ )  

   (^)     

  • (^) − −

26

Index Join

Memory

….. …..

R (^) S

R

Index Join Algorithm

Read a block B from R

For each r ∈ B do

X ← index-lookup(S.C, r.C)

For each s ∈ X do

output (r,s) Repeat to the end of R

  • How many disk IOs?

Factors to Consider

  • Index look up cost
    • How many blocks for index?
    • How many levels?
  • Number of matching tuples

29

Partial Index Caching

  • How many disk IOs for index lookup on average? ... (^)...

99 nodes (^) 100 nodes

30

Summary of cost: Block nested-loop join 5500 Merge join 1500 Sort+merge joint 7500 Hash join 4500 R1.C index 2090–

Statistics for Tables

  • 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
    • Summary of Join Algorithms

      • Nested-loop join ok for “small” relations (relative to memory size)
      • Hash join usually best forequi- join if relations not sorted and no index
      • Merge join for sorted relations
      • Sort merge join good fornon- equi-join
      • Consider index join if index exists
      • DBMS maintains statistics on data