Post · 6 min read· pp. 71–74

Merge Join is Weird

Since I’ve made myself a pariah at work over this, I figured I should explain in more detail what I mean.

I’m going to start with a very brief explanation of what I mean by “join.” An “inner join” (henceforce “join”) is the most fundamental query primitive in relational databases. It’s not an exaggeration to say that SQL derives most of its benefits from the fact that it allows users to write joins and have them executed relatively fast.

A “relation”, or “table” (depending on whether you’re a nerd or a jock) is a set or multiset (again, depending on whether you’re a nerd or a jock) of “tuples” or “rows” (again, …).

A “join” of two relations AA and BB on a predicate pp (written A⋈pBA \bowtie_p B) is a new relation whose rows are the concatenations of each pair of rows in AA and BB that satisfy pp.

Symbolically:

A⋈pB=σp(A×B)={ab ∣ a∈A,b∈B,p(ab)}A \bowtie_p B = \sigma_p(A \times B) = \left\{ ab \:|\: a \in A, b \in B, p(ab) \right\}

Where ×\times is the “cross product” operator that just gives you every pair of rows, and σp\sigma_p is the “select” or “filter” operator.

This definition has some useful properties.

Commutativity (note that we don’t care about the ordinalities of the various n-tuples, usually we just care about the names of their entries):

A⋈B=B⋈AA \bowtie B = B \bowtie A

Associativity (don’t worry about fiddling around with the predicates here, it works):

A⋈(B⋈C)=(A⋈B)⋈CA \bowtie (B \bowtie C) = (A \bowtie B) \bowtie C

And less often talked about, distributivity:

A⋈(B+C)=A⋈B+A⋈CA \bowtie (B + C) = A \bowtie B + A \bowtie C

(where ++ is the union operator where you smash two tables together). It doesn’t look like it, but this one is actually extra spicy and justifies a lot of non-trivial algorithms.

Anyone with a cursory knowledge of query processing will tell you that the three musketeers of binary join algorithms are the hash join, nested loops join, and merge join. But I’m going to argue that there’s really only one join algorithm: the one that encompasses hash join and nested loop join (and some other cool stuff). And then also merge join happens to exist for some reason.

So what’s the one true join algorithm? Well, the definition suggests an algorithm:

text
for each row a in A:
  for each row b in B:
    if p(ab):
      emit(ab)

A cute way of interpreting this algorithm is that

text
if p(ab):
  emit(ab)

is a join algorithm that only works on relations when each has a single row. We then rewrite AA to a1+a2+…a_1 + a_2 + \ldots and BB to (b_1 + b_2 + \ldots) (where each aia_i and bjb_j are a single row from AA and BB respectively).

Then A⋈BA \bowtie B is (a1+a2+…)⋈(b1+b2+…)(a_1 + a_2 + \ldots) \bowtie (b_1 + b_2 + \ldots) and by the distributive property,

A⋈B=a1⋈b1+a1⋈b2+…=∑a∈A,b∈Ba⋈bA \bowtie B = a_1 \bowtie b_1 + a_1 \bowtie b_2 + \ldots = \sum_{a \in A, b \in B} a \bowtie b
and we’ve successfully bootstrapped a real join algorithm from our baby one that only works on singletons.

This is the “nested loops join” and it can be mildly generalized by replacing the inner loop with a lookup into an index or something (I’ve heard this variation called “lookup join”).

We can pull this trick to derive hash join too. If pp is an equality predicate, say A.x=B.yA.x = B.y, and VV is the domain of A.xA.x and B.yB.y, partition AA as

A=Av1+Av2+…=∑v∈VAvA = A_{v_1} + A_{v_2} + \ldots = \sum_{v \in V}A_v
where each AvA_{v} contains all the tuples in AA having x=vx = v. Then we do the same for BB:
B=Bv1+Bv2+…=∑v∈VBvB = B_{v_1} + B_{v_2} + \ldots = \sum_{v \in V}B_v
with each BvB_{v} contains all the tuples in BB having y=vy = v.

Then rewrite:

A⋈x=yB=(∑v∈VAv)⋈x=y(∑v∈VBv)A \bowtie_{x=y} B = \left(\sum_{v \in V}A_v\right) \bowtie_{x = y} \left(\sum_{v \in V}B_v\right)

use the distributive property:

∑vx∈V∑vy∈VAvx⋈x=yBvy\sum_{v_x \in V} \sum_{v_y \in V} A_{v_x} \bowtie_{x=y} B_{v_y}

iterations of this sum where v1≠v2v_1 \not= v_2 are just empty, and ones where they are equal don’t need to be filtered, so

∑v∈VAv×Bv\sum_{v \in V} A_{v} \times B_{v}

Which pretty directly gives the algorithm for hash join:

text
m := {}
for a in A:
  m[a.x].append(a)
for b in B:
  for a in m[b.y]:
    emit(ab)

This strategy of exploiting distributivity can get us even more esoteric join algorithms.

Consider a setup where a table AA is sharded among a number of machines and we’d like to join it with a small table BB. Naively, to join these two tables we’d have to bring them both to a single machine and use a traditional algorithm, but distributivity justifies the following algorithm: send each machine that has rows from AA its own copy of BB. Have it perform the join locally and then emit the results to be collected elsewhere (this is often called “broadcast join”).

Grace hash join is also easily justified by distributivity.

I’m not going to go into the even more esoteric modern “worst-case-optimal” join algorithms, but we can do basically the same thing to justify their correctness (with some notable exceptions).

So where does merge join fit into all this?

As a refresher, merge join works in the following way: we have an equality condition, A.x=B.yA.x = B.y, and we have AA stored ordered on xx and BB stored ordered on yy. We can then iterate the two tables in lockstep taking the cross product between contiguous chunks of the tables where the equality columns match.

None of these strategies (that, IMO, are very natural and follow pretty directly from the definitions) really work to justify it. I think this alone suggests that there’s something unique going on here.

Look at all the concepts we had to add to our vocabulary in order to even define this! We now have a notion of how our relations are “stored”, iteration order is important, even “ordering” at all was nowhere to be seen before this. To me, exploiting the structure of our predicate feels like an optimization in hash join, here it’s central to the structure of the algorithm.

You could argue that merge sort is very natural and merge join directly descends from it, but I think that’s kind of suspect too: in merge sort, ordering is inherent to the problem. The total order you’ve defined on the things you’re sorting is an input, and without it not only does the algorithm not make sense, the problem doesn’t either. This isn’t true of merge join, nowhere in the definition of the join do we define “order” over the things we’re joining on, it’s purely an implementation detail.

It’s tempting to attribute the effectiveness here as “ordering” functionally acting as a “partitioning,” but that doesn’t really work. Consider that the two relations being joined need to “agree” on the total order they’re using for the algorithm to work. It feels to me as though there’s some kind of implicit communication going on between the two tables being joined in that they both decide ahead of time what “ordered” means to them. Also note that any total ordering suffices, but generally we’re limited to the “natural” ordering of a given data type.

Anyway, thanks for reading my email Larry. I trust merge join will be removed from the 2020 release of Oracle.