---
title: "Native random sampling in ClickHouse"
description: "ClickHouse native random sampling enables aggregate queries to execute against a representative fraction of data when querying full datasets is too slow. To maintain sampling accuracy, tables require a sample key derived from high-cardinality columns using hash functions like sipHash64 to ensure an even distribution. The sample key must be included in the primary key ORDER BY expression, preferably at the front to let ClickHouse quickly skip index granules. Queries can sample data deterministically by specifying a fractional ratio or a minimum row threshold, scaling aggregate counts using the virtual column _sample_factor. In benchmark tests on a 30-million-row dataset, sampling 10% of records reduced query latency by approximately 80% while retaining approximate metrics suitable for exploratory analysis."
---

# Native random sampling in ClickHouse

[Clickhouse](https://yomu.fyi/company/clickhouse) · Mark Needham · May 22, 2026

**Type:** Tutorial

## Summary

ClickHouse native random sampling enables aggregate queries to execute against a representative fraction of data when querying full datasets is too slow. To maintain sampling accuracy, tables require a sample key derived from high-cardinality columns using hash functions like sipHash64 to ensure an even distribution. The sample key must be included in the primary key ORDER BY expression, preferably at the front to let ClickHouse quickly skip index granules. Queries can sample data deterministically by specifying a fractional ratio or a minimum row threshold, scaling aggregate counts using the virtual column \_sample\_factor. In benchmark tests on a 30-million-row dataset, sampling 10% of records reduced query latency by approximately 80% while retaining approximate metrics suitable for exploratory analysis.

## Context

Running aggregate queries across entire large datasets in ClickHouse can be too slow for exploratory data analysis.

## Approach / What changed

Configure native random sampling in ClickHouse by defining a deterministic sample key with sipHash64 over high-cardinality columns at the front of the ORDER BY clause, executing queries with the SAMPLE clause by fraction or row count, and scaling results using \_sample\_factor.

## Takeaways

- Sample keys must be high-cardinality expressions with uniform distributions, such as sipHash64 on detailed identifiers; low-cardinality fields like county skew bucket distributions.
- The SAMPLE BY expression must be part of the table primary key ORDER BY clause, and placing it first allows ClickHouse to skip unneeded index granules.
- The SAMPLE clause supports both fractional sampling and minimum row count thresholds, producing deterministic query results that scale counts with the \_sample\_factor virtual column.

**Tags:** [Performance](https://yomu.fyi/topic/performance), [Scalability](https://yomu.fyi/topic/scalability)

- Source: [Clickhouse](https://clickhouse.com/blog/native-random-sampling)
- Source URL: https://clickhouse.com/blog/native-random-sampling
- Ingested by Yomu: 2026-08-28T01:26:24.877Z

[Read original post](https://clickhouse.com/blog/native-random-sampling)
