---
title: "How to Scale K-Means Clustering with just ClickHouse SQL"
description: "K-Means clustering can be implemented entirely in ClickHouse SQL by computing centroids incrementally with avgForEachState aggregate states stored in an AggregatingMergeTree table, fed by a Materialized View over a Null-engine table."
url: "https://swe.quest/articles/how-to-scale-k-means-clustering-with-just-clickhouse-sql-72a89ac76ba9cbb9"
canonical: "https://clickhouse.com/blog/kmeans-clustering-with-clickhouse"
source: "ClickHouse"
published: "2024-04-11"
difficulty: "medium"
kind: "How-to"
reading_minutes: 21
tags: ["analytics","machine-learning","sql","performance"]
---

# How to Scale K-Means Clustering with just ClickHouse SQL

A post on [ClickHouse](https://swe.quest/sources/clickhouse.md), published 2024-04-11, 21 min read, How-to. Original: [https://clickhouse.com/blog/kmeans-clustering-with-clickhouse](https://clickhouse.com/blog/kmeans-clustering-with-clickhouse)

## Summary

K-Means clustering can be implemented entirely in ClickHouse SQL by computing centroids incrementally with avgForEachState aggregate states stored in an AggregatingMergeTree table, fed by a Materialized View over a Null-engine table. Each iteration is a single INSERT INTO SELECT that assigns every point to its nearest previous centroid via L2Distance, so memory stays bounded and the work scales across a cluster. Querying pre-aggregated centroids is over 1700x faster than a raw GROUP BY on 100M rows. Clustering 170M NYC taxi rides takes under 3 minutes, versus over 100 minutes and 90GB of RAM with scikit-learn on the same resources.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Analytics & OLAP](https://swe.quest/tags/analytics.md) (Data & databases)
- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)
- [SQL](https://swe.quest/tags/sql.md) (Languages)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)

## Prerequisites

- K-Means clustering basics
- SQL aggregation ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- ClickHouse MergeTree tables ([easy posts on Analytics & OLAP](https://swe.quest/tags/analytics.md?difficulty=easy))

## Keywords

ClickHouse, K-Means, AggregatingMergeTree, Materialized Views, avgForEach, scikit-learn, NYC taxi dataset

## Related posts

- [How we scaled raw GROUP BY to 100 B+ rows in under a second](https://swe.quest/articles/how-we-scaled-raw-group-by-to-100-b-rows-in-under-a-second-c36aee54a6541514.md): ClickHouse · 2025-09-29 · Medium · 23 min read · Query engines, Analytics & OLAP, Performance engineering, Distributed databases, Scaling
  ClickHouse Cloud's parallel replicas feature spreads a single GROUP BY query across every core of every node in a service, with no pre-aggregation or data reshuffling.
- [Using Materialized Views in ClickHouse](https://swe.quest/articles/using-materialized-views-in-clickhouse-f547ca7b5675b5e9.md): ClickHouse · 2023-01-19 · Medium · 13 min read · Analytics & OLAP, SQL, Performance engineering, Data pipelines
  ClickHouse materialized views act as insert-time triggers that write the result of a SELECT into a target table, letting expensive aggregations be precomputed.
- [How Klaviyo uses a 192-node ClickHouse cluster for real-time segmentation and personalization](https://swe.quest/articles/how-klaviyo-uses-a-192-node-clickhouse-cluster-for-real-time-segmentation-and-ba32bd092d16f47c.md): ClickHouse · 2025-08-05 · Medium · 5 min read · Analytics & OLAP, Scaling, System design, Distributed databases
  Klaviyo replaced a Python-orchestrated segmentation engine built on MySQL and a large Cassandra cluster of pre-aggregated counters with a 192-node on-prem ClickHouse cluster.
- [ClickHouse and The One Billion Row Challenge](https://swe.quest/articles/clickhouse-and-the-one-billion-row-challenge-a492ebece6e75794.md): ClickHouse · 2024-01-23 · Medium · 8 min read · Analytics & OLAP, SQL, Performance engineering
  ClickHouse Local solves the One Billion Row Challenge (min/mean/max temperature per station from a 13GB text file) in about 20 seconds on an 8-core Hetzner VM, versus nearly 3 minutes for the Java baseline.
- [Chaining Materialized Views in ClickHouse](https://swe.quest/articles/chaining-materialized-views-in-clickhouse-79c630686e93ebe9.md): ClickHouse · 2024-04-16 · Medium · 8 min read · Analytics & OLAP, Stream processing, SQL
  ClickHouse materialized views can be chained so that one view reads from a table populated by another, rather than having several views all read from the same Kafka engine table.

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)
