ClickHouse Functions/Aggregate Functions

uniq

Approximate distinct count — fast cardinality estimation for DAU and unique visitors.

Signature

uniq(column): UInt64

Returns

UInt64

What it does

Returns an approximate count of distinct values using the HyperLogLog algorithm. Typically 2–5% error rate. Up to 10× faster than COUNT(DISTINCT) on large datasets.

uniq() is ClickHouse's recommended function for high-cardinality distinct counts. It uses a 2^17-cell HyperLogLog sketch (512 KB per aggregate state) giving roughly 2% error at the 99th percentile. For lower error rates use uniqExact() (exact, slower) or uniqHLL12() (configurable precision). For AggregatingMergeTree materialised views, use uniqState() and uniqMerge().

Notes

  • Error rate ≈ 2–5% for most cardinalities. Use uniqExact() when you need 100% accuracy.
  • For pre-aggregated data in AggregatingMergeTree, use uniqState() on insert and uniqMerge() on read.
  • uniqCombined() uses less memory with similar accuracy — good for high-fan-out GROUP BY.

Example SQL

uniq in ClickHouse SQL

uniq-example.sql

TypeScript with hypequery

Use uniq in a typed TypeScript query

hypequery gives you a type-safe query builder for ClickHouse. The generated schema maps your ClickHouse columns to TypeScript types, and raw SQL expressions let you incorporate functions like uniq when you need them inside a builder query.

daily-unique-users.ts

Common questions

What developers search for with uniq

ClickHouse distinct users fast query

uniq(user_id) is the standard way to count unique users in ClickHouse. It is 5–10× faster than COUNT(DISTINCT user_id) on tens-of-millions of rows.

ClickHouse HyperLogLog DAU

uniq() uses HyperLogLog internally. For materialised DAU views, store the sketch with uniqState() and merge with uniqMerge() at query time.

FAQ

Frequently asked questions about uniq

How accurate is uniq() in ClickHouse?

Typically within 2–5% of the exact count. If you need exact counts, use uniqExact() — but it is significantly slower and uses more memory.

What is the difference between uniq() and uniqCombined()?

uniqCombined() uses an array for small cardinalities and switches to HyperLogLog for large ones — lower memory, similar accuracy. Preferred for high-cardinality GROUP BY.

Related functions

Functions used alongside uniq

Next step

Use uniq in a type-safe TypeScript query

hypequery generates TypeScript types from your ClickHouse schema. Use uniq alongside the builder, and reach for raw SQL expressions when the function is not exposed as a dedicated helper.