edited by
31,990 views
67 67 votes

Consider a join (relation algebra) between relations $r(R)$ and $s(S)$ using the nested loop method. There are $3$ buffers each of size equal to disk block size, out of which one buffer is reserved for intermediate results. Assuming $\text{size}(r(R))<\text{size}(s(S)),$ the join will have fewer number of disk block accesses if

  1. relation $r(R)$ is in the outer loop.
  2. relation $s(S)$ is in the outer loop.
  3. join selection factor between $r(R)$ and $s(S)$ is more than $0.5$.
  4. join selection factor between $r(R)$ and $s(S)$ is less than $0.5$.

7 Answers

Best answer
50 50 votes

In joining A and B using nested loop method, with A in outer loop two factors are involved.

  1. No. of  blocks containing all rows in A should be fetched.
  2. No. of rows of A times no. of blocks containing all rows of B.

(in worst case all rows of B are matched with all rows of A).

In above ques, $|R|<|S|$

(i) will be less when number of rows in outer table is less, since less no. of rows will take less no. of blocks

(ii) if we keep $R$ in outer loop, no. of rows in $R$ are less and no. of blocks in $S$ are more

If we keep $S$ in outer loop, no of rows in $S$ are more and no. of blocks in $R$ are less.

In (ii) block accesses will be multiplication and will come same in both cases.

So, (i) will determine no of block accesses

So, answer is A.

edited by
9 9 votes

Nested loop join Algo: https://en.wikipedia.org/wiki/Nested_loop_join

No. of block transfers, N(B) = $n_r*b_s+b_r$ ; when R is in outer loop and S is in inner loop

Where, $n_r$ are number of tuples in relation R.

$b_s$ and $b_r$ are number of blocks in relation S and R respectively.

size(r(R))<size(s(S)) blocks occupied by R are less ($b_r<b_s$). Also, ($n_r<n_s$). As, less no of blocks acquire less no. of rows. 

For minimizing N(B), product term must be minimum and it will be minimum when R is in the outer loop.

For Example: let $b_r=10 , b_s=20; n_r=50, n_s=100$ since size(r(R))<size(s(S))

1. when R is in the Outer loop: N(B) = $n_r*b_s+b_r$ = 50*20+10 = 1010.

2. when S is in the outer loop: N(B) = $n_s*b_r+b_s$ = 100*10+20 = 1020.

So, relation R should be in outer loop.

Option(A).

4 4 votes
for each tuple r in R:
    for each tuple s in S:
        if r.a = s.b:
            output (r, s)
this is nested loop method and

let assume r is 5 disk blocks and s is 10 disk blocks

if r is outer relation then for each block access the inner block should be accessed i.e r*s for inner loop and r for outer relation which is r+r*s which is 5+5*10=55

similarly if we swap then the block accesses becomes 10+10*5=60

so r(R) is at outer loop

OPTION A IS THE RIGHT ANSWER

i hope this is helpful for you
1 1 vote

The answer is option $\left ( A\right )$

But there is a misconception in the explanations given so far which I am rectifying here.

I take the example given in Database System Concepts, 5th Edition, Silberschatz Et.al

Number of records of customer: $n_{customer_{}} = 10,000$

Number of blocks of customer: $b_{customer_{}} = 400$ 

Number of records of depositor: $n_{depositor_{}} = 5,000 $

Number of blocks of depositor: $b_{depositor_{}} = 100$ 

Consider the Formula for number of Block Transfer $\left ( n_{r}\times b_{s} \right )+b_{r}$ , Where $r$ is outer relation and $s$ is the inner relation.

Now consider $r\Rightarrow depositor$ and $s\Rightarrow customer$ (That is $depositor$ is the smaller and considered as outer relation)

By substituting values $\left ( 5000\times 400 \right )+100 = 20,00,100$

Now consider $r\Rightarrow customer$ and $s\Rightarrow depositor$ (That is $customer$ is the larger and considered as outer relation)

By substituting values $\left ( 10000\times 100 \right )+400 = 10,00,400$

So by observing this example, it is obvious that even if we consider the outer table to be smaller, the number of block transfer seems to be higher than considering the outer table to be larger.

But if you observe the question carefully, they are asking about Block Access (in particular Block Seek) rather than asking Block Transfer.

So the Number of Block Seek is $n_{r}+b_{r}$ and this is minimized only when outer relation $r$ is smaller.

Form the above example 

$r\Rightarrow depositor :$ $n_{r}+b_{r} = 5000+100 = 5,100$

$r\Rightarrow customer :$ $n_{r}+b_{r} = 10000+400 = 10,400$

$\therefore$ Option $\left ( A\right )$

 

0 0 votes

The join between r(R) and s(S) which is using nested loop will be as follows,
For each tuple r in R do
    For each tuple s in S do
        If r and s satisfy the join condition then output the tuple 

The above algorithm involves (nr*bs+br ) block transfer and (nr+br ) seeks.
where,
br → no. of blocks in R
bs → no. of blocks in S
nr → no. of tuples in R
To have less block accesses in nr should be less and in question it is given that size(r(R)) < size(s(S)).
To have fewer no. of disk block accesses the relation r(R) should be in outer loop.   

Answer:
Position:
Show:

Related questions

123 123 votes
8 8 answers
30.5k
30.5k views
go_editor asked Sep 28, 2014
30,538 views
SQL allows duplicate tuples in relations, and correspondingly defines the multiplicity of tuples in the result of joins. Which one of the following queries always gives t...
88 88 votes
4 answers 4 answers
45.1k
45.1k views
go_editor asked Sep 28, 2014
45,072 views
Consider the following schedule S of transactions $T1, T2, T3, T4:$$${\begin{array}{|l|l|l|l|}\hline\textbf{T1}& \textbf{T2}& \textbf{T3}& \textbf{T4} \\\hline& \...
121 121 votes
9 answers 9 answers
47.7k
47.7k views
go_editor asked Sep 28, 2014
47,657 views
Consider the main memory system that consists of $8$ memory modules attached to the system bus, which is one word wide. When a write request is made, the bus is occupied ...
41 41 votes
6 answers 6 answers
15.1k
15.1k views
go_editor asked Sep 28, 2014
15,101 views
Which one of the following Boolean expressions is NOT a tautology?$((\,a\,\to\,b\,)\,\wedge\,(\,b\,\to\,c))\,\to\,(\,a\,\to\,c)$$(\,a\,\to\,c\,)\,\to\,(\,\sim b\,\to\,(a\...