当前位置: 首页 > news >正文

利用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 …)更快。

http://www.jsqmd.com/news/313968/

相关文章:

  • 热力学计算技术或将大幅降低AI图像生成能耗
  • 多代理系统VS单体AI:性能对比与选型建议
  • 用TRAE如何实现开源AI智能体“Clawdbot”
  • Context7 MCP
  • 零散HTML页面整合为交互式Demo系统的专业的技术规划与实现路径
  • Memory MCP
  • 【Redis架构优化篇】吃透Docker+Redis:从单节点到主从哨兵,配置模板直接抄
  • Playwright MCP
  • 使用 TRAE进行团队开发
  • Gitlab MCP
  • 恒小花分期商城额度怎么使用以及怎么提现出来
  • 基于大语言模型协同标注的政策文本技术实体识别研究
  • 延迟绑定机制与ret2dlresolve
  • 完整教程:Leetcode 70 好数对的数目 | 与对应负数同时存在的最大正整数
  • Spring 中 REST API 调用工具对比:RestTemplate vs OpenFeign
  • 【游戏推荐】展翅翱翔 全DLC(WINGSPAN)免安装中文版
  • Simulink 程序状态声音提示方案-PART-蜂鸣-程序提示
  • k8s部署dify
  • k8s部署harbor(storageclass+ingress)
  • k8s新版本nfs-csi
  • iPhone XS 全面解析:宣传图/配色/参数速查 + 体验亮点 + 二手选购避坑清单
  • 2026年了,作为一个程序员你觉得是业务重要还是技术重要?
  • 随机数预测与爆破canary
  • 我爱学习,我的思想特别的正
  • Java项目用了JSQParser,但它用的是LGPL-2.1-only许可证,这会有哪些合规隐患?怎么安全替换或规避?
  • HTTPS克隆Git仓库时总要输密码,怎么一劳永逸地解决?
  • 数据结构:(三)字符串——从暴力匹配到 KMP 的跨越
  • Abaqus螺栓施加预紧力
  • 智能体设计模式重点
  • 数据说话,2026国自然或许是最难的一年