# 1 40T1 60T2 30T3 10T4 20T5 10T6 60T7 40T8 20T9 R S C C R JOIN S?

## Presentation on theme: "1 40T1 60T2 30T3 10T4 20T5 10T6 60T7 40T8 20T9 R S C C R JOIN S?"— Presentation transcript:

1 40T1 60T2 30T3 10T4 20T5 10T6 60T7 40T8 20T9 R S C C R JOIN S?

2 For each r  R do For each s  S do if r.C = s.C then output r,s pair 40T1 60T2 30T3 10T4 20T5 10T6 60T7 40T8 20T9 R S Nested-Loop Join (NLJ)

3 (1) Create an index for S.C if needed (2) For each r  R do X  index-lookup(S.C, r.C) For each s  X, output (r,s) 40T1 60T2 30T3 10T4 20T5 10T6 60T7 40T8 20T9 R S Index Join (IJ)

4 10T4 20T5 30T3 40T1 60T2 10T6 20T9 40T8 60T7 RS Sort the relations first and join Sort-Merge Join (SMJ)

5 (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 10T4 20T5 30T3 40T1 60T2 10T6 20T9 40T8 60T7 R S Sort-Merge Join (SMJ)

6 Procedure outputTuples While (R[i].C = S[j].C)  (i  | R|) do k  j; While (R[i].C = S[k].C)  (k  |S| ) do output R[i], S[k] pair; k  k + 1; i  i + 1; Sort-Merge Join (SMJ)

7 Hash function h(v), range 1  k Algorithm (1)Hashing stage (bucketizing): hash tuples into buckets Hash R tuples into G1,…,Gk buckets Hash S tuples into H1,…,Hk buckets (2)Join stage: join tuples in matching buckets For i = 1 to k do match tuples in Gi, Hi buckets R … S … … Hash Join (HJ)

8 H(k) = k mod 3 0 1 2 R 0 1 2 S 40T1 60T2 30T3 10T4 20T5 10T6 60T7 40T8 20T9 Hash Join (HJ)

9 Step (1): Hashing stage Step (2): Join stage... Memory buckets G1 G2 Gk R S... Gi memory... Hi R H1 H2 G1 G2 G3 Hash Join (HJ)

Download ppt "1 40T1 60T2 30T3 10T4 20T5 10T6 60T7 40T8 20T9 R S C C R JOIN S?"

Similar presentations