9
Query Execution :Nested-Loop Joins Rohit Deshmukh ID 120 CS-257

Query Execution :Nested-Loop Joins Rohit Deshmukh ID 120 CS-257 Rohit Deshmukh ID 120 CS-257

  • View
    220

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Query Execution :Nested-Loop Joins Rohit Deshmukh ID 120 CS-257 Rohit Deshmukh ID 120 CS-257

Query Execution :Nested-Loop Joins

Query Execution :Nested-Loop Joins

Rohit Deshmukh

ID 120

CS-257

Rohit Deshmukh

ID 120

CS-257

Page 2: Query Execution :Nested-Loop Joins Rohit Deshmukh ID 120 CS-257 Rohit Deshmukh ID 120 CS-257

Tuple-Based Nested-Loop JoinTuple-Based Nested-Loop Join

1. The Nested-Loop joins can be used for relations of any size

2. Hence it is not necessary one relation fits in main memory.

3. In this algorithm we compute join

R(X,Y) join S(Y,Z)

1. The Nested-Loop joins can be used for relations of any size

2. Hence it is not necessary one relation fits in main memory.

3. In this algorithm we compute join

R(X,Y) join S(Y,Z)

Page 3: Query Execution :Nested-Loop Joins Rohit Deshmukh ID 120 CS-257 Rohit Deshmukh ID 120 CS-257

Tuple-Based Nested-Loop JoinTuple-Based Nested-Loop Join

R(X,Y) join S(Y,Z)

Algorithm

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;

R(X,Y) join S(Y,Z)

Algorithm

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;

Page 4: Query Execution :Nested-Loop Joins Rohit Deshmukh ID 120 CS-257 Rohit Deshmukh ID 120 CS-257

Tuple-Based Nested-Loop JoinTuple-Based Nested-Loop Join

1. To lower the number of disk I/O’s there are two cases

§ Use of index on the join attribute or attributes of R to find the tuples of R that match a given tuple of S

§ Divide the tuples R,S in blocks and use as much of the memory as it can to reduce the number of disk I/O’s.

1. To lower the number of disk I/O’s there are two cases

§ Use of index on the join attribute or attributes of R to find the tuples of R that match a given tuple of S

§ Divide the tuples R,S in blocks and use as much of the memory as it can to reduce the number of disk I/O’s.

Page 5: Query Execution :Nested-Loop Joins Rohit Deshmukh ID 120 CS-257 Rohit Deshmukh ID 120 CS-257

Iterator for Tuple-Based Nested-Loop Join

Iterator for Tuple-Based Nested-Loop Join

One advantage of nested loop join is that it fits well into an iterator framework.

This allows us to avoid storing intermediate relations on disk in some situations

The iterator R join S is easy to build from iterators of R and S.

It makes the assumption that neither relation R nor S is empty.

One advantage of nested loop join is that it fits well into an iterator framework.

This allows us to avoid storing intermediate relations on disk in some situations

The iterator R join S is easy to build from iterators of R and S.

It makes the assumption that neither relation R nor S is empty.

Page 6: Query Execution :Nested-Loop Joins Rohit Deshmukh ID 120 CS-257 Rohit Deshmukh ID 120 CS-257

Block-Based Nested-Loop JoinBlock-Based Nested-Loop Join

The nested-loop join can be improved if we compute R join S as:

Organizing access to both argument relations by blocks

Using as much main memory as we can to store tuples belonging to relation S.

The nested-loop join can be improved if we compute R join S as:

Organizing access to both argument relations by blocks

Using as much main memory as we can to store tuples belonging to relation S.

Page 7: Query Execution :Nested-Loop Joins Rohit Deshmukh ID 120 CS-257 Rohit Deshmukh ID 120 CS-257

Block-Based Nested-Loop JoinBlock-Based Nested-Loop Join

The first point makes sure that when we run through tuples in R we use few disk I/O’s to read R

Second point enables us to join each tuple of R with as many tuples of S as will fit in memory

The first point makes sure that when we run through tuples in R we use few disk I/O’s to read R

Second point enables us to join each tuple of R with as many tuples of S as will fit in memory

Page 8: Query Execution :Nested-Loop Joins Rohit Deshmukh ID 120 CS-257 Rohit Deshmukh ID 120 CS-257

Nested-Loop Join AlgorithmNested-Loop Join AlgorithmFOR each chunk of M-1 blocks of S DO BEGIN

read blocks in main memory;organize their tuples such that search key is common attribute;FOR each block b of R DO BEGIN

read b into main memory;FOR each tuple t of v 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;End;

End;

FOR each chunk of M-1 blocks of S DO BEGINread blocks in main memory;organize their tuples such that search key is common attribute;FOR each block b of R DO BEGIN

read b into main memory;FOR each tuple t of v 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;End;

End;

Page 9: Query Execution :Nested-Loop Joins Rohit Deshmukh ID 120 CS-257 Rohit Deshmukh ID 120 CS-257

Nested loop Join AlgorithmNested loop Join Algorithm

Assuming S is the smaller relation, the iterations of the outer loop is:

B(S) / (M-1).

The number of Disk I/O’s is

B(S) + [B(S) B(R) / (M-1) ]

Assuming S is the smaller relation, the iterations of the outer loop is:

B(S) / (M-1).

The number of Disk I/O’s is

B(S) + [B(S) B(R) / (M-1) ]