---
title: "Native random sampling in ClickHouse"
description: "ClickHouse's SAMPLE clause runs aggregations over a deterministic subset of rows selected by a hash-based sample key, which must be part of the primary key so granules above the hash threshold can be skipped."
url: "https://swe.quest/articles/native-random-sampling-in-clickhouse-b04e18e64b8c1545"
canonical: "https://clickhouse.com/blog/native-random-sampling"
source: "ClickHouse"
published: "2026-05-22"
difficulty: "medium"
kind: "How-to"
reading_minutes: 11
tags: ["analytics","query-engines","performance","sql"]
---

# Native random sampling in ClickHouse

A post on [ClickHouse](https://swe.quest/sources/clickhouse.md), published 2026-05-22, 11 min read, How-to. Original: [https://clickhouse.com/blog/native-random-sampling](https://clickhouse.com/blog/native-random-sampling)

## Summary

ClickHouse's SAMPLE clause runs aggregations over a deterministic subset of rows selected by a hash-based sample key, which must be part of the primary key so granules above the hash threshold can be skipped. On a 30M-row UK house price dataset, sipHash64(postcode1, postcode2) spreads rows evenly across buckets, while a low-cardinality column like county skews badly because Greater London holds 13% of sales. Sampling works by fraction or by minimum row count, and the \_sample\_factor virtual column scales sums and counts back to full-dataset estimates; avg needs no adjustment. A 10% sample cut a grouped analytical query from 687 ms to 141 ms by reading 384 of 3,719 granules, with sales estimates about 7–8% off, which is acceptable for exploratory work.

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)
- [Query engines](https://swe.quest/tags/query-engines.md) (Data & databases)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [SQL](https://swe.quest/tags/sql.md) (Languages)

## Prerequisites

- 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))
- primary key ordering

## Keywords

ClickHouse, MergeTree, SAMPLE BY, sipHash64, \_sample\_factor

## Related posts

- [ClickHouse Newsletter February 2023: A ‘Sample’ of What’s Happening in the Community](https://swe.quest/articles/clickhouse-newsletter-february-2023-a-sample-of-what-s-happening-in-the-9cf1bfd9f4573dfb.md): ClickHouse · 2023-02-15 · Medium · 9 min read · Analytics & OLAP, SQL, Performance engineering
  ClickHouse's SAMPLE BY clause trades exact answers for speed on very large tables, illustrated with a 425-billion-row Wikistat dataset.
- [How to take random samples from big PostgreSQL tables](https://swe.quest/articles/how-to-take-random-samples-from-big-postgresql-tables-9724e6f9679558c6.md): Render · 2024-06-13 · Medium · 10 min read · PostgreSQL, SQL, Performance engineering, Query engines
  Random sampling from billion-row PostgreSQL tables with ORDER BY random() scans every row and runs a top-N heapsort, taking over a minute.
- [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.
- [Generating Random Data in ClickHouse](https://swe.quest/articles/generating-random-data-in-clickhouse-e59cc8689ff303d4.md): ClickHouse · 2022-12-29 · Medium · 13 min read · Analytics & OLAP, SQL, Testing
  ClickHouse 22.10 added random functions for non-uniform distributions (randNormal, randBinomial, randExponential, randPoisson, randLogNormal, randBernoulli and others) alongside the existing uniform randCanonical and randUniform.
- [ClickHouse Newsletter November 2022: The purifying effects of obfuscation](https://swe.quest/articles/clickhouse-newsletter-november-2022-the-purifying-effects-of-obfuscation-ba5293e588a311a6.md): ClickHouse · 2022-11-10 · Medium · 8 min read · Analytics & OLAP, Testing, Privacy, Databases
  ClickHouse Cloud pricing now charges only for compute and storage, and ClickHouse 22.10 adds S3 backups (full and incremental), \`SET setting = DEFAULT\`, forced merging of old parts via \`min\_age\_to\_force\_merge\_seconds\`, a relaxed too-many-parts check for large partitions, new random-distribution functions and a preview Kafka Connect sink with exactly-once semantics.

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)
