ArticleslgStudy

computer science

Sort-merge join

Sort-merge join is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Sort-merge join rather than just read about it. In short: The sort-merge join (also known as merge join) is a join algorithm and is used in the implementation of a relational database management system. The basic problem of a join algorithm is to find, for each distinct value of the join attribute, the set of tuples in each relation which display that value.

Key takeaways

  • Sort-merge join belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Sort-merge join to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Sort-merge join from memory before moving on to harder problems.

Reference excerpt

The sort-merge join (also known as merge join) is a join algorithm and is used in the implementation of a relational database management system. The basic problem of a join algorithm is to find, for each distinct value of the join attribute, the set of tuples in each relation which display that value. The key idea of the sort-merge algorithm is to first sort the relations by the join attribute, so that interleaved linear scans will encounter these sets at the same time. The essential principle is implemented, for example, by the traditional Unix join command. In practice, the most expensive part of performing a sort-merge join is arranging for both inputs to the algorithm to be presented in sorted order. This can be achieved via an explicit sort operation (often an external sort), or by taking advantage of a pre-existing ordering in one or both of the join relations. The latter condition, called interesting order, can occur because an input to the join might be produced by an index scan of a tree-based index, another merge join, or some other plan operator that happens to produce output sorted on an appropriate key. Interesting orders need not be serendipitous: the optimizer may seek out this possibility and choose a plan that is suboptimal for a specific preceding operation if it yields an interesting order that one or more downstream nodes can exploit.

Complexity Let R {\displaystyle R} and S {\displaystyle S} be relations where | R | < | S | {\displaystyle |R|<|S|} . R {\displaystyle R} fits in P r {\displaystyle P_{r}} pages memory and S {\displaystyle S} fits in P s {\displaystyle P_{s}} pages memory. In the worst case, a sort-merge join will run in O ( P r + P s ) {\displaystyle O(P_{r}+P_{s})} I/O operations. In the case that R {\displaystyle R} and S {\displaystyle S} are not ordered the worst case time cost will contain additional terms of sorting time: O ( P r + P s + P r log ⁡ ( P r ) + P s log ⁡ ( P s ) ) {\displaystyle O(P_{r}+P_{s}+P_{r}\log(P_{r})+P_{s}\log(P_{s}))} , which equals O ( P r log ⁡ ( P r ) + P s log ⁡ ( P s ) ) {\displaystyle O(P_{r}\log(P_{r})+P_{s}\log(P_{s}))} (as linearithmic terms outweigh the linear terms, see Big O notation – Orders of common functions).

Pseudocode For simplicity, the algorithm is described in the case of an inner join of two relations left and right. Generalization to other join types is straightforward. The output of the algorithm will contain only rows contained in the left and right relation and duplicates form a Cartesian product.

Since the comparison logic is not the central aspect of this algorithm, it is hidden behind a generic comparator and can also consist of several comparison criteria (e.g. multiple columns). The compare function should return if a row is less(-1), equal(0) or bigger(1) than another row:

Note that a relation in terms of this pseudocode supports some basic operations:

Simple C# implementation Note that this implementation assumes the join attributes are unique, i.e., there is no need to output multiple tuples for a given value of the key.

See also Hash join Nested loop join

References

External links C# Implementations of Various Join Algorithms

Worked examples

Example 1 — a first encounter with Sort-merge join

Start with the simplest possible case. Write down what Sort-merge join claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Sort-merge join before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Sort-merge join ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Sort-merge join

In research
Sort-merge join appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Sort-merge join in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Sort-merge join is common in secondary-school and first-year university syllabi. It links to neighbouring topics Join algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Sort-merge join outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Sort-merge join in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Sort-merge join means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Sort-merge join out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Sort-merge join in simple terms?

The sort-merge join (also known as merge join) is a join algorithm and is used in the implementation of a relational database management system. The basic problem of a join algorithm is to find, for each distinct value of the join attribute, the set of tuples in each relation which display that val…

Why does Sort-merge join matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Sort-merge join?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Sort-merge join.

Tags

  • Join algorithms

Keep exploring