Skip to content
Computing Medium #joins#hash-join#merge-join

Hash, Merge, and Nested Loop Joins

Three join algorithms, three workload sweet spots. The optimizer picks per query.

A free, animated hash, merge, and nested loop joins you can read here or embed on any website, from Scrollchart.

Hash, Merge, and Nested Loop Joins

Hash, Merge, and Nested Loop JoinsThe query optimizer picks based on table size, sort order, and available memoryNested LoopO(N x M)For each outer rowScan entire inner tableEmit matching pairsBest: Small inner sideWorst: Large x Large30ms (1K x 1K)Hash JoinO(N + M)Build hash table (small)Probe with each large rowEmit matches from bucketBest: Large unsorted tablesWorst: Low memory (spills)4ms (1K x 1K)Merge JoinO(N log N + M log M)Sort both inputs on keyWalk pointers in lockstepAdvance the lesser keyBest: Pre-sorted / indexedWorst: Unsorted large tables6ms (1K x 1K)Optimizer cost model: estimates rows, selects cheapest plan. Force with JOIN hints only as last resort.0K2K4K6K8K10K0ms25ms50ms75ms100msRow count (each table)Cost (ms)Cost vs Row CountNestedHashMergeNL wins belowRule: Hash join for large unsorted tables. Merge join when both sides already sorted. Nested loop for index lookups on a small inner set.

Two tables joined three ways. Nested loop: best for small inner side. Hash: build a hash on smaller side, probe with the larger. Merge: requires sorted inputs, streams in lockstep. Cost vs row-count plotted.

Good for

  • Query plan analysis with EXPLAIN ANALYZE
  • Database performance tuning guides
  • Teaching query optimization in CS or data engineering courses

Source & accuracy

This hash, merge, and nested loop joins is an editorial illustration built to represent the concept accurately. Where it shows figures, they are typical or representative values chosen to make the relationship clear, not a single underlying dataset. The diagram and its explainer are reviewed and maintained centrally, and updated over time as understanding improves.

Nested loop, merge, and hash join strategies

A nested loop join iterates the outer table and for each row, scans the inner table for matches. It is slow (O(n*m) with n and m as table sizes) but requires no sorted data or extra memory. A merge join assumes both tables are sorted by the join key, then scans both in lockstep, advancing pointers to find matches in O(n + m). A hash join builds a hash table on the inner table, then probes it once per outer row, also O(n + m).

Hash joins are fastest for unsorted data; merge joins excel when data is already sorted or when joins are cascaded (multiple joins in one query). Nested loops are used only when one table is tiny or when specific row orderings must be preserved.

Query optimizer selection and memory considerations

The query optimizer estimates table sizes, selectivity, and available memory, then picks the best algorithm. Hash joins spill to disk if the hash table exceeds memory, degrading to slower I/O. Merge joins avoid this by streaming sorted data and requiring only a small buffer. In-memory databases favor hash joins; disk-based systems with limited memory may prefer merge joins.

For production queries, understanding the chosen algorithm is critical for tuning. Forcing an index (to enable merge join) or increasing memory (to prevent spill in hash join) can transform a slow query into a fast one. EXPLAIN output reveals the chosen algorithm; deviations from expectation suggest stale statistics or suboptimal index design.

Embed this diagram

Add this animated hash, merge, and nested loop joins to your own site. Copy one line of HTML, or use the embed builder for theme and sizing options.

Reference

What this is
A free, embeddable, animated hash, merge, and nested loop joins for any website.
Who uses it
Developer blogs, DevOps / SRE sites.
How to embed
Copy one line of HTML. No signup. No watermark. Works in WordPress, Webflow, Ghost, Substack, plain HTML.
File size
iframe embed, ~80 KB gzipped (loads on demand, does not block your page paint).
License
Free forever. Editorial explainer text included; updated centrally over time.

Embed format options

Copy the universal HTML snippet, the WordPress shortcode, or an iframe fallback - see the WordPress plugin page for details. Any format keeps the same Core Web Vitals profile and the same explainer text.

Embed snippet
<div data-scrollchart="join-algorithms" data-scrollchart-v="1"></div>
<script src="https://scrollchart.com/embed.js" async></script>

Frequently asked questions

Where can I get a free animated "Hash, Merge, and Nested Loop Joins" for my website?
Scrollchart provides "Hash, Merge, and Nested Loop Joins" as a free, embeddable animated diagram you can add to any website with one line of HTML. No signup is required and there is no watermark. The diagram and its explainer text are served from scrollchart.com, so the embed stays current without any maintenance on your end.
How do I embed a hash, merge, and nested loop joins in a developer or tech blog?
Copy the one-line snippet from the Scrollchart diagram page and paste it into your post HTML. It works in any static site generator, CMS, or hand-coded HTML page. The embed is a thin loader, not an iframe, so the content is fully in your DOM.