110 likes | 248 Views
QUERY EXECUTION. 15.3 Nested-Loop Joins. By: Saloni Tamotia (215). Introduction to Nested-Loop Joins. Used for relations of any side. Not necessary that relation fits in main memory Uses “ One-and-a-half” pass method in which for each variation: One argument read just once.
E N D
QUERY EXECUTION 15.3 Nested-Loop Joins By: SaloniTamotia (215)
Introduction to Nested-Loop Joins • Used for relations of any side. • Not necessary that relation fits in main memory • Uses “One-and-a-half” pass method in which for each variation: • One argument read just once. • Other argument read repeatedly. • Two kinds: • Tuple-Based Nested Loop Join • Block-Based Nested Loop Join
ADVANTAGES OF NESTED-LOOP JOIN • Fits in the iterator framework. • Allows us to avoid storing intermediate relation on disk.
Tuple-Based Nested-Loop Join • Simplest variation of the nested-loop join • Loop ranges over individual tuples
Tuple-Based Nested-Loop Join • Algorithm to compute the Join R(X,Y) | | S(Y,Z) FOR each tuple s in S DO FOR each tuple r in R DO IF r and s join to make tuple t THEN output t • R and S are two Relations with r and s as tuples. • carelessness in buffering of blocks causes the use of T(R)T(S) disk I/O’s
IMPROVEMENT & MODIFICATION To decrease the cost • Method 1: Use algorithm for Index-Based joins • We find tuple of R that matches given tuple of S • We need not to read entire relation R • Method 2: Use algorithm for Block-Based joins • Tuples of R & S are divided into blocks • Uses enough memory to store blocks in order to reduce the number of disk I/O’s.
Block-Based Nested-Loop Join Algorithm • Access to arguments is organized by block. • While reading tuples of inner relation we use less number of I/O’s disk. • Using enough space in main memory to store tuples of relation of the outer loop. • Allows to join each tuple of the inner relation with as many tuples as possible.
Block-Based Nested-Loop Join Algorithm ALGORITHM: FOR each chunk of M-1 blocks of SDO FOR each block b of RDO FOR each tuplet of bDO find the tuples of S in memory that join with t output the join of t with each of these tuples
Block-Based Nested-Loop Join Algorithm • Assumptions: • B(S) ≤ B(R) • B(S) > M This means that the neither relation fits in the entire main memory.
Analysis of Nested-Loop Join • Number of disk I/O’s: [B(S)/(M-1)]*(M-1 +B(R)) or B(S) + [B(S)B(R)/(M-1)] or approximately B(S)*B(R)/M