Exercise 6: Indexes and Join Algorithms Suggested Answers
1. 3

![]()

This
is the same as Figure 10.16 in the book on page 354.

Circle the pages you would need to read in order to find all entries >= key
value 15.
Number the circled pages in the order that you would read them.
The pages are circled and numbered above.
2. For this question, use the figure above. Assume that the index is over an attribute called “age”.
Now, make the following assumptions:
3. Suppose
I am joining two tables A and B using the join condition A.field1 = B.field1.
Suppose that table A is already sorted into ascending order on field1.
Suppose that table B is already sorted into ascending order on field1.
a. Will
the Page-oriented nested loops join algorithm, as shown on slide 2 of lecture
6, work any slower or any faster on these sorted tables than it would if tables
A and B were not sorted? (In other words, is the Page-oriented nested loops
join algorithm able to exploit the fact that one or both of its input tables
are sorted on the join attribute?) Explain briefly.
No. The
page-oriented nested loops join algorithm works the same whether the data is
sorted or unsorted. You can think of it
as a “brute force” join algorithm because it cycles through every combination
of one row from the left table with one row from the right table.
Note: someone could write a new algorithm, maybe called “sort-aware-page-oriented
nested loops join algorithm” that would expect to be told whether or not either
of the input table was sorted on the join key and then it would be able to “stop
early” on the scan of the right table in the inner loop. The might be other optimizations possible, as
well.
b. Can
you use the page-oriented nested loops algorithm to compute the cross product
of two tables A and B? Explain
briefly. (For the purpose of this
question, you can assume that A and B are not sorted.)
Yes. The basic
page-oriented nested loops algorithm is computing the cross product – by cycling
through all possible combinations of one row from the left table with one row
from the right table. You could use it
by specifying a join condition of “true” or some join condition that always evaluates
to true such as “6 = 6”.
c. Can
you use the index-nested loops algorithm to compute the cross product of two
tables A and B? Explain briefly. (If you are able to use this algorithm, then
explain what sort of index you would use for the table in the inner loop.)
No. It won’t
work to use the indexed-nested loops algorithm to compute the cross
product. For a cross product, you need
to scan the entire table (the table on the right) in the inner loop. The indexed nested loops algorithm expects to
read data from the table on the right using some search key.
4. Challenge
question (on your own, if you want to): Devise an algorithm that calculates the
DIVIDE operator, and calculate the number of I/O's your algorithm takes. (Do the DIVIDE directly – in a single
operator.)
I leave this as a question for you folks to discuss
using the e-mail list. If you think you
have an algorithm for the divide operator you can send it out to the class
e-mail list. Other students may look
decide that they want to look at your algorithm and comment on it.