Slideshow:
R −Set S |
R = {1, 2, 3}; S = {2, 3, 4}; R −Set S = { 1 } |
|
1. R - S (S is the smaller of the 2 relations) 2. S - R (S is the smaller of the 2 relations) |
|
S = {1, 2, 3, 4}; R = {2, 3, 5, 6, 7}; {1, 2, 3, 4} − {2, 3, 5, 6, 7} = {1, 4}; ^^^^^^^^^^ Index this set |
initialize a search structure H on all attributes ; /* ============================================================ Phase 1: Use 1 buffer and scan the SMALLER relation first. Build a search structure on the SMALLER relation ============================================================ */ while ( S has more data blocks ) { read 1 data block in buffer b; for ( each tuple t ∈ b ) { /* ===================================================== We need a search structure H to implement the test t ∈ H efficiently !!! We can use hash table or some bin. search tree ====================================================== */ insert t in H; } } /* =================================================== Now we know the elements in S =================================================== */ /* ============================================================ Phase 2: output tuples in S Use 1 buffer and scan the other relation. Use the search structure to remove the common elements !! ============================================================ */ while ( R has more data blocks ) { read 1 data block in buffer b; for ( each tuple t ∈ b ) { /* ===================================================== We use search structure H to implement the test t ∈ H efficiently !!! We can use hash table or some bin. search tree ====================================================== */ if ( t ∈ H ) { Delete t from H; // t is "subtracted" !!! } else { // Ignore t, it's not in difference ! } } } /* =================================================== ONLY now we can output the difference =================================================== */ for ( every t ∈ H ) { output t ; } |
Buffer utilization when there are M buffers available:
|
S = {1, 2, 3, 4}; R = {2, 3, 5, 6, 7}; {2, 3, 5, 6, 7} − {1, 2, 3, 4} = {5, 6, 7}; ^^^^^^^^^^ Index this set |
initialize a search structure H on all attributes of γ; /* ============================================================ Phase 1: (same) Use 1 buffer and scan the SMALLER relation first. Build a search structure on the SMALLER relation The search structure contains a count for the search key ============================================================ */ while ( S has more data blocks ) { read 1 data block in buffer b; for ( each tuple t ∈ b ) { /* ===================================================== We need a search structure H to implement the test t ∈ H efficiently !!! We can use hash table or some bin. search tree ====================================================== */ insert t in H; } } /* =================================================== Now we know the elements in S =================================================== */ /* ============================================================ Phase 2: output tuples in R Use 1 buffer and scan the other relation. Use the search structure to "stop output" of common elements ============================================================ */ while ( R has more data blocks ) { read 1 data block in buffer b; for ( each tuple t ∈ b ) { /* ===================================================== We use search structure H to implement the test t ∈ H efficiently !!! We can use hash table or some bin. search tree ====================================================== */ if ( t ∉ H ) { Output t; // Tuple did not get subtracted ! } } } |
Buffer utilization when there are M buffers available:
|
|
|