count_distinct
COUNT(DISTINCT …) 聚合的替代方案
仓库
tvondra/count_distinct
https://github.com/tvondra/count_distinct
源码
count_distinct-3.0.2.tar.gz
count_distinct-3.0.2.tar.gz
概览
| 扩展包名 | 版本 | 分类 | 许可证 | 语言 |
|---|---|---|---|---|
count_distinct | 3.0.2 | FUNC | BSD 2-Clause | C |
| ID | 扩展名 | Bin | Lib | Load | Create | Trust | Reloc | 模式 |
|---|---|---|---|---|---|---|---|---|
| 4630 | count_distinct | 否 | 是 | 否 | 是 | 否 | 是 | - |
| 相关扩展 | topn hll omnisketch ddsketch quantile lower_quantile first_last_agg aggs_for_arrays |
|---|
no pg14 on el8/9 pgdg
版本
| 类型 | 仓库 | 版本 | PG 大版本 | 包名 | 依赖 |
|---|---|---|---|---|---|
| EXT | MIXED | 3.0.2 | 1817161514 | count_distinct | - |
| RPM | PIGSTY | 3.0.2 | 1817161514 | count_distinct_$v | - |
| DEB | PIGSTY | 3.0.2 | 1817161514 | postgresql-$v-count-distinct | - |
构建
您可以使用 pig build 命令构建 count_distinct 扩展的 RPM / DEB 包:
pig build pkg count_distinct # 构建 RPM / DEB 包
安装
您可以直接安装 count_distinct 扩展包的预置二进制包,首先确保 PGDG 和 PIGSTY 仓库已经添加并启用:
pig repo add pgsql -u # 添加仓库并更新缓存
使用 pig 或者是 apt/yum/dnf 安装扩展:
pig install count_distinct; # 当前活跃 PG 版本安装
pig ext install -y count_distinct -v 18 # PG 18
pig ext install -y count_distinct -v 17 # PG 17
pig ext install -y count_distinct -v 16 # PG 16
pig ext install -y count_distinct -v 15 # PG 15
pig ext install -y count_distinct -v 14 # PG 14
dnf install -y count_distinct_18 # PG 18
dnf install -y count_distinct_17 # PG 17
dnf install -y count_distinct_16 # PG 16
dnf install -y count_distinct_15 # PG 15
dnf install -y count_distinct_14 # PG 14
apt install -y postgresql-18-count-distinct # PG 18
apt install -y postgresql-17-count-distinct # PG 17
apt install -y postgresql-16-count-distinct # PG 16
apt install -y postgresql-15-count-distinct # PG 15
apt install -y postgresql-14-count-distinct # PG 14
创建扩展:
CREATE EXTENSION count_distinct;
用法
提供 COUNT(DISTINCT ...) 的替代实现,避免排序并支持并行聚合。
CREATE EXTENSION count_distinct;
函数
| 函数 | 描述 |
|---|---|
count_distinct(value anyelement) | 计算去重计数(COUNT(DISTINCT ...) 的替代方案) |
array_agg_distinct(value anyelement) | 将去重值聚合为数组 |
count_distinct_elements(value anyarray) | 计算输入数组中去重元素的数量 |
array_agg_distinct_elements(value anyarray) | 将输入数组中的去重元素聚合为数组 |
示例
CREATE TABLE test_table (id INT, val INT);
INSERT INTO test_table
SELECT mod(i, 1000), (1000 * random())::int
FROM generate_series(1, 10000000) s(i);
-- 替代:SELECT id, COUNT(DISTINCT val) FROM test_table GROUP BY 1;
-- 使用:
SELECT id, count_distinct(val) FROM test_table GROUP BY 1;
-- 将去重值聚合为数组
SELECT id, array_agg_distinct(val) FROM test_table GROUP BY 1;
-- 计算数组中的去重元素数量
SELECT count_distinct_elements(ARRAY[1, 2, 2, 3]);