hyperloglog() 和 approx_count_distinct() 函数
工具包TimescaleDB 工具包函数在 Timescale 社区版下可用。它们自动包含在 Timescale 中,但对于自托管的 TimescaleDB 必须单独安装。点击了解更多。Timescale Cloud:性能、规模、企业级
自托管产品
MST
介绍
估算数据集中的不同值数量。这也称为基数估算。对于大型数据集和高基数(许多不同值)的数据集,这种方法在 CPU 和内存方面都比使用 count(DISTINCT)
进行精确计数效率更高。
该估算使用 hyperloglog++
算法。如果您不确定如何为
hyperloglog
设置参数,可以尝试使用 approx_count_distinct
聚合,它设置了一些合理的默认值。
聚合
- hyperloglog
- 将数据聚合到 hyperloglog 中以进行近似计数
备用聚合
- approx_count_distinct
- 将数据聚合到 hyperloglog 中以进行近似计数,无需指定桶的数量
访问器
- distinct_count
- 从 hyperloglog 估算不同值的数量
- stderror
- 估算 hyperloglog 的相对标准误差
汇总
- rollup
- 汇总多个 hyperloglog
hyperloglog(buckets INTEGER,value AnyElement) RETURNS Hyperloglog
这是使用 hyperloglog
算法估算近似不同值数量的第一步。使用 hyperloglog
从您的原始数据创建中间聚合。然后,此中间形式可由此组中的一个或多个访问器用于计算最终结果。
可选地,在应用访问器之前,可以使用 rollup()
组合多个此类中间聚合对象。
如果您不确定 buckets
的值应设置多少,可以尝试使用备用聚合函数 approx_count_distinct()
。approx_count_distinct
也会创建一个 hyperloglog
,但它设置了一个应适用于许多用例的默认桶值。
必需参数
名称 | 类型 | 描述 |
---|---|---|
buckets | INTEGER | hyperloglog 中的桶数量。增加桶数量可提高准确性,但会增加内存使用。该值向上舍入到最接近的 2 的幂,且必须在 2^4 (16) 和 2^18 之间。如果真实基数很高,设置小于 2^10 (1,024) 的值可能会导致准确性较差,不建议这样做。如果不确定,可以从 8,192 (2^13) 开始实验,其近似错误率为 1.15%。 |
value | AnyElement | 包含要计数的元素的列。该类型必须具有扩展的 64 位哈希函数。 |
返回
列 | 类型 | 描述 |
---|---|---|
hyperloglog | Hyperloglog | 一个 hyperloglog 对象,可以传递给其他 hyperloglog API 以进行汇总和最终计算 |
示例
给定一个名为 samples
的表,其中包含一个名为 weights
的列,返回 weights
列的 hyperloglog
SELECT hyperloglog(32768, weights) FROM samples;
使用相同的数据,从聚合中构建一个视图,您可以将其传递给其他 hyperloglog
函数
CREATE VIEW hll AS SELECT hyperloglog(32768, data) FROM samples;
approx_count_distinct(value AnyElement) RETURNS Hyperloglog
这是近似不同值数量的另一个第一步。它通过使用一些合理的默认参数来创建 hyperloglog
,从而提供了一些额外的便利。
使用 approx_count_distinct
从您的原始数据创建中间聚合。然后,此中间形式可由此组中的一个或多个访问器用于计算最终结果。
可选地,在应用访问器之前,可以使用 rollup()
组合多个此类中间聚合对象。
必需参数
名称 | 类型 | 描述 |
---|---|---|
value | AnyElement | 包含要计数的元素的列。该类型必须具有扩展的 64 位哈希函数。 |
返回
列 | 类型 | 描述 |
---|---|---|
hyperloglog | Hyperloglog | 一个 hyperloglog 对象,可以传递给其他 hyperloglog API 以进行汇总和最终计算 |
示例
给定一个名为 samples
的表,其中包含一个名为 weights
的列,返回 weights
列的 hyperloglog
:
SELECT toolkit_experimental.approx_count_distinct(weights) FROM samples;
使用相同的数据,从聚合中构建一个视图,您可以将其传递给其他 hyperloglog
函数
CREATE VIEW hll AS SELECT toolkit_experimental.approx_count_distinct(data) FROM samples;
distinct_count(hyperloglog Hyperloglog) RETURNS BIGINT
从 hyperloglog 估算不同值的数量
必需参数
名称 | 类型 | 描述 |
---|---|---|
hyperloglog | Hyperloglog | 从中提取计数的 hyperloglog。 |
返回
列 | 类型 | 描述 |
---|---|---|
distinct_count | BIGINT | hyperloglog 计数的不同元素数量。 |
示例
从名为 hyperloglog
的 hyperloglog 估算不同值的数量。预期输出为 98,814
SELECT distinct_count(hyperloglog(8192, data))FROM generate_series(1, 100000) data
distinct_count----------------98814
stderror(hyperloglog Hyperloglog) RETURNS DOUBLE PRECISION
估算 Hyperloglog
的相对标准误差。有关按桶数量的近似相对误差,请参阅相对误差部分。
必需参数
名称 | 类型 | 描述 |
---|---|---|
hyperloglog | Hyperloglog | 要估算误差的 hyperloglog。 |
返回
列 | 类型 | 描述 |
---|---|---|
stderror | DOUBLE PRECISION | hyperloglog 的近似相对标准误差。 |
示例
估算名为 hyperloglog
的 hyperloglog 的相对标准误差。预期输出为 0.011490485194281396
SELECT stderror(hyperloglog(8192, data))FROM generate_series(1, 100000) data
stderror----------------------0.011490485194281396
rollup(hyperloglog Hyperloglog) RETURNS Hyperloglog
将由 hyperloglog 生成的多个中间 hyperloglog 聚合合并为单个中间 hyperloglog 聚合。例如,您可以使用 rollup
将 15 分钟桶的 hyperloglog 合并为每日桶。
必需参数
名称 | 类型 | 描述 |
---|---|---|
hyperloglog | Hyperloglog | 要汇总的 hyperloglog 聚合。 |
返回
列 | 类型 | 描述 |
---|---|---|
rollup | Hyperloglog | 通过组合输入 hyperloglog 聚合创建的新 hyperloglog 聚合。 |
汇总两个 hyperloglog。第一个 hyperloglog 包含从 1 到 100,000 的整数,第二个 hyperloglog 包含从 50,000 到 150,000 的整数。考虑到重叠,组合集中精确的不同值数量为 150,000。
对汇总后的 hyperloglog 调用 distinct_count
得到最终值为 150,552,因此近似值仅偏差 0.368%
SELECT distinct_count(rollup(logs))FROM ((SELECT hyperloglog(4096, v::text) logs FROM generate_series(1, 100000) v)UNION ALL(SELECT hyperloglog(4096, v::text) FROM generate_series(50000, 150000) v)) hll;
输出
distinct_count----------------150552
这些是每个桶大小的近似误差
精度 | 寄存器(桶大小) | 误差 | 列大小(字节) |
---|---|---|---|
4 | 16 | 0.2600 | 12 |
5 | 32 | 0.1838 | 24 |
6 | 64 | 0.1300 | 48 |
7 | 128 | 0.0919 | 96 |
8 | 256 | 0.0650 | 192 |
9 | 512 | 0.0460 | 384 |
10 | 1024 | 0.0325 | 768 |
11 | 2048 | 0.0230 | 1536 |
12 | 4096 | 0.0163 | 3072 |
13 | 8192 | 0.0115 | 6144 |
14 | 16384 | 0.0081 | 12288 |
15 | 32768 | 0.0057 | 24576 |
16 | 65536 | 0.0041 | 49152 |
17 | 131072 | 0.0029 | 98304 |
18 | 262144 | 0.0020 | 196608 |
关键词