31
J1. NESTED LOOP JOIN S on S.S#=E.S# For each record in S, (the outer loop over records from the outer or driver relation), retrieve every record from E, (the inner loops over the inner relation), test join condition, if it's true, concatenate the tuples (project off unwanted columns) and output, else go to next E (inner-relation) record. SELECT SNAME,C#,GRADE FROM S=STUDENT,E=ENROLL WHERE S.S#=E.S#; CODE GENERATION implements the operator, JOIN (equi-join, we will use for it.) S#|C#|GR 32|8 |89 32|7 |91 25|7 |68 25|6 |76 32|6 |62 E S#|SNAME |LCODE 25|CLAY |NJ5101 32|THAISZ|NJ5102 38|GOOD |FL6321 17|BAID |NY2091 57|BROWN |NY2092 S S.S#=E.S# FALSE S.S#=E.S# FALSE S . S#=E.S# TRUE SNAME |C# |GRADE Result CLAY | 7 | 68 S.S#=E .S# TRUE CLAY | 6 | 76 S.S#=E.S# FALSE Section 9 # 19 09_Queries_LECTURE2

J1. NESTED LOOP JOIN S on S.S#=E.S#

  • Upload
    suchin

  • View
    60

  • Download
    0

Embed Size (px)

DESCRIPTION

S. E. S#|SNAME |LCODE 25|CLAY |NJ5101 32|THAISZ|NJ5102 38|GOOD |FL6321 17|BAID |NY2091 57|BROWN |NY2092. SNAME |C# |GRADE. S#|C#|GR 32|8 |89 32|7 |91 25|7 |68 25|6 |76 32|6 |62. Result. S.S#=E.S# TRUE. S.S#=E.S# FALSE. S.S#=E.S# FALSE. S.S#=E.S# FALSE. S.S#=E.S# - PowerPoint PPT Presentation

Citation preview

Page 1: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

J1. NESTED LOOP

JOIN S on S.S#=E.S#

For each record in S, (the outer loop over records from the outer or driver relation),

retrieve every record from E, (the inner loops over the inner relation),

test join condition,

if it's true, concatenate the tuples (project off unwanted columns) and output,

else go to next E (inner-relation) record.SELECT SNAME,C#,GRADE FROM S=STUDENT,E=ENROLL WHERE S.S#=E.S#;

CODE GENERATION implements the operator, JOIN (equi-join, we will use for it.)

S#|C#|GR

32|8 |89

32|7 |91

25|7 |68

25|6 |76

32|6 |62

ES#|SNAME |LCODE

25|CLAY |NJ5101

32|THAISZ|NJ5102

38|GOOD |FL6321

17|BAID |NY2091

57|BROWN |NY2092

S

S.S#=E.S#

FALSES.S#=E.S#FALSE

S.S#=E.S#TRUE

SNAME |C# |GRADE Result

CLAY | 7 | 68

S.S#=E.S#TRUE

CLAY | 6 | 76

S.S#=E.S#FALSE

Section 9 # 19

09_Queries_LECTURE2

Page 2: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

J1. NESTED LOOP

Second inner loop pass:

SELECT SNAME,C#,GRADE FROM S=STUDENT,E=ENROLL WHERE S.S#=E.S#;

CODE GENERATION implements the operator, JOIN (equi-join, we will use for it.)

S#|C#|GR

32|8 |89

32|7 |91

25|7 |68

25|6 |76

32|6 |62

ES#|SNAME |LCODE

25|CLAY |NJ5101

32|THAISZ|NJ5102

38|GOOD |FL6321

17|BAID |NY2091

57|BROWN |NY2092

S

S.S#=E.S

TRUES.S#=E.S#FALSE

S.S#=E.S#FALSE

SNAME |C# |GRADE res

CLAY | 7 | 68

S.S#=E.S#TRUE

CLAY | 6 | 76

THAISZ| 7 | 91 THAISZ| 6 | 62

S.S#=E.S

TRUETHAISZ| 8 | 89

Section 9 # 20

Page 3: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

J1. NESTED LOOP

Third inner loop pass:

SELECT SNAME,C#,GRADE FROM S=STUDENT,E=ENROLL WHERE S.S#=E.S#;

CODE GENERATION implements the operator, JOIN (equi-join, we will use for it.)

S#|C#|GR

32|8 |89

32|7 |91

25|7 |68

25|6 |76

32|6 |62

ES#|SNAME |LCODE

25|CLAY |NJ5101

32|THAISZ|NJ5102

38|GOOD |FL6321

17|BAID |NY2091

57|BROWN |NY2092

S

S.S#=E.S

FALSES.S#=E.S#FALSE

S.S#=E.S#FALSE

SNAME |C# |GRADE res

CLAY | 7 | 68 CLAY | 6 | 76

THAISZ| 7 | 91 THAISZ| 6 | 62

THAISZ| 8 | 89

S.S#=E.S

FALSE

S.S#=E.S

FALSE

Section 9 # 21

Page 4: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

J1. NESTED LOOP

4th inner loop pass:

SELECT SNAME,C#,GRADE FROM S=STUDENT,E=ENROLL WHERE S.S#=E.S#;

CODE GENERATION implements the operator, JOIN (equi-join, we will use for it.)

S#|C#|GR

32|8 |89

32|7 |91

25|7 |68

25|6 |76

32|6 |62

ES#|SNAME |LCODE

25|CLAY |NJ5101

32|THAISZ|NJ5102

38|GOOD |FL6321

17|BAID |NY2091

57|BROWN |NY2092

SSNAME |C# |GRADE res

CLAY | 7 | 68 CLAY | 6 | 76

THAISZ| 7 | 91 THAISZ| 6 | 62

THAISZ| 8 | 89

S.S#=E.S

FALSES.S#=E.S#FALSE

S.S#=E.S

FALSE

S.S#=E.S

FALSES.S#=E.S

FALSE

Section 9 # 9

Page 5: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

J1. NESTED LOOP

R 5th and last inner loop pass:

SELECT SNAME,C#,GRADE FROM S=STUDENT,E=ENROLL WHERE S.S#=E.S#;

CODE GENERATION implements the operator, JOIN (equi-join, we will use for it.)

S#|C#|GR

32|8 |89

32|7 |91

25|7 |68

25|6 |76

32|6 |62

ES#|SNAME |LCODE

25|CLAY |NJ5101

32|THAISZ|NJ5102

38|GOOD |FL6321

17|BAID |NY2091

57|BROWN |NY2092

SSNAME |C# |GRADE res

CLAY | 7 | 68 CLAY | 6 | 76

THAISZ| 7 | 91 THAISZ| 6 | 62

THAISZ| 8 | 89

S.S#=E.S

FALSE

S.S#=E.S#

FALSE

S.S#=E.S

FALSE

S.S#=E.S

FALSE

S.S#=E.S

FALSE

Section 9 # 23

Page 6: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

J2. When there is an Index on one join attribute the join can be done in one pass (called Indexed Nested Loop.

If there is an index on E.S#, get r in S, get matching E-tuples using the index (need not scan entire inner relation, E, each time as was necessary with J1) .

R=SELECT SNAME,C#,GRADE FROM S=STUDENT,E=ENROLL WHERE S.S#=E.S#;

CODE GENERATION implements the operator, JOIN (equi-join, we will use for it.)

S#|SNAME |LCODE 25|CLAY |NJ510132|THAISZ|NJ510238|GOOD |FL632117|BAID |NY209157|BROWN |NY2092

RRN|S#|C#|GR 0 |32|8 |89 1 |32|7 |91 2 |25|7 |68 3 |25|6 |76 4 |32|6 |62

ES

RRN |S# 2,3 |250,1,4|32

Dense Index on E.S#

SNAME |C# |GRADE RCLAY | 7 | 68 CLAY | 6 | 76

THAISZ| 7 | 91 THAISZ| 6 | 62

THAISZ| 8 | 89

Section 9 # 24

Page 7: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

J3. MERGE JOIN: If both S.S# and E.S# are clustered, then scan both S and E once in order, keeping in mind that S.S# is the primary key (uniqueness property), but E.S# is not.

R=SELECT SNAME,C#,GRADE FROM S=STUDENT,E=ENROLL WHERE S.S#=E.S#;

CODE GENERATION implements the operator, JOIN (equi-join, we will use for it.)

S#|SNAME |LCODE 17|BAID |NY209125|CLAY |NJ510132|THAISZ|NJ510238|GOOD |FL632157|BROWN |NY2092

S#|C#|GR 25|7 |68 25|6 |76 32|6 |6232|8 |89 32|7 |91

ESS.S#=E.S#

FALSE

SNAME |C# |GRADE R

S.S#=E.S#

TRUE

CLAY | 7 | 68 S.S#=E.S#

TRUE

CLAY | 6 | 76 S.S#=E.S#FALSE

S.S#=E.S#

TRUE

THAISZ| 6 | 62 S.S#=E.S#

TRUE

THAISZ| 8 | 89 S.S#=E.S#TRUETHAISZ| 7 | 91 S.S#=E.S#

FALSES.S#=E.S#

FALSE

J3'. SORT-MERGE JOIN: If R.A and S.B are not ordered, sort them first (into R' clustered on A and S' clustered on B), then apply MERGE (J2 above).

Section 9 # 25

Page 8: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

J4. HASH-JOIN: RIDs hashed to buckets (pages). Corresponding buckets retrieved and scanned

GRACE JOIN: (first example of a hash-join technique): Allocate M pages of memory to the join process. Partition the M page frames as follows: One page frame for new pages as they are read from disk (called that page frame, IN), then the remaining B+1 become hash buckets R0,..,RB ( Note: B = M-2 ). Use hash function, h(S#)=MODB(S#)

Partial Sort (Build) Phase:

Partially-Sort-S: Read each S-page to IN, hash each record using h(S#) to R0,...,RB. If the bucket, say R i, is full (collision), flush it to temporary disk file named Si.

Partially-Sort-E: Read each E-page into IN, hash each record with h(S#). Upon collision in a bucket, say R j, flush its' contents to a temporary disk file, named Ej.

Probe Phase: With each pair of temporary files, S0 & E0, S1 & E2, S2 & E2, ... in turn, do as follows: Re-partition memory with two page frames called IN and OUT. With the rest, form one large hash area. For S i, construct an internal a hash table in the hash area using another hash function, k(S#) FOR E i, PROBE the hash table using k(S#) for matches, output the join of all matches to OUT.

CODE GENERATION implements the operator, JOIN

Section 9 # 26

Page 9: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

2,0|25|6 |76|NY2091

J4. HASH-JOIN: RIDs hashed to buckets (pages). Matching buckets are retrieved and scanned.

R=SELECT SNAME,C#,GRADE FROM S=STUDENT,E=ENROLL WHERE S.S#=E.S#;

GRACE JOIN: (first example of a hash-join technique): Allocate M=4 pages of memory to the join process. Partition the M pages as follows: - One page for IN (putting new pages as they are read from disk), M-1=3 pages for buckets R 0,..,R2.

Build Phase.

Use hash function, h(S#)=MOD3(S#)

CODE GENERATION implements the operator, JOIN

SRID|S#|SNAME | LCODE1,0|17|BAID |NY2091

3,0|38|GOOD |FL6321

1,1|25|CLAY |NJ5101

2,0|32|THAISZ|NJ5102

2,1|57|BROWN |NY2092

ERID|S#|C#|GR| LCODE 1,0|17|5 |96|NJ5101

1,1|25|7 |68|ND4456

2,1|32|8 |89|NY2091

3,0|32|7 |91|FL6320

3,1|34|6 |62|ND4456

R0

R1

R2

IN

Partial-Sort-S:

Read each S-page to IN, hash

each record using h(S#) to R0,R1,R2.

Upon collision in any of the buckets,

flush to temporary disk file,

called S0,S1,S2.

S0

S1

S2

1,0|17|BAID |NY2091

1,1|25|CLAY |NJ5101

2,0|32|THAISZ|NJ5102

2,1|57|BROWN |NY2092

3,0|38|GOOD |FL6321

Collision! Dump R2 Then flush all.

17|BAID |NY2091

25|CLAY |NJ5101

32|THAISZ|NJ5102

57|BROWN |NY2092

38|GOOD |FL6321

Do the same with E.E0

E1

E2

25|6 |76|NY2091

17|5 |96|NJ5101

25|7 |68|ND4456

32|8 |89|NY2091

32|7 |91|FL6320

34|6 |62|ND4456

Section 9 # 27

Page 10: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

CODE GENERATION implements the operator, GRACE JOIN

S0

S1

S2

Hash Phase: With each pair of temporary files, S0 & E0, S1 & E2, S2 & E2,... in turn, do as follows:

Re-partition memory into IN, OUT and one large hash area.

Probe Phase: For Si, BUILD internal a hash table in the hash area using another hash function, k(S#)=MOD4

(open addr for collisions)

FOR Ei, PROBE hash table using k for matches,

output join of matches to OUT.

Start with S0 and E0. But E0 empty (no output will be produced) so skip.

PROBE S1 and E1: 1. Read S1 page-1 to IN. 2. Hash IN to HASH.

17|BAID |NY2091

25|CLAY |NJ5101

32|THAISZ|NJ5102

57|BROWN |NY2092

38|GOOD |FL6321

E0

E1

E2

25|6 |76|NY2091

17|5 |96|NJ5101

25|7 |68|ND4456

32|8 |89|NY2091

32|7 |91|FL6320

34|6 |62|ND4456

25|CLAY |NJ5101

25|CLAY |NJ5101

0

OUT

IN

123

Hash

Section 9 # 28

Page 11: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

25|7 |68|ND4456

CODE GENERATION implements GRACE JOIN

S0

S1

S2

PROBE S1, E1: 1. Read S1 page-1 to IN. 2. Hash IN to HASH. 3. Read E1 to IN 4. Concatenate to OUT.

17|BAID |NY2091

25|CLAY |NJ5101

32|THAISZ|NJ5102

57|BROWN |NY2092

38|GOOD |FL6321

E0

E1

E2

25|6 |76|NY2091

17|5 |96|NJ5101

25|7 |68|ND4456

32|8 |89|NY2091

32|7 |91|FL6320

34|6 |62|ND4456

25|CLAY |NJ5101

CLAY|7 |68

25|6 |76|NY2091

CLAY|6 |76

0

OUT

IN

123

Hash

Section 9 # 29

Page 12: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

CODE GENERATION implements GRACE JOIN

S0

S1

S2

PROBE S1, E1: 1. Read S1 pg1 to IN. 2. Hash IN to HASH. 3. Read E1 to IN 4. Concatenate to OUT.5. Read (next page of) E1 to IN. 6. Hash to HASH. 7. If match, Concatenate to OUT (Since OUT is full, flush OUT first.). But no match!8. Flush HASH and IN when done with S1, E1 Probe (before starting Probe S2, E2).

17|BAID |NY2091

25|CLAY |NJ5101

32|THAISZ|NJ5102

57|BROWN |NY2092

38|GOOD |FL6321

E0

E1

E2

25|6 |76|NY2091

17|5 |96|NJ5101

25|7 |68|ND4456

32|8 |89|NY2091

32|7 |91|FL6320

34|6 |62|ND4456

25|CLAY |NJ5101

CLAY|7 |68

CLAY|6 |76

0

OUT

IN

123

Hash

34|6 |62|ND4456

Section 9 # 30

Page 13: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

CODE GENERATION implements GRACE JOIN

S0

S1

S2

PROBE S1, E1: 1. Read S1 pg1 to IN. 2. Hash IN to HASH. 3. Read E1 to IN 4. Concatenate to OUT.5. Read (next page of) E1 to IN. 6. Hash to HASH. 7. If match, Concatenate to OUT... 8. Flush HASH and IN when done Probing E1.

17|BAID |NY2091

25|CLAY |NJ5101

32|THAISZ|NJ5102

57|BROWN |NY2092

38|GOOD |FL6321

E0

E1

E2

25|6 |76|NY2091

17|5 |96|NJ5101

25|7 |68|ND4456

32|8 |89|NY2091

32|7 |91|FL6320

34|6 |62|ND4456

CLAY|7 |68

CLAY|6 |76

0

OUT

IN

123

Hash

PROBE S2, E2: 1. Read S2 pg1 to IN 2. MOD3 hash IN to HASH (open addressing for collisions).

17|BAID |NY2091

32|THAISZ|NJ5102

Section 9 # 31

Page 14: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

CODE GENERATION implements GRACE JOIN

S0

S1

S2

PROBE S1, E1: 1. Read S1 pg1 to IN. 2. Hash IN to HASH. 3. Read E1 to IN 4. Concatenate to OUT.5. Read (next page of) E1 to IN. 6. Hash to HASH. 7. If match, Concatenate to OUT... 8. Flush HASH and IN when done Probing E1.

17|BAID |NY2091

25|CLAY |NJ5101

32|THAISZ|NJ5102

57|BROWN |NY2092

38|GOOD |FL6321

E0

E1

E2

25|6 |76|NY2091

17|5 |96|NJ5101

25|7 |68|ND4456

32|8 |89|NY2091

32|7 |91|FL6320

34|6 |62|ND4456

CLAY|7 |68

CLAY|6 |76

0

OUT

IN

123

Hash

PROBE S2, E2: 1. Read S2 pg1 to IN 2. MOD3 hash IN to HASH (open addressing for collisions).3. Read S2 pg2 to IN 4. MOD3 hash IN to HASH (open addressing for collisions).

17|BAID |NY2091

32|THAISZ|NJ5102

38|GOOD |FL6321

Section 9 # 32

Page 15: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

CODE GENERATION implements GRACE JOIN

S0

S1

S2

PROBE S1, E1: 1. Read S1 pg1 to IN. 2. Hash IN to HASH. 3. Read E1 to IN 4. Concatenate to OUT.5. Read (next page of) E1 to IN. 6. Hash to HASH. 7. If match, Concatenate to OUT... 8. Flush HASH and IN when done Probing E1.

17|BAID |NY2091

25|CLAY |NJ5101

32|THAISZ|NJ5102

57|BROWN |NY2092

38|GOOD |FL6321

E0

E1

E2

25|6 |76|NY2091

17|5 |96|NJ5101

25|7 |68|ND4456

32|8 |89|NY2091

32|7 |91|FL6320

34|6 |62|ND4456

CLAY|7 |68

CLAY|6 |76

0

OUT

IN

123

Hash

PROBE S2, E2: 1. Read S2 pg1 to IN 2. MOD3 hash IN to HASH (open addressing for collisions).3. Read S2 pg2 to IN 4. MOD3 hash IN to HASH (open addressing for collisions).5.Read E2 to IN. 6.Hash to HASH. 7. If match, Concatenate to OUT...

17|BAID |NY2091

32|THAISZ|NJ5102

38|GOOD |FL6321

17|5 |96|NJ5101 32|8 |89|NY2091

BAID |5|96

Section 9 # 33

Page 16: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

CODE GENERATION implements GRACE JOIN

S0

S1

S2

PROBE S1, E1: 1. Read S1 pg1 to IN. 2. Hash IN to HASH. 3. Read E1 to IN 4. Concatenate to OUT.5. Read (next page of) E1 to IN. 6. Hash to HASH. 7. If match, Concatenate to OUT... 8. Flush HASH and IN when done Probing E1.

17|BAID |NY2091

25|CLAY |NJ5101

32|THAISZ|NJ5102

57|BROWN |NY2092

38|GOOD |FL6321

E0

E1

E2

25|6 |76|NY2091

17|5 |96|NJ5101

25|7 |68|ND4456

32|8 |89|NY2091

32|7 |91|FL6320

34|6 |62|ND4456

CLAY|7 |68

CLAY|6 |76

0

OUT

IN

123

Hash

PROBE S2, E2: 1. Read S2 pg1 to IN 2. MOD3 hash IN to HASH (open addressing for collisions).3. Read S2 pg2 to IN 4. MOD3 hash IN to HASH (open addressing for collisions).5.Read E2 to IN. 6.Hash to HASH. 7. If match, Concatenate to OUT...

17|BAID |NY2091

32|THAISZ|NJ5102

38|GOOD |FL6321

32|8 |89|NY2091

BAID |5|96 THAISZ|8|89

Section 9 # 34

Page 17: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

CODE GENERATION implements GRACE JOIN

S0

S1

S2

PROBE S1, E1: 1. Read S1 pg1 to IN. 2. Hash IN to HASH. 3. Read E1 to IN 4. Concatenate to OUT.5. Read (next page of) E1 to IN. 6. Hash to HASH. 7. If match, Concatenate to OUT... 8. Flush HASH and IN when done Probing E1.

17|BAID |NY2091

25|CLAY |NJ5101

32|THAISZ|NJ5102

57|BROWN |NY2092

38|GOOD |FL6321

E0

E1

E2

25|6 |76|NY2091

17|5 |96|NJ5101

25|7 |68|ND4456

32|8 |89|NY2091

32|7 |91|FL6320

34|6 |62|ND4456

CLAY |7|68

CLAY |6|76

0

OUT

IN

123

Hash

PROBE S2, E2: 1. Read S2 pg1 to IN 2. MOD3 hash IN to HASH (open addressing for collisions).3. Read S2 pg2 to IN 4. MOD3 hash IN to HASH (open addressing for collisions).5.Read E2 to IN. 6.Hash to HASH. 7. If match, Concatenate to OUT...(repeat until E2 empty)8. Flush HASH and IN when done Probing E1.

17|BAID |NY2091

32|THAISZ|NJ5102

38|GOOD |FL6321

32|7 |91|FL6320

BAID |5|96 THAISZ|8|89

THAISZ|7|91

Section 9 # 35

Page 18: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

J4. HASH-JOIN: (a better way than GRACE JOIN):

HYBRID HASH JOIN of SS#E (developed by former chair of NDSU CS Dept., Dr. L. Shapiro):

Partition the M pages of main memory allocated to the join process as follows: One page for the IN, 0ne page for the OUT, B pages for hash buckets, R1..RB, Leave the rest for a large internal hash table, R0.

PARTIAL SORT S: Read each S page to IN, hash each record using h(S#)=MODB(S#) to R0..RB. If a record hashes to R0, apply an internal hash function, k(S#) which hashes the record to a slot in R0. Use open addressing for k-collisions. When an h-collision occurs in any page R i i=1..B, flush that page to a disk file, called Si i=1..B.

PARTIALLY SORT E: Read each E page into IN, hash each record with h(S#) to R0..RB If record hashes to R0, apply internal hash function k(S#), and concatenate records that match to OUT. If a collision occurs in any page, Ri i=1..B, flush to temporary disk file, Ei i=1..B.

PROBE PHASE for pairs, Si and Ei i=1..B, do the same as in Grace Join.

Hybrid Hash Join can be done with Bit Filtering to eliminate non-participating tuples early and avoid wasted processing of non-participating tuples Much more detail and example walkthroughs can be found in the HTML version of these notes (also available from "Other Materials" http://www.cs.ndsu.nodak.edu/~perrizo/classes/765/09query.html

CODE GENERATION implements the operator, HYBRID HASH JOIN

Section 9 # 36

Page 19: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

2,0|25|6 |76|NY2091

h=MOD3 k=MOD4 SELECT SNAME,C#,GRADE FROM S,E WHERE S.S#=E.S#;CODE GENERATION implements the operator, HYBRID HASH JOIN

SRID|S#|SNAME | LCODE1,0|17|BAID |NY2091

3,0|38|GOOD |FL6321

1,1|25|CLAY |NJ5101

2,0|32|THAISZ|NJ5102

2,1|57|BROWN |NY2092

ERID|S#|C#|GR| LCODE 1,0|17|5 |96|NJ5101

1,1|25|7 |68|ND4456

2,1|32|8 |89|NY2091

3,0|32|7 |91|FL6320

3,1|34|6 |62|ND4456

R1

R2

IN

Read each S-page to IN, hash each record using h(S#)=MOD3(S#) to R0,R1,R2. If h(S#)=0, k(S#) determines R0 slot (open addressing for collisions in R0). h collisions in R1, R2 flush to file, S1, S2 resp.

Similarly (note all h hashes goes to R1 or R2, So all E records flush to E1 and E2 and R0 is flushed too.

S0

S1

S2

1,0|17|BAID |NY2091

1,1|25|CLAY |NJ5101

2,0|32|THAISZ|NJ5102

2,1|57|BROWN |NY2092

3,0|38|GOOD |FL6321

17|BAID |NY2091

25|CLAY |NJ5101

32|THAISZ|NJ5102

57|BROWN |NY2092

38|GOOD |FL6321

E0

E1

E2

25|6 |76|NY2091

17|5 |96|NJ5101

25|7 |68|ND4456

32|8 |89|NY2091

32|7 |91|FL6320

34|6 |62|ND4456

R0

0123

OUT

Section 9 # 37

Page 20: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

25|7 |68|ND4456

CODE GENERATION implements HYBRID JOIN

S0

S1

S2

PROBE S1, E1: 1. Read S1 page-1 to IN. 2. Hash IN to HASH. 3. Read E1 to IN 4. Concatenate to OUT.

17|BAID |NY2091

25|CLAY |NJ5101

32|THAISZ|NJ5102

38|GOOD |FL6321

E0

E1

E2

25|6 |76|NY2091

17|5 |96|NJ5101

25|7 |68|ND4456

32|8 |89|NY2091

32|7 |91|FL6320

34|6 |62|ND4456

25|CLAY |NJ5101

CLAY|7 |68

25|6 |76|NY2091

CLAY|6 |76

0

OUT

IN

123

Hash

Section 9 # 38

Page 21: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

CODE GENERATION implements HYBRID JOIN

S0

S1

S2

PROBE S1, E1: 1. Read S1 pg1 to IN. 2. Hash IN to HASH. 3. Read E1 to IN 4. Concatenate to OUT.5. Read (next page of) E1 to IN. 6. Hash to HASH. 7. If match, Concatenate to OUT (Since OUT is full, flush OUT first.). But no match!8. Flush HASH and IN when done with S1, E1 Probe (before starting Probe S2, E2).

17|BAID |NY2091

25|CLAY |NJ5101

32|THAISZ|NJ5102

38|GOOD |FL6321

E0

E1

E2

25|6 |76|NY2091

17|5 |96|NJ5101

25|7 |68|ND4456

32|8 |89|NY2091

32|7 |91|FL6320

34|6 |62|ND4456

25|CLAY |NJ5101

CLAY|7 |68

CLAY|6 |76

0

OUT

IN

123

Hash

34|6 |62|ND4456

Section 9 # 39

Page 22: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

CODE GENERATION implements HYBRID JOIN

S0

S1

S2

PROBE S1, E1: 1. Read S1 pg1 to IN. 2. Hash IN to HASH. 3. Read E1 to IN 4. Concatenate to OUT.5. Read (next page of) E1 to IN. 6. Hash to HASH. 7. If match, Concatenate to OUT... 8. Flush HASH and IN when done Probing E1.

17|BAID |NY2091

25|CLAY |NJ5101

32|THAISZ|NJ5102

38|GOOD |FL6321

E0

E1

E2

25|6 |76|NY2091

17|5 |96|NJ5101

25|7 |68|ND4456

32|8 |89|NY2091

32|7 |91|FL6320

34|6 |62|ND4456

CLAY|7 |68

CLAY|6 |76

0

OUT

IN

123

Hash

PROBE S2, E2: 1. Read S2 pg1 to IN 2. MOD3 hash IN to HASH (open addressing for collisions).

17|BAID |NY2091

32|THAISZ|NJ5102

Section 9 # 40

Page 23: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

CODE GENERATION implements HYBRID JOIN

S0

S1

S2

PROBE S1, E1: 1. Read S1 pg1 to IN. 2. Hash IN to HASH. 3. Read E1 to IN 4. Concatenate to OUT.5. Read (next page of) E1 to IN. 6. Hash to HASH. 7. If match, Concatenate to OUT... 8. Flush HASH and IN when done Probing E1.

17|BAID |NY2091

25|CLAY |NJ5101

32|THAISZ|NJ5102

38|GOOD |FL6321

E0

E1

E2

25|6 |76|NY2091

17|5 |96|NJ5101

25|7 |68|ND4456

32|8 |89|NY2091

32|7 |91|FL6320

34|6 |62|ND4456

CLAY|7 |68

CLAY|6 |76

0

OUT

IN

123

Hash

PROBE S2, E2: 1. Read S2 pg1 to IN 2. MOD3 hash IN to HASH (open addressing for collisions).3. Read S2 pg2 to IN 4. MOD3 hash IN to HASH (open addressing for collisions).

17|BAID |NY2091

32|THAISZ|NJ5102

38|GOOD |FL6321

Section 9 # 41

Page 24: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

CODE GENERATION implements HYBRID JOIN

S0

S1

S2

PROBE S1, E1: 1. Read S1 pg1 to IN. 2. Hash IN to HASH. 3. Read E1 to IN 4. Concatenate to OUT.5. Read (next page of) E1 to IN. 6. Hash to HASH. 7. If match, Concatenate to OUT... 8. Flush HASH and IN when done Probing E1.

17|BAID |NY2091

25|CLAY |NJ5101

32|THAISZ|NJ5102

38|GOOD |FL6321

E0

E1

E2

25|6 |76|NY2091

17|5 |96|NJ5101

25|7 |68|ND4456

32|8 |89|NY2091

32|7 |91|FL6320

34|6 |62|ND4456

CLAY|7 |68

CLAY|6 |76

0

OUT

IN

123

Hash

PROBE S2, E2: 1. Read S2 pg1 to IN 2. MOD3 hash IN to HASH (open addressing for collisions).3. Read S2 pg2 to IN 4. MOD3 hash IN to HASH (open addressing for collisions).5.Read E2 to IN. 6.Hash to HASH. 7. If match, Concatenate to OUT...

17|BAID |NY2091

32|THAISZ|NJ5102

38|GOOD |FL6321

17|5 |96|NJ5101 32|8 |89|NY2091

BAID |5|96

Section 9 # 42

Page 25: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

CODE GENERATION implements HYBRID JOIN

S0

S1

S2

PROBE S1, E1: 1. Read S1 pg1 to IN. 2. Hash IN to HASH. 3. Read E1 to IN 4. Concatenate to OUT.5. Read (next page of) E1 to IN. 6. Hash to HASH. 7. If match, Concatenate to OUT... 8. Flush HASH and IN when done Probing E1.

17|BAID |NY2091

25|CLAY |NJ5101

32|THAISZ|NJ5102

38|GOOD |FL6321

E0

E1

E2

25|6 |76|NY2091

17|5 |96|NJ5101

25|7 |68|ND4456

32|8 |89|NY2091

32|7 |91|FL6320

34|6 |62|ND4456

CLAY|7 |68

CLAY|6 |76

0

OUT

IN

123

Hash

PROBE S2, E2: 1. Read S2 pg1 to IN 2. MOD3 hash IN to HASH (open addressing for collisions).3. Read S2 pg2 to IN 4. MOD3 hash IN to HASH (open addressing for collisions).5.Read E2 to IN. 6.Hash to HASH. 7. If match, Concatenate to OUT...

17|BAID |NY2091

32|THAISZ|NJ5102

38|GOOD |FL6321

32|8 |89|NY2091

BAID |5|96 THAISZ|8|89

Section 9 # 43

Page 26: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

CODE GENERATION implements HYBRID JOIN

S0

S1

S2

PROBE S1, E1: 1. Read S1 pg1 to IN. 2. Hash IN to HASH. 3. Read E1 to IN 4. Concatenate to OUT.5. Read (next page of) E1 to IN. 6. Hash to HASH. 7. If match, Concatenate to OUT... 8. Flush HASH and IN when done Probing E1.

17|BAID |NY2091

25|CLAY |NJ5101

32|THAISZ|NJ5102

38|GOOD |FL6321

E0

E1

E2

25|6 |76|NY2091

17|5 |96|NJ5101

25|7 |68|ND4456

32|8 |89|NY2091

32|7 |91|FL6320

34|6 |62|ND4456

CLAY |7|68

CLAY |6|76

0

OUT

IN

123

Hash

PROBE S2, E2: 1. Read S2 pg1 to IN 2. MOD3 hash IN to HASH (open addressing for collisions).3. Read S2 pg2 to IN 4. MOD3 hash IN to HASH (open addressing for collisions).5.Read E2 to IN. 6.Hash to HASH. 7. If match, Concatenate to OUT...(repeat until E2 empty)8. Flush HASH and IN when done Probing E1.

17|BAID |NY2091

32|THAISZ|NJ5102

38|GOOD |FL6321

32|7 |91|FL6320

BAID |5|96 THAISZ|8|89

THAISZ|7|91

Section 9 # 44

Page 27: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

2,0|32|THAISZ|NJ51021,0|17|BAID |NY2091

2,1|57|BROWN |NY20921,1|25|CLAY |NJ5101

3,0|38|GOOD |FL6321

2,0|25|6 |76|NY2091

h=MOD3 k=MOD4 SELECT SNAME,C#,GRADE FROM S,E WHERE S.S#=E.S#;

CODE generation of HYBRID HASH JOIN using R2 as internal hash bucket!

SRID|S#|SNAME | LCODE1,0|17|BAID |NY2091

3,0|38|GOOD |FL6321

1,1|25|CLAY |NJ5101

2,0|32|THAISZ|NJ5102

2,1|57|BROWN |NY2092

ERID|S#|C#|GR| LCODE 1,0|17|5 |96|NJ5101

1,1|25|7 |68|ND4456

2,1|32|8 |89|NY2091

3,0|32|7 |91|FL6320

3,1|34|6 |62|ND4456

IN

BUILD S: Read each S-page to IN, hash each record using h(S#)=MOD3(S#) to R0,R1,R2. If h(S#)=2, k(S#) determines R2 slot (open addressing for R2). h collisions in R1, R0 flush to file, S1, S0 resp.

S0

25|CLAY |NJ5101

57|BROWN |NY2092

E0R1

R0

R2

0123

OUT

E1

S1

Section 9 # 45

Page 28: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

17|BAID |NY2091

32|THAISZ|NJ5102

38|GOOD |FL6321

2,0|25|6 |76|NY2091

h=MOD3 k=MOD4 SELECT SNAME,C#,GRADE FROM S,E WHERE S.S#=E.S#; CODE generation implements HYBRID HASH JOIN using R2 as internal hash bucket!

SRID|S#|SNAME | LCODE1,0|17|BAID |NY2091

3,0|38|GOOD |FL6321

1,1|25|CLAY |NJ5101

2,0|32|THAISZ|NJ5102

2,1|57|BROWN |NY2092

ERID|S#|C#|GR| LCODE 1,0|17|5 |96|NJ5101

1,1|25|7 |68|ND4456

2,1|32|8 |89|NY2091

3,0|32|7 |91|FL6320

3,1|34|6 |62|ND4456

IN

BUILD E: Read first E-page to IN, hash each record using h(S#)=MOD3(S#) to R0,R1,R2. If h(S#)=2, k(S#) determines R2 slot (open addressing for R2). If match, concatenate to OUT. h collisions in R1, R0 flush to file, E1, E0 respectively.

S0

S1

S2

25|CLAY |NJ5101

57|BROWN |NY2092

E0R1

R0

R2

0123

OUT

E1

S1

1,0|17|5 |96|NJ5101

1,1|25|7 |68|ND4456

BAID |5 |96

Section 9 # 46

Page 29: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

17|BAID |NY2091

32|THAISZ|NJ5102

38|GOOD |FL6321

2,0|25|6 |76|NY2091

h=MOD3 k=MOD4 SELECT SNAME,C#,GRADE FROM S,E WHERE S.S#=E.S#;

SRID|S#|SNAME | LCODE1,0|17|BAID |NY2091

3,0|38|GOOD |FL6321

1,1|25|CLAY |NJ5101

2,0|32|THAISZ|NJ5102

2,1|57|BROWN |NY2092

ERID|S#|C#|GR| LCODE 1,0|17|5 |96|NJ5101

1,1|25|7 |68|ND4456

2,1|32|8 |89|NY2091

3,0|32|7 |91|FL6320

3,1|34|6 |62|ND4456

IN

BUILD E: Read second E-page to IN, hash each record using h(S#)=MOD3(S#) to R0,R1,R2. If h(S#)=2, k(S#) determines R2 slot (open addressing for R2). If match, concatenate to OUT. h collisions in R1, R0 flush to file, E1, E0 respectively.

S0

S1

S2

25|CLAY |NJ5101

57|BROWN |NY2092

E0R1

R0

R2

0123

OUT

E1

S1

25|7 |68|ND4456

BAID |5 |96

2,0|25|6 |76|NY2091

2,1|32|8 |89|NY2091

THAISZ|8 |89

CODE GEN implements HYBRID HASH JOIN using R2 as internal hash bucket!

Section 9 # 47

Page 30: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

17|BAID |NY2091

32|THAISZ|NJ5102

38|GOOD |FL6321

2,0|25|6 |76|NY2091

h=MOD3 k=MOD4 SELECT SNAME,C#,GRADE FROM S,E WHERE S.S#=E.S#;

SRID|S#|SNAME | LCODE1,0|17|BAID |NY2091

3,0|38|GOOD |FL6321

1,1|25|CLAY |NJ5101

2,0|32|THAISZ|NJ5102

2,1|57|BROWN |NY2092

ERID|S#|C#|GR| LCODE 1,0|17|5 |96|NJ5101

1,1|25|7 |68|ND4456

2,1|32|8 |89|NY2091

3,0|32|7 |91|FL6320

3,1|34|6 |62|ND4456

IN

BUILD E: Read third E-page to IN, hash each record using h(S#)=MOD3(S#) to R0,R1,R2. If h(S#)=2, k(S#) determines R2 slot (open addressing for R2). If match, concatenate to OUT. h collisions in R1, R0 flush to file, E1, E0 respectively. When done building E, flush R2.

S0

S1

S2

25|CLAY |NJ5101

57|BROWN |NY2092

E0R1

R0

R2

0123

OUT

E1

S1

25|7 |68|ND4456

BAID |5 |96

25|6 |76|NY2091

THAISZ|8 |89

3,0|32|7 |91|FL6320

3,1|34|6 |62|ND4456

THAISZ|7 |91

25|7 |68|ND4456

25|6 |76|NY2091

34|6 |62|ND4456

CODE GEN implements HYBRID HASH JOIN using R2 as internal hash bucket!

Section 9 # 48

Page 31: J1.  NESTED LOOP  JOIN S on S.S#=E.S#

Note: If memory allocation is static, use all Ri pages for internal hash function! So k=MOD8

CODE GEN implements HYBRID HASH JOIN using R2 as internal hash bucket probe:

PROBE S:

S0

S1

S2

25|CLAY |NJ5101

57|BROWN |NY2092

E0

E1

S1

THAISZ|7 |91

25|7 |68|ND4456

25|6 |76|NY2091

34|6 |62|ND4456

IN

R2

0123

OUT

4567

25|CLAY |NJ5101

25|7 |68|ND4456

25|6 |76|NY2091

CLAY |7 |68

BAID |5 |96

THAISZ|8 |89

THAISZ|6 |76

h=MOD3 k=MOD4 SELECT SNAME,C#,GRADE FROM S,E WHERE S.S#=E.S#;

Notice how much more efficient the probe phase of HH JOIN is than Grace JOIN when the internal Hash table is chosen to apply to the right bucket!

(And how it may not be faster, if that decision is badly made!)

Section 9 # 49