For Equijoins, the most efficient join is achieved when both relations are sorted on the join
attributes. In this case, we can look for qualifying tuples of R and S by merging the two
relations. If they are not sorted, a preprocessing step can be carried out to sort them. Since
the relations are in sorted order, tuples with the same join attribute value are guaranteed to
be in consecutive order. If we assume that the join is many-to-many, that is there can be
many tuples of both R and S with the same join value, and if we assume that each set of
tuples with the same join value can be held in the database buffer at the same time, then
each block of each relation need only be read once. Therefore, the cost estimate for the
sort–merge join is: