Skip to main content

uniq

Introduced in: v1.1.0 Calculates the approximate number of different values of the argument. The function uses an adaptive sampling algorithm. For the calculation state, the function uses a sample of element hash values up to 65536. This algorithm is very accurate and very efficient on the CPU. When the query contains several of these functions, using uniq is almost as fast as using other aggregate functions.
This function calculates a hash for all parameters in the aggregate, then uses it in calculations. It uses an adaptive sampling algorithm. For the calculation state, the function uses a sample of element hash values up to 65536. This algorithm is very accurate and very efficient on the CPU. When the query contains several of these functions, using uniq is almost as fast as using other aggregate functions.The sample keeps 32-bit hashes, which cannot distinguish more than a few billion values, so in addition a small heavily-thinned-out sample of full 64-bit hashes is maintained (about one hash per 262144 values, bounded by 16384 hashes), and the estimate switches to it at around two billion. This keeps the estimate accurate (within about 1%) for cardinalities of hundreds of billions and more, where servers older than 26.8 returned garbage (issue #6078). States written by those servers carry no 64-bit sample, so merging with them keeps only the 32-bit precision for the merged-in values.
We recommend using this function over other variants in almost all scenarios.
Syntax
Arguments Returned value Returns a UInt64-type number representing the approximate number of different values. UInt64 Examples Example usage
Query
Response
Multiple arguments
Query
Response
See Also
Last modified on October 6, 2026