---
title: "ClickHouse Joins Under the Hood - Full Sorting Merge Join, Partial Merge Join - MergingSortedTransform"
description: "ClickHouse offers two external-sort-based join algorithms that are not memory bound."
url: "https://swe.quest/articles/clickhouse-joins-under-the-hood-full-sorting-merge-join-partial-merge-join-1ae27b9bc393d8f0"
canonical: "https://clickhouse.com/blog/clickhouse-fully-supports-joins-full-sort-partial-merge-part3"
source: "ClickHouse"
published: "2023-05-24"
difficulty: "hard"
kind: "Deep dive"
reading_minutes: 32
tags: ["query-engines","analytics","databases","performance"]
---

# ClickHouse Joins Under the Hood - Full Sorting Merge Join, Partial Merge Join - MergingSortedTransform

A post on [ClickHouse](https://swe.quest/sources/clickhouse.md), published 2023-05-24, 32 min read, Deep dive. Original: [https://clickhouse.com/blog/clickhouse-fully-supports-joins-full-sort-partial-merge-part3](https://clickhouse.com/blog/clickhouse-fully-supports-joins-full-sort-partial-merge-part3)

## Summary

ClickHouse offers two external-sort-based join algorithms that are not memory bound. The full sorting merge join is a classical sort-merge join built into the query pipeline. It can pre-filter each table by the other's join keys and skip sorting entirely when a table's physical row order matches the join key. In those cases it can rival hash joins while using far less memory. The partial merge join minimizes memory for large joins by externally sorting the right table with on-disk index structures and sorting the left table block by block. Query pipeline graphs, trace logs, query\_log metrics and settings such as max\_bytes\_before\_external\_sort and max\_rows\_in\_set\_to\_optimize\_join show how spilling and the optimizations affect runtime and memory.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Query engines](https://swe.quest/tags/query-engines.md) (Data & databases)
- [Analytics & OLAP](https://swe.quest/tags/analytics.md) (Data & databases)
- [Databases](https://swe.quest/tags/databases.md) (Data & databases)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)

## Prerequisites

- SQL joins ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- sort-merge join basics
- ClickHouse query pipeline ([easy posts on Analytics & OLAP](https://swe.quest/tags/analytics.md?difficulty=easy))

## Keywords

ClickHouse, full sorting merge join, partial merge join, MergingSortedTransform, external sorting, ClickHouse Cloud

## Related posts

- [Choosing the Right Join Algorithm](https://swe.quest/articles/choosing-the-right-join-algorithm-3a2fc89a0d23b80c.md): ClickHouse · 2023-06-27 · Medium · 13 min read · Query engines, Analytics & OLAP, Performance engineering, SQL
  ClickHouse offers six join algorithms (direct, hash, parallel hash, grace hash, full sorting merge, partial merge), and the right choice depends on speed, memory and supported join types.
- [ClickHouse Joins Under the Hood - Hash Join, Parallel Hash Join, Grace Hash Join](https://swe.quest/articles/clickhouse-joins-under-the-hood-hash-join-parallel-hash-join-grace-hash-join-eba0126b891cf1aa.md): ClickHouse · 2023-04-20 · Medium · 22 min read · Query engines, Analytics & OLAP, Performance engineering, SQL
  ClickHouse implements three in-memory hash-table-based join algorithms within its parallel query pipeline.
- [HIFI’s migration from BigQuery to ClickHouse](https://swe.quest/articles/hifi-s-migration-from-bigquery-to-clickhouse-5318550bbb72f66e.md): ClickHouse · 2023-01-09 · Medium · 8 min read · Analytics & OLAP, SQL, Databases, JavaScript
  HIFI moved music royalty analytics from BigQuery to ClickHouse because BigQuery's per-query pricing discouraged exploratory use and slot reservations didn't fit a startup's shifting workloads.
- [ClickHouse Joins Under the Hood - Direct Join](https://swe.quest/articles/clickhouse-joins-under-the-hood-direct-join-4c5bfc57b28f012b.md): ClickHouse · 2023-06-07 · Medium · 15 min read · Query engines, Analytics & OLAP, Performance engineering, SQL
  ClickHouse's direct join is its fastest join algorithm. It applies when the right-hand table sits on storage that supports low-latency key-value lookups: the Join engine, EmbeddedRocksDB or a Dictionary.
- [ClickHouse Cloud: Fast, Updatable Lookups with the Join Table Engine](https://swe.quest/articles/clickhouse-cloud-fast-updatable-lookups-with-the-join-table-engine-b50f6e80ec781fcd.md): ClickHouse · 2026-05-12 · Medium · 6 min read · Analytics & OLAP, Databases, SQL, Cloud platforms
  ClickHouse's Join table engine keeps a precomputed in-memory hash map for a fixed join strictness, type and key.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
