news 2026/4/18 14:28:25

利用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/18 3:46:31

Playwright MCP

在TRAE中使用Playwright MCP,相当于给你的AI助手装上了能“看”网页、能“点”按钮的虚拟手眼。你不再需要逐行编写测试代码,只需像告诉同事一样,用自然语言描述测试步骤即可。 整个过程的核心是 “环境配置 -> 连接工具 -> 对话驱动”…

作者头像 李华
网站建设 2026/4/18 9:45:09

【剑斩OFFER】算法的暴力美学——力扣 1020 题:飞地的数量

一、题目描述二、算法原理思路:使用 BFS 算法先处理边界 1 ,再使用 BFS 统计陆地,体现正难则反的思想;例如:1)创建一个二维数组来专门标记是否入过队列或者说遍历过;2)使用 BFS 算法…

作者头像 李华
网站建设 2026/4/18 12:29:20

为什么自己写的论文AIGC率那么高?

去年底,Turnitin系统进行了一次重要升级。 以下是更新通知: 从通知上来看turnitin更新了AI语言大模型,更新后的AI检测模型能识别出更多AI内容,同时仍然保持较低的误报率。 系统更新后,很多同学就遇到一个问题&#x…

作者头像 李华