Download presentation
Presentation is loading. Please wait.
1
15.3 Nested-Loop Joins - Medha Pradhan - ID: 203 - CS 257 Section 2 - Spring 2008
2
Agenda What are Nested-Loop Joins Types of Nested-Loop Joins Tuple Based Nested-Loop Join Block-Based Nested Loop Join Analysis of Nested-Loop Joins
3
What are Nested-Loop Joins “One and a half passes” algorithms One argument has its tuples read only once Second argument is read repeatedly Nested-Loop joins can be used for relations of any size
4
Tuple-Based Nested Loop Joins Loops range over individual tuples of the relation Join is computed as follows: FOR each tuple s in S do FOR each tuple r in R do IF r and s join to make a tuple t THEN output t;
5
Tuple-Based Nested Loop Joins (cont’d) Need to be careful regarding buffering of blocks for relations R and S Worst case: T(R)T(S) disk I/Os Improvements: Using indexes on the join attributes Using block-based version of the nested-loop join
6
Iterator for Tuple-based Nested-Loop Joins Nested-Loop Joins fit easily into the iterator framework Allow us to avoid storing intermediate relations on disk
7
Open(){ R.Open(); S.Open(); s := S.GetNext(); } GetNext() { REPEAT { r := R.GetNext(); IF (r = NotFound) { R.Close(); s := S.GetNext(); IF (s = NotFound) RETURN NotFound; R.Open(); r := R.GetNext(); } UNTIL(r and s join); RETURN the join of r and s; } Close() { R.Close(); S.Close(); }
8
Block-Based Nested Loop Algorithm Tuple-based Nested-Loop Join can be improved by Organizing access to both argument relations by blocks. This permits fewer disk I/Os to read R (inner loop) Using as much main memory as we can to store tuples belonging to the relation of the outer loop.
9
FOR each chunk of M-1 blocks of S DO BEGIN read these blocks into main memory buffers; organize their tuples into a search structure whose search key is the common attributes of R and S; FOR each block B of R DO BEGIN read b into main memory; FOR each tuple t of b DO BEGIN find the tuples of S in main memory that join with t; output the join of t with each of these tuples; END;
10
Example of Block Based Nested-Loop Join Algorithm Let B(R) = 1000; B(S) = 500, M = 101 Thus, 100 blocks of memory are used to buffer S into 100 block chunks. Thus, outer loop iterates 5 times At each iteration we do 100 disk I/Os to read S. Reading R will require 1000 disk I/Os at each iteration Thus, total number of disk I/Os = 5 * (100 + 1000) = 5500
11
Example (cont’d) If we reverse R and S, number of disk I/Os will increase. i.e. Number of disk I/Os = 10 * (100 + 500) = 6000 In general, using smaller relation in the outer loop requires fewer I/Os.
12
Analysis of Nested-Loop Joins Given: B(R), B(S), M and B(S) < B(R) Thus number of iterations of the outer loop: B(S)/(M-1) Total number of disk I/Os: (B(S)/(M-1) ) * (M-1 + B(R)) = B(S) + (B(S)*B(R)/(M-1)) Which is approximately equal to: B(S)*B(R)/M If B(S) < M-1, then nested-loop join becomes identical to one-pass join
13
Thank You!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.