<s>
The	O
hash	B-Algorithm
join	I-Algorithm
is	O
an	O
example	O
of	O
a	O
join	B-Language
algorithm	I-Language
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>
All	O
variants	O
of	O
hash	B-Algorithm
join	I-Algorithm
algorithms	O
involve	O
building	O
hash	B-Algorithm
tables	I-Algorithm
from	O
the	O
tuples	O
of	O
one	O
or	O
both	O
of	O
the	O
joined	O
relations	O
,	O
and	O
subsequently	O
probing	O
those	O
tables	O
so	O
that	O
only	O
tuples	O
with	O
the	O
same	O
hash	O
code	O
need	O
to	O
be	O
compared	O
for	O
equality	O
in	O
equijoins	O
.	O
</s>
<s>
Hash	B-Algorithm
joins	I-Algorithm
are	O
typically	O
more	O
efficient	O
than	O
nested	O
loops	O
joins	O
,	O
except	O
when	O
the	O
probe	O
side	O
of	O
the	O
join	O
is	O
very	O
small	O
.	O
</s>
<s>
They	O
require	O
an	O
equijoin	O
predicate	B-Application
(	O
a	O
predicate	B-Application
comparing	O
records	O
from	O
one	O
table	O
with	O
those	O
from	O
the	O
other	O
table	O
using	O
a	O
conjunction	O
of	O
equality	O
operators	O
'	O
=	O
'	O
on	O
one	O
or	O
more	O
columns	O
)	O
.	O
</s>
<s>
The	O
classic	O
hash	B-Algorithm
join	I-Algorithm
algorithm	O
for	O
an	O
inner	O
join	O
of	O
two	O
relations	O
proceeds	O
as	O
follows	O
:	O
</s>
<s>
First	O
,	O
prepare	O
a	O
hash	B-Algorithm
table	I-Algorithm
using	O
the	O
contents	O
of	O
one	O
relation	O
,	O
ideally	O
whichever	O
one	O
is	O
smaller	O
after	O
applying	O
local	O
predicates	O
.	O
</s>
<s>
The	O
hash	B-Algorithm
table	I-Algorithm
entries	O
are	O
mappings	O
from	O
the	O
value	O
of	O
the	O
(	O
composite	O
)	O
join	O
attribute	O
to	O
the	O
remaining	O
attributes	O
of	O
that	O
row	O
(	O
whichever	O
ones	O
are	O
needed	O
)	O
.	O
</s>
<s>
Once	O
the	O
hash	B-Algorithm
table	I-Algorithm
is	O
built	O
,	O
scan	O
the	O
other	O
relation	O
(	O
the	O
probe	O
side	O
)	O
.	O
</s>
<s>
For	O
each	O
row	O
of	O
the	O
probe	O
relation	O
,	O
find	O
the	O
relevant	O
rows	O
from	O
the	O
build	O
relation	O
by	O
looking	O
in	O
the	O
hash	B-Algorithm
table	I-Algorithm
.	O
</s>
<s>
Similarly	O
,	O
the	O
join	O
relation	O
on	O
which	O
the	O
hash	B-Algorithm
table	I-Algorithm
is	O
built	O
is	O
called	O
the	O
"	O
build	O
"	O
input	O
,	O
whereas	O
the	O
other	O
input	O
is	O
called	O
the	O
"	O
probe	O
"	O
input	O
.	O
</s>
<s>
If	O
the	O
size	O
of	O
the	O
hash	B-Algorithm
table	I-Algorithm
equals	O
the	O
maximum	O
in-memory	O
size	O
:	O
</s>
<s>
This	O
is	O
essentially	O
the	O
same	O
as	O
the	O
block	B-Algorithm
nested	I-Algorithm
loop	I-Algorithm
join	B-Language
algorithm	I-Language
.	O
</s>
<s>
A	O
better	O
approach	O
is	O
known	O
as	O
the	O
"	O
grace	O
hash	B-Algorithm
join	I-Algorithm
"	O
,	O
after	O
the	O
GRACE	O
database	O
machine	O
for	O
which	O
it	O
was	O
first	O
implemented	O
.	O
</s>
<s>
The	O
algorithm	O
then	O
loads	O
pairs	O
of	O
partitions	O
into	O
memory	O
,	O
builds	O
a	O
hash	B-Algorithm
table	I-Algorithm
for	O
the	O
smaller	O
partitioned	O
relation	O
,	O
and	O
probes	O
the	O
other	O
relation	O
for	O
matches	O
with	O
the	O
current	O
hash	B-Algorithm
table	I-Algorithm
.	O
</s>
<s>
The	O
hybrid	O
hash	B-Algorithm
join	I-Algorithm
algorithm	O
is	O
a	O
combination	O
of	O
the	O
classical	O
hash	B-Algorithm
join	I-Algorithm
and	O
grace	O
hash	B-Algorithm
join	I-Algorithm
.	O
</s>
<s>
It	O
uses	O
minimal	O
amount	O
of	O
memory	O
for	O
partitioning	O
like	O
in	O
grace	O
hash	B-Algorithm
join	I-Algorithm
and	O
uses	O
the	O
remaining	O
memory	O
to	O
initialize	O
a	O
classical	O
hash	B-Algorithm
join	I-Algorithm
during	O
partitioning	O
phase	O
.	O
</s>
<s>
During	O
the	O
partitioning	O
phase	O
,	O
the	O
hybrid	O
hash	B-Algorithm
join	I-Algorithm
uses	O
the	O
available	O
memory	O
for	O
two	O
purposes	O
:	O
</s>
<s>
Because	O
partition	O
0	O
is	O
never	O
written	O
to	O
disk	O
,	O
hybrid	O
hash	B-Algorithm
join	I-Algorithm
typically	O
performs	O
fewer	O
I/O	O
operations	O
than	O
grace	O
hash	B-Algorithm
join	I-Algorithm
.	O
</s>
<s>
When	O
fits	O
nearly	O
fully	O
into	O
memory	O
hybrid	O
hash	B-Algorithm
join	I-Algorithm
has	O
a	O
similar	O
behavior	O
like	O
the	O
classical	O
hash	B-Algorithm
join	I-Algorithm
which	O
is	O
more	O
beneficial	O
.	O
</s>
<s>
Otherwise	O
hybrid	O
hash	B-Algorithm
join	I-Algorithm
imitates	O
grace	O
hash	B-Algorithm
join	I-Algorithm
.	O
</s>
<s>
Note	O
that	O
this	O
algorithm	O
is	O
memory-sensitive	O
,	O
because	O
there	O
are	O
two	O
competing	O
demands	O
for	O
memory	O
(	O
the	O
hash	B-Algorithm
table	I-Algorithm
for	O
partition	O
0	O
,	O
and	O
the	O
output	O
buffers	O
for	O
the	O
remaining	O
partitions	O
)	O
.	O
</s>
<s>
Choosing	O
too	O
large	O
a	O
hash	B-Algorithm
table	I-Algorithm
for	O
partition	O
0	O
might	O
cause	O
the	O
algorithm	O
to	O
recurse	O
because	O
one	O
of	O
the	O
non-zero	O
partitions	O
is	O
too	O
large	O
to	O
fit	O
into	O
memory	O
.	O
</s>
<s>
Hash	B-Algorithm
joins	I-Algorithm
can	O
also	O
be	O
evaluated	O
for	O
an	O
anti-join	O
predicate	B-Application
(	O
a	O
predicate	B-Application
selecting	O
values	O
from	O
one	O
table	O
when	O
no	O
related	O
values	O
are	O
found	O
in	O
the	O
other	O
)	O
.	O
</s>
<s>
Prepare	O
a	O
hash	B-Algorithm
table	I-Algorithm
for	O
the	O
NOT	O
IN	O
side	O
of	O
the	O
join	O
.	O
</s>
<s>
Scan	O
the	O
other	O
table	O
,	O
selecting	O
any	O
rows	O
where	O
the	O
join	O
attribute	O
hashes	O
to	O
an	O
empty	O
entry	O
in	O
the	O
hash	B-Algorithm
table	I-Algorithm
.	O
</s>
<s>
Prepare	O
a	O
hash	B-Algorithm
table	I-Algorithm
for	O
the	O
FROM	O
side	O
of	O
the	O
join	O
.	O
</s>
<s>
Scan	O
the	O
NOT	O
IN	O
table	O
,	O
removing	O
the	O
corresponding	O
records	O
from	O
the	O
hash	B-Algorithm
table	I-Algorithm
on	O
each	O
hash	O
hit	O
.	O
</s>
<s>
Return	O
everything	O
that	O
left	O
in	O
the	O
hash	B-Algorithm
table	I-Algorithm
.	O
</s>
<s>
Prepare	O
a	O
hash	B-Algorithm
table	I-Algorithm
for	O
the	O
IN	O
side	O
of	O
the	O
join	O
.	O
</s>
<s>
The	O
actual	O
records	O
from	O
the	O
hash	B-Algorithm
table	I-Algorithm
are	O
ignored	O
.	O
</s>
<s>
Prepare	O
a	O
hash	B-Algorithm
table	I-Algorithm
for	O
the	O
FROM	O
side	O
of	O
the	O
join	O
.	O
</s>
<s>
Scan	O
the	O
IN	O
table	O
,	O
returning	O
the	O
corresponding	O
records	O
from	O
the	O
hash	B-Algorithm
table	I-Algorithm
and	O
removing	O
them	O
.	O
</s>
<s>
With	O
this	O
algorithm	O
,	O
each	O
record	O
from	O
the	O
hash	B-Algorithm
table	I-Algorithm
(	O
that	O
is	O
,	O
FROM	O
table	O
)	O
can	O
only	O
be	O
returned	O
once	O
,	O
since	O
it	O
is	O
removed	O
after	O
being	O
returned	O
.	O
</s>
