tezvyn:

Sort-Merge Join: The 'Line Up and Walk' Join

AI-drafted, machine-checkedSource: Wikipedia: Sort-merge joinintermediate

A sort-merge join is like merging two sorted lines of people. It's efficient when tables are already sorted on the join key or memory is tight. The footgun: if data isn't pre-sorted, the initial sort can make it slower than other join methods.

WHY IT EXISTS Databases need to combine data from different tables, a process called a join. The simplest method, a nested loop join, is brutally slow for large tables. We need algorithms that can efficiently join large datasets, especially ones that don't fit into memory. The sort-merge join provides a robust method that works well for large inputs and has predictable performance.

THE MENTAL MODEL Imagine you have two separate, long lines of people, each sorted alphabetically by last name. Your task is to find all pairs of people, one from each line, who have the same last name. Instead of taking the first person from line A and checking everyone in line B, you can be smarter. You put a pointer at the start of each line. If the names match, you've found a pair. If they don't, you just advance the pointer for the name that comes first alphabetically. You only ever move forward, never backward. This is the essence of a sort-merge join.

HOW IT WORKS The algorithm has two phases: SORT PHASE: If the two tables are not already sorted by the join key, the database sorts them. This is often the most expensive part of the operation. If the data is too large for memory, the database uses an external sort algorithm. MERGE PHASE: The database iterates through the two sorted tables simultaneously, much like merging two sorted lists. It uses pointers for each table. If the keys at the pointers match, it outputs the joined row and advances a pointer. If the keys don't match, it advances the pointer of the table with the smaller key, knowing the other key has no chance of matching the current row. This continues until one table is exhausted.

WHEN TO USE IT This join method shines in a few scenarios. First, when the data is already sorted on the join keys, perhaps by a clustered index or an index-only scan. This allows the database to skip the expensive sort phase entirely. Second, when the final result set is required to be sorted by the join key, as sort-merge produces this ordering for free. Third, it's a good choice when memory is constrained, as it doesn't require holding a large hash table in memory like a hash join does.

WHEN NOT TO USE IT Sort-merge is often a poor choice for unsorted data when one table is small enough to fit in memory. In that case, a hash join is almost always faster because building one hash table and probing it is cheaper than sorting two large tables. It's also not suitable for non-equi joins (e.g., joining on A.id > B.id), where the simple 'advance the smaller key' logic doesn't apply.

ONE CANONICAL EXAMPLE A query to join a large orders table with a large line_items table on order_id. If both tables are already sorted by order_id (a common scenario), the database can perform a very fast merge join. If not, the query optimizer might still choose sort-merge if it estimates the cost of sorting both is still better than other strategies, especially if memory is low.

Read the original → en.wikipedia.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.