<s>
The	O
sort-merge	B-Algorithm
join	I-Algorithm
(	O
also	O
known	O
as	O
merge	B-Algorithm
join	I-Algorithm
)	O
is	O
a	O
join	O
algorithm	O
and	O
is	O
used	O
in	O
the	O
implementation	O
of	O
a	O
relational	B-Application
database	I-Application
management	O
system	O
.	O
</s>
<s>
The	O
basic	O
problem	O
of	O
a	O
join	O
algorithm	O
is	O
to	O
find	O
,	O
for	O
each	O
distinct	O
value	O
of	O
the	O
join	O
attribute	O
,	O
the	O
set	O
of	O
tuples	B-Application
in	O
each	O
relation	O
which	O
display	O
that	O
value	O
.	O
</s>
<s>
In	O
practice	O
,	O
the	O
most	O
expensive	O
part	O
of	O
performing	O
a	O
sort-merge	B-Algorithm
join	I-Algorithm
is	O
arranging	O
for	O
both	O
inputs	O
to	O
the	O
algorithm	O
to	O
be	O
presented	O
in	O
sorted	O
order	O
.	O
</s>
<s>
This	O
can	O
be	O
achieved	O
via	O
an	O
explicit	O
sort	O
operation	O
(	O
often	O
an	O
external	B-Algorithm
sort	I-Algorithm
)	O
,	O
or	O
by	O
taking	O
advantage	O
of	O
a	O
pre-existing	O
ordering	O
in	O
one	O
or	O
both	O
of	O
the	O
join	O
relations	O
.	O
</s>
<s>
The	O
latter	O
condition	O
,	O
called	O
interesting	O
order	O
,	O
can	O
occur	O
because	O
an	O
input	O
to	O
the	O
join	O
might	O
be	O
produced	O
by	O
an	O
index	O
scan	O
of	O
a	O
tree-based	O
index	O
,	O
another	O
merge	B-Algorithm
join	I-Algorithm
,	O
or	O
some	O
other	O
plan	O
operator	O
that	O
happens	O
to	O
produce	O
output	O
sorted	O
on	O
an	O
appropriate	O
key	O
.	O
</s>
<s>
So	O
,	O
in	O
the	O
worst	O
case	O
sort-merge	B-Algorithm
join	I-Algorithm
will	O
run	O
in	O
I/Os	O
.	O
</s>
<s>
Note	O
that	O
this	O
implementation	O
assumes	O
the	O
join	O
attributes	O
are	O
unique	O
,	O
i.e.	O
,	O
there	O
is	O
no	O
need	O
to	O
output	O
multiple	O
tuples	B-Application
for	O
a	O
given	O
value	O
of	O
the	O
key	O
.	O
</s>
