Generating Random Data in ClickHouse
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. Combined with numbers(), these functions can populate MergeTree tables with realistic test data: exponentially distributed purchase totals, Poisson-distributed event timestamps peaking at midday, binomially shaped CPU metrics with noise, multi-modal seasonal peaks, weighted enum values and random strings. Use floor() rather than round() to get uniformly distributed integers, because rounding under-samples the range edges.
Related posts
- Native random sampling in ClickHouse
ClickHouse mediumAdept
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.
- ClickHouse Newsletter November 2022: The purifying effects of obfuscation
ClickHouse mediumAdept
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. The featured query shows how `clickhouse-obfuscator` turns a real dataset, demonstrated on 26.7M UK property sales in Parquet, into synthetic data that keeps row count, cardinality and value distributions close to the original. That matters for realistic GROUP BY memory and performance testing, but the output is neither encrypted nor hashed and is not automatically safe to share. Also covered: upcoming meetups and webinars, plus links to migration stories and tuning posts.
- Working with Time Series Data in ClickHouse
ClickHouse mediumAdept
ClickHouse handles time-series workloads with Date, DateTime and DateTime64 types (up to nanosecond precision, with per-column timezones) plus SQL functions for bucketing by period. toStartOfHour and toStartOfInterval group by fixed or custom intervals, WITH FILL fills empty buckets, dateDiff builds offset rolling windows, and bar() gives quick inline visuals. Gauges can be filled forward with any() plus INTERPOLATE, and histogram() builds adaptive histograms. Examples run against a 1-billion-row Wikipedia pageviews table, where full scans complete in about 0.26 seconds.
- Decorating a Christmas Tree With the Help Of Flaky Tests
ClickHouse mediumAdept
ClickHouse treats flaky tests as bugs to investigate rather than noise to mute or retry, and deliberately makes tests flakier to surface hidden issues. Its practices include randomizing the environment on every run, running new tests 100 times before merge, using new tests as a fuzzing corpus, injecting random sleeps and core switches around mutex operations, and running in parallel on slow machines. Results of all test runs, nearly 1.8 billion records and 5–9 million runs per day, are stored in ClickHouse itself. A 561-day by 3772-test pass/fail pixel map of the stateless functional tests, rendered by JavaScript querying ClickHouse directly, forms a 'Christmas tree' where horizontal red lines mark flaky tests and vertical red lines mark days master was broken.
- How to take random samples from big PostgreSQL tables
Render mediumAdept
Random sampling from billion-row PostgreSQL tables with ORDER BY random() scans every row and runs a top-N heapsort, taking over a minute. A WHERE random() < p filter (Bernoulli sampling) drops the sort and halves runtime, but it still reads every heap page. TABLESAMPLE SYSTEM picks whole 8kB heap blocks and returns about 10k rows in roughly 50ms, at the cost of a nondeterministic result count and correlated rows within each block. A 10k-trial simulation showed result sizes and runtimes are normally distributed with modest row overlap. For joins, sample one side (the "many" side) before joining, and never sample both tables independently.