news 2026/3/27 21:23:38

利用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/3/14 8:55:42

Context7 MCP

在TRAE中使用Context7,相当于给你的AI助手配了一位随时能调阅全球最新技术文档的图书管理员。它的核心是解决一个根本问题:让AI的答案摆脱训练数据的时间限制,能“活在当下”。 Context7 是什么:为什么需要它? 你可以…

作者头像 李华
网站建设 2026/3/13 2:28:02

数据结构:(三)字符串——从暴力匹配到 KMP 的跨越

一、 串的存储结构:定长 vs 堆串是由零个或多个字符组成的有限序列。在 C 语言中,我们主要关注两种实现:定长顺序存储:使用静态数组 char str[MAXSIZE]。缺点是长度固定,容易发生截断。堆分配存储(重点&…

作者头像 李华
网站建设 2026/3/27 17:38:36

Leetcode49:字母异位词分组

给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。 示例 1: 输入: strs ["eat", "tea", "tan", "ate", "nat", "bat"] 输出: [["bat"],["nat",…

作者头像 李华
网站建设 2026/3/27 5:56:52

Puppeteer MCP

在TRAE中使用Puppeteer MCP,相当于给你的AI编程助手装上了一双可以自动操作浏览器的手。它能把那些需要你手动点击、输入和查看网页的重复性工作,变成一句简单的指令。 🛠️ Puppeteer MCP 能做什么? 简单来说,它让T…

作者头像 李华
网站建设 2026/3/16 23:38:39

Sequential Thinking MCP

在TRAE国际版中,Sequential Thinking是让你与AI协作处理复杂任务的“思维导航仪”。它能把一个笼统的大问题,像拼乐高一样,拆解成一系列清晰、可执行的小步骤,并且边做边想,随时调整。 🧠 核心理解&#x…

作者头像 李华
网站建设 2026/3/22 0:14:09

深度测评 自考必用TOP8一键生成论文工具:高效写作全解析

深度测评 自考必用TOP8一键生成论文工具:高效写作全解析 自考论文写作工具测评:为何需要一份权威榜单? 随着自考人数逐年增长,论文写作成为众多考生必须面对的挑战。从选题构思到内容撰写,再到格式规范与查重处理&am…

作者头像 李华