A topic in the Open Knowledge Graph — a free, open map of 15,495 topics and the order to learn them in.

Join Algorithms: Nested-Loop, Sort-Merge, and Hash Join

College Depth 97 in the knowledge graph ☐ I know this ☆ Set as goal
4topics build on this
532prerequisites beneath it
See this on the map →
Physical Storage: Pages, Records, and Heap FilesRelational Algebra+3 more→→Query Optimization
query-processing joins nested-loop-join hash-join sort-merge-join cost-model

Core Idea

A database can compute the same join in several physical ways, and their costs differ by orders of magnitude. Nested-loop joins compare each tuple of one input with the tuples of the other and work for any join condition. Sort-merge join sorts both inputs on the join attribute and merges them in one pass. Hash join partitions both inputs with a hash function so that matching tuples land in the same partition, then joins each partition in memory. The query optimizer estimates disk I/O for each method and chooses the cheapest for the query at hand.

How It's Best Learned

Use one running example with known sizes in blocks and tuples, and compute the block transfers for each algorithm by hand, following the cost formulas in Silberschatz, Korth and Sudarshan's chapter on query processing. Run `EXPLAIN` on a real database to see which join method the optimizer picks, then add or drop an index and watch the plan change. Emphasize which methods need an equality condition and which can handle any predicate.

Common Misconceptions

Explainer

SQL lets you say which rows you want joined. It does not say how to find them. That choice belongs to the database, and it matters, because tables are stored in disk blocks and reading blocks dominates the cost. Textbooks therefore count block transfers. Write b_r and b_s for the number of blocks in relations r and s, and n_r for the number of tuples in r.

The simplest method is the nested-loop join. For each tuple of the outer relation r, scan all of s and test the join condition on each pair. It works for any condition, even `r.a < s.b`, but in the worst case it reads s once per tuple of r: n_r x b_s + b_r transfers. The block nested-loop join improves this by reading s once per block of r instead: b_r x b_s + b_r in the worst case. With more memory, it holds several blocks of r at a time and scans s fewer times. If s has an index on the join attribute, an indexed nested-loop join replaces each scan of s with an index lookup, which is very effective when r is small.

For equality joins, two smarter methods avoid comparing most pairs. Sort-merge join sorts both relations on the join attribute, using external sorting if needed. It then walks through both sorted lists together, like merging in merge sort, and outputs matching groups. After sorting, the merge reads each relation once, b_r + b_s transfers. If the inputs are already sorted, for example through a clustered index, the method is very cheap.

Hash join uses a hash function h on the join attribute. In the partition phase, it reads both relations and writes each tuple into one of several partitions according to h. Tuples with equal keys always land in partitions with the same number. In the build-and-probe phase, it loads one partition of the smaller relation, the build input, into an in-memory hash table. It then reads the matching partition of the other relation and probes the table for each tuple. Ignoring partially filled blocks, this costs about 3(b_r + b_s) transfers: read both, write partitions, read partitions again.

No method wins every time. Nested loops handle any predicate and are good for small inputs. Index joins win when one side is small and the other is indexed. Hash join is often best for large unsorted equi-joins. Sort-merge shines when sorted order is already available. The query optimizer estimates sizes and picks the cheapest plan.

Practice Questions 4 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Combining Small Groups Within 5 → Number Bonds Within 5 → Number Bonds to 10 → Tens and Ones Place Value → Reading and Writing Three-Digit Numbers → Comparing Three-Digit Numbers → Comparing and Ordering Three-Digit Quantities → Comparing and Ordering Whole Numbers → Comparing Numbers 1 to 20 → Ordering Numbers to 20 → Number Line 0 to 20 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Opposites and Additive Inverses → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes → Objects and Instances → Methods and Attributes → Algorithm Design Basics → Asymptotic Notation: Big-O, Big-Omega, Big-Theta → Big-O Notation and Complexity Analysis → Time and Space Complexity → Binary Search → Divide and Conquer → Merge Sort → Join Algorithms: Nested-Loop, Sort-Merge, and Hash Join

Longest path: 98 steps · 532 total prerequisite topics

Prerequisites (5)

Leads To (1)