10 / 19 · Storage and Execution
Join algorithms
Compute the same catalogue join with loops, a hash table or ordered streams. Account for input size, memory and repeated keys.
The report needs the catalogue category beside every order line. The inner join defines the pairs. The engine can find those pairs by checking rows one by one, looking up a hash table, or advancing through ordered inputs. The output remains six rows.
Repeat a lookup for each outer row#
Use the six order lines and four catalogue entries from the SQL example. A nested-loop join reads one outer row, finds all its matching inner rows, then advances the outer input. If the inner side scans the whole catalogue for each line, it evaluates candidate pairs. Only six pass equality.
The inner step can instead look up catalogue.product by its index. That gives six probes, each with at most one match because the catalogue key is unique. A probe can touch several pages and may need a row fetch. Six probes and 24 comparisons count different operations, so the numbers alone do not give a timing comparison. Loops with indexed inner access are useful when few outer rows reach the join. SQLite implements joins with nested scans and shows indexed searches or scans for individual loops. (PostgreSQL 18, §14.1; SQLite, §1.1)
Build a temporary lookup table#
A hash join for equality first builds a hash table from one input, then probes it using the other. Here we insert the four catalogue rows using product as the hash key. Then each of the six order lines probes by its product. A candidate with the same hash still needs an equality check. If several build rows have the same key, they all remain eligible for the probe.
With good hash distribution and enough memory, let and be the numbers of probe and build rows, and let be the output row count. Expected work is . Memory grows with the build rows and their payload. This example has four build rows, six probes and six outputs. A larger build input may require partitioning both sides and writing temporary data. Skew can leave a partition too large even when the average partition is small. (PostgreSQL 18, §14.1; Microsoft, Hash joins)
Build once and probe six times
Advance through ordered keys#
A merge join can use inputs ordered by the join key. Compare the current keys. If one is smaller, advance that input, since its key cannot match the current or later keys on the other side. When both keys agree, match their runs of equal keys and continue. This example emits one cocoa pair, two coffee pairs and three tea pairs. The right input’s mint key falls between coffee and tea, and the left input has no mint to pair with it.
If both sides have repeats, the join emits the product of their run sizes. Three tea lines and two tea offers still require six output pairs. A merge implementation needs a way to revisit or keep one matching run. With ordered inputs, the work grows with rows read and pairs emitted. When the engine must sort the inputs first, the cost also includes those sorts, which can spill to temporary storage. An index can supply a useful input order. (PostgreSQL 18, §14.1; Microsoft, Merge joins)
INNER JOIN specifies which rows match. Nested Loop, Hash Join and Merge Join are physical node types that can implement it in PostgreSQL. The decision depends on cardinalities, available indexes, input orders, row widths and the cost of producing all matching pairs.
Next#
A planner needs to know whether it is choosing six lookups or six million. Statistics and query optimization derive row estimates from the data and compare them with an executed plan.
References#
- PostgreSQL Global Development Group. Using EXPLAIN, §14.1. PostgreSQL 18 documentation with nested-loop, hash and merge plan examples.
- SQLite Project. EXPLAIN QUERY PLAN, §1.1. Shows the nested scans and index searches used for SQLite joins.
- Microsoft. Joins. SQL Server documentation on nested loops, merge, hash build and probe, and hash spill.