news 2026/4/16 8:59:58

利用DuckDB的bitstring_agg函数配合bit_count快速求不同值的计数

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
利用DuckDB的bitstring_agg函数配合bit_count快速求不同值的计数

在翻阅DuckDB的文档时看到bitstring_agg这么个函数, 还提到能代替count(DISTINCT …)获得更高的性能。但文档没有给出输出的例子。

bitstring_agg(arg)
Description The bitstring_agg function takes any integer type as input and returns a bitstring with bits set for each distinct value. The left-most bit represents the smallest value in the column and the right-most bit the maximum value. If possible, the min and max are retrieved from the column statistics. Otherwise, it is also possible to provide the min and max values.
Example bitstring_agg(A)
Tip
The combination of bit_count and bitstring_agg can be used as an alternative to count(DISTINCT …), with possible performance improvements in cases of low cardinality and dense values.

bitstring_agg(arg, min, max)
Description Returns a bitstring with bits set for each distinct position defined in arg. All positions must be within the range [min, max] or an Out of Range Error will be thrown.
Example bitstring_agg(A, 1, 42)

先来看bitstring_agg的输出

memory Dselectbitstring_agg(A,1,22)from(select11aunionallselect13unionallselect11);┌─────────────────────────┐ │ bitstring_agg(A,1,22)│ │bit│ ├─────────────────────────┤ │0000000000101000000000│ └─────────────────────────┘ memory Dselectbitstring(bitstring_agg(A,1,22),30)from(select11aunionallselect13unionallselect11);┌────────────────────────────────────────┐ │ bitstring(bitstring_agg(A,1,22),30)│ │bit│ ├────────────────────────────────────────┤ │000000000000000000101000000000│ └────────────────────────────────────────┘

它返回从左到右第11位和第13位为1,其他位为0的二进制字符串。如果用bitstring(长度)扩充字符串的长度,则在左侧补零。
下面用随机100万个整数来测试bitstring_agg函数配合bit_count求不同值的计数,并与count(DISTINCT …)比较用时。

memory Dcreatetabletas(select(i*random())::intifromrange(1,1000000)t(i));memory Dselectcount(distincti)fromt;┌───────────────────┐ │count(DISTINCTi)│ │ int64 │ ├───────────────────┤ │499996│ └───────────────────┘ RunTime(s):real0.023user0.136000sys0.016000memory Dselectbit_count(bitstring_agg(i,0,1000000))fromt;┌─────────────────────────────────────────┐ │ bit_count(bitstring_agg(i,0,1000000))│ │ int64 │ ├─────────────────────────────────────────┤ │499996│ └─────────────────────────────────────────┘ RunTime(s):real0.008user0.052000sys0.000000

可见,虽然数据不很稠密,大概有一半重复,bit_count(bitstring_agg())还是比count(DISTINCT …)更快。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/13 14:45:51

数据说话,2026国自然或许是最难的一年

国自然申报季的钟声早已敲响,2026年集中接收期已明确为3月1日至3月20日16时,万千科研人正全力冲刺申请书撰写。回望2024-2025年国自然资助全貌,一组组数据背后,不仅是资助导向的细微调整,更暗藏着逐年加剧的竞争信号—…

作者头像 李华
网站建设 2026/4/14 16:37:24

Uncertainty-Aware Bayesian PINN机械退化趋势预测(Pytorch)

算法特点贝叶斯不确定性量化,将贝叶斯神经网络与物理信息神经网络结合,提供预测结果的不确定性区间,解决传统黑箱模型信任度低的问题自适应物理约束学习,通过可学习物理权重参数,动态平衡数据驱动与物理规律约束&#…

作者头像 李华
网站建设 2026/4/8 18:08:02

【游戏推荐】云族裔 韩国模拟人生 (inZOI)免安装中文版

类型: 建造, 生活模拟 链接:https://pan.quark.cn/s/02986ba329e7 游戏简介 在 inZOI(云族裔) 这款生活模拟游戏中,玩家将化身为创造者,按照自己的构想塑造世界,见证一个个精彩故事的展开。 …

作者头像 李华
网站建设 2026/3/21 6:14:57

家禽商城销售系统

家禽商城销售系统的课题背景 随着互联网技术的快速发展和电子商务的普及,传统家禽行业正面临数字化转型的需求。家禽产品作为日常生活必需品,市场需求稳定,但传统销售模式存在信息不对称、流通效率低、供应链管理粗放等问题。线下交易受地域限…

作者头像 李华
网站建设 2026/4/14 17:09:36

【模拟】螺旋矩阵

求解代码 public ArrayList<Integer> spiralOrder(int[][] matrix) {ArrayList<Integer> ans new ArrayList<>();// 处理空矩阵、空行、空列场景&#xff0c;避免空指针/数组越界if (matrix null || matrix.length 0 || matrix[0].length 0) {return ans…

作者头像 李华