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

直接case when 聚合和先聚合后case when在duckdb150和sqlite3.52的性能比较

同样对10000000行的内存表执行2个值分别和7个值和11个值的复合分组聚合。

C:\d>duckdb150 DuckDB v1.5.0(Variegata)Enter".help"forusagehints.memory D.timeronmemory Dcreatetabletasselecti%2a,i%7b,i%11cfromrange(1,10000000)t(i);RunTime(s):real0.288user0.234375sys0.046875memory Dselecta,count(casebwhen0then1end)b0,count(casebwhen1then1end)b1,count(casebwhen2then1end)b2,count(casebwhen3then1end)b3,count(casebwhen4then1end)b4,count(casebwhen5then1end)b5,count(casebwhen6then1end)b5fromtgroupbya;┌───────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┐ │ a │ b0 │ b1 │ b2 │ b3 │ b4 │ b5 │ b5 │ │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ ├───────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┤ │0714285714286714286714285714286714285714286│ │1714286714286714286714286714285714286714285│ └───────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┘ RunTime(s):real0.051user0.171875sys0.203125memory Dwitht1as(selectcount(*)cnt,a,bfromtgroupbya,b)selecta,sum(casebwhen0thencntend)b0,sum(casebwhen1thencntend)b1,sum(casebwhen2thencntend)b2,sum(casebwhen3thencntend)b3,sum(casebwhen4thencntend)b4,sum(casebwhen5thencntend)b5,sum(casebwhen6thencntend)b5fromt1groupbya;┌───────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┐ │ a │ b0 │ b1 │ b2 │ b3 │ b4 │ b5 │ b5 │ │ int64 │ int128 │ int128 │ int128 │ int128 │ int128 │ int128 │ int128 │ ├───────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┤ │0714285714286714286714285714286714285714286│ │1714286714286714286714286714285714286714285│ └───────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┘ RunTime(s):real0.021user0.156250sys0.031250memory Dselecta,count(casecwhen0then1end)c0,count(casecwhen1then1end)c1,count(casecwhen2then1end)c2,count(casecwhen3then1end)c3,count(casecwhen4then1end)c4,count(casecwhen5then1end)c5,count(casecwhen6then1end)c6,count(casecwhen7then1end)c7,count(casecwhen8then1end)c8,count(casecwhen9then1end)c9,count(casecwhen10then1end)c10fromtgroupbya;┌───────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┐ │ a │ c0 │ c1 │ c2 │ c3 │ c4 │ c5 │ c6 │ c7 │ c8 │ c9 │ c10 │ │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ ├───────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┤ │0454545454545454546454545454546454545454546454545454546454545454545│ │1454545454546454545454546454545454546454545454546454545454546454545│ └───────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┘ RunTime(s):real0.061user0.406250sys0.093750memory Dwitht1as(selectcount(*)cnt,a,cfromtgroupbya,c)selecta,sum(casecwhen0thencntend)c0,sum(casecwhen1thencntend)c1,sum(casecwhen2thencntend)c2,sum(casecwhen3thencntend)c3,sum(casecwhen4thencntend)c4,sum(casecwhen5thencntend)c5,sum(casecwhen6thencntend)c6,sum(casecwhen7thencntend)c7,sum(casecwhen8thencntend)c8,sum(casecwhen9thencntend)c9,sum(casecwhen10thencntend)c10fromt1groupbya;┌───────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┐ │ a │ c0 │ c1 │ c2 │ c3 │ c4 │ c5 │ c6 │ c7 │ c8 │ c9 │ c10 │ │ int64 │ int128 │ int128 │ int128 │ int128 │ int128 │ int128 │ int128 │ int128 │ int128 │ int128 │ int128 │ ├───────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┤ │0454545454545454546454545454546454545454546454545454546454545454545│ │1454545454546454545454546454545454546454545454546454545454546454545│ └───────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┘ RunTime(s):real0.024user0.015625sys0.000000memory Dpivot(selectcount(*)cnt,a,bfromtgroupbya,b)onbusingsum(cnt);┌───────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┐ │ a │0123456│ │ int64 │ int128 │ int128 │ int128 │ int128 │ int128 │ int128 │ int128 │ ├───────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┤ │0714285714286714286714285714286714285714286│ │1714286714286714286714286714285714286714285│ └───────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┘ RunTime(s):real0.037user0.000000sys0.000000memory Dpivot(selectcount(*)cnt,a,cfromtgroupbya,c)oncusingsum(cnt);┌───────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┐ │ a │011023456789│ │ int64 │ int128 │ int128 │ int128 │ int128 │ int128 │ int128 │ int128 │ int128 │ int128 │ int128 │ int128 │ ├───────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┤ │0454545454545454545454546454545454546454545454546454545454546454545│ │1454545454546454545454545454546454545454546454545454546454545454546│ └───────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┘ RunTime(s):real0.031user0.171875sys0.015625memory Dpivottonbusingcount(*)groupbya;┌───────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┐ │ a │0123456│ │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ ├───────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┤ │0714285714286714286714285714286714285714286│ │1714286714286714286714286714285714286714285│ └───────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┘ RunTime(s):real0.109user1.015625sys0.125000memory Dpivottoncusingcount(*)groupbya;┌───────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┐ │ a │011023456789│ │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ int64 │ ├───────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┤ │0454545454545454545454546454545454546454545454546454545454546454545│ │1454545454546454545454545454546454545454546454545454546454545454546│ └───────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┘ RunTime(s):real0.115user1.421875sys0.015625

DuckDB无论case when 7次和11次,都是先聚合更快。pivot比等价的手工case when慢一些。

SQLite version3.52.02026-03-0616:01:44Enter".help"forusagehints.Connectedtoa transientin-memorydatabase.Use".open FILENAME"toreopenona persistentdatabase.sqlite>.timeronsqlite>createtabletasselecti%2a,i%7b,i%11cfrom(selectvalueifromgenerate_series(1,10000000));RunTime:real0.803898user0.781250sys0.015625sqlite>selecta,...>count(casebwhen0then1end)b0,...>count(casebwhen1then1end)b1,...>count(casebwhen2then1end)b2,...>count(casebwhen3then1end)b3,...>count(casebwhen4then1end)b4,...>count(casebwhen5then1end)b5,...>count(casebwhen6then1end)b5...>fromtgroupbya;╭───┬────────┬────────┬────────┬────────┬────────┬────────┬────────╮ │ a │ b0 │ b1 │ b2 │ b3 │ b4 │ b5 │ b5 │ ╞═══╪════════╪════════╪════════╪════════╪════════╪════════╪════════╡ │0714285714286714286714286714286714285714286│ │1714286714286714286714286714285714286714285│ ╰───┴────────┴────────┴────────┴────────┴────────┴────────┴────────╯ RunTime:real2.634213user2.515625sys0.109375sqlite>witht1as(selectcount(*)cnt,a,bfromtgroupbya,b)...>selecta,...>sum(casebwhen0thencntend)b0,...>sum(casebwhen1thencntend)b1,...>sum(casebwhen2thencntend)b2,...>sum(casebwhen3thencntend)b3,...>sum(casebwhen4thencntend)b4,...>sum(casebwhen5thencntend)b5,...>sum(casebwhen6thencntend)b5...>fromt1groupbya;╭───┬────────┬────────┬────────┬────────┬────────┬────────┬────────╮ │ a │ b0 │ b1 │ b2 │ b3 │ b4 │ b5 │ b5 │ ╞═══╪════════╪════════╪════════╪════════╪════════╪════════╪════════╡ │0714285714286714286714286714286714285714286│ │1714286714286714286714286714285714286714285│ ╰───┴────────┴────────┴────────┴────────┴────────┴────────┴────────╯ RunTime:real3.167292user3.031250sys0.125000sqlite>selecta,...>count(casecwhen0then1end)c0,...>count(casecwhen1then1end)c1,...>count(casecwhen2then1end)c2,...>count(casecwhen3then1end)c3,...>count(casecwhen4then1end)c4,...>count(casecwhen5then1end)c5,...>count(casecwhen6then1end)c6,...>count(casecwhen7then1end)c7,...>count(casecwhen8then1end)c8,...>count(casecwhen9then1end)c9,...>count(casecwhen10then1end)c10...>fromtgroupbya;╭───┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────╮ │ a │ c0 │ c1 │ c2 │ c3 │ c4 │ c5 │ c6 │ c7 │ c8 │ c9 │ c10 │ ╞═══╪════════╪════════╪════════╪════════╪════════╪════════╪════════╪════════╪════════╪════════╪════════╡ │0454545454545454546454545454546454545454546454545454546454545454546│ │1454545454546454545454546454545454546454545454546454545454546454545│ ╰───┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────╯ RunTime:real3.113160user2.890625sys0.218750sqlite>witht1as(selectcount(*)cnt,a,cfromtgroupbya,c)...>selecta,...>sum(casecwhen0thencntend)c0,...>sum(casecwhen1thencntend)c1,...>sum(casecwhen2thencntend)c2,...>sum(casecwhen3thencntend)c3,...>sum(casecwhen4thencntend)c4,...>sum(casecwhen5thencntend)c5,...>sum(casecwhen6thencntend)c6,...>sum(casecwhen7thencntend)c7,...>sum(casecwhen8thencntend)c8,...>sum(casecwhen9thencntend)c9,...>sum(casecwhen10thencntend)c10...>fromt1groupbya;╭───┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────╮ │ a │ c0 │ c1 │ c2 │ c3 │ c4 │ c5 │ c6 │ c7 │ c8 │ c9 │ c10 │ ╞═══╪════════╪════════╪════════╪════════╪════════╪════════╪════════╪════════╪════════╪════════╪════════╡ │0454545454545454546454545454546454545454546454545454546454545454546│ │1454545454546454545454546454545454546454545454546454545454546454545│ ╰───┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────╯ RunTime:real3.251369user3.046875sys0.171875sqlite>

SQLite无论case when 7次和11次,都是后聚合更快。11次的两种方法差距更小,预计更多项需要case when时会反转。

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

相关文章:

  • 打卡信奥刷题(2944)用C++实现信奥题 P5858 「SWTR-3」Golden Sword
  • Flutter 三方库 twitch_api 的鸿蒙适配指南 - 打造高性能流媒体互动体验、深度集成直播数据与实时信令
  • 工程设计类学习(DAY21):EMC检测全解析:从EMI到EMS
  • 【优化功率】基于遗传算法GA分析发电站的用电需求和发电量优化输电线路的功率损失附Matlab实现
  • 【麒麟系统】Kylin-Server-10-SP2-x86日常使用记录
  • C语言学习Class5
  • .NET命名之谜:它与C#纠缠20年的关系揭秘
  • 从六边形到 DDD:一条真正可落地的 Go 渐进演进路线
  • Java高频面试题(五):MySQL事务与索引优化全解析
  • 51单片机开发的直流电机PID 算法控制转速项目,可实现稳定调节设定转速。 非常实用的一个项目
  • Python基于flask的美容美发理发店管理系统 基于JAVAWEB的理发店会员管理系统
  • 全国各省/直辖市/自治区CLCD1985~2024年30米土地利用数据(分省裁剪)
  • 柔性温度传感器---直线型结构(2)
  • 鸿蒙应用开发UI基础第二十一节:自定义组件与页面的生命周期
  • SFT构造数据的一些经验
  • VMware虚拟机配置桥接网络
  • 丝杆升降机如何正确选型?参数、工况、电机匹配一篇讲透
  • Python基于flask的角色扮演论坛的设计与实现 可视化
  • RAG架构实战:从文档问答到企业知识中枢的跨越
  • 2026年03月11日最热门的开源项目(Github)
  • 第一章 JVM 基础执行指令与调优基础
  • 利率显示清晰的贷款平台怎么选?这份避坑指南请收好 - 速递信息
  • 食品厂0.5吨立式生物质蒸汽发生器
  • 高德车机版9.1.87美化版
  • 2026-03-12 全国各地响应最快的 BT Tracker 服务器(电信版)
  • 2026年大模型TOP 5落地场景出炉:第一场景从“知识库”转向“智能决策”
  • 2026新托福机构首选:多次元托福稳居TOP1的5大核心理由(附机构对比) - 速递信息
  • 计算机网络绪论:socket套接字、fd、进程、端口号之间的联系
  • CUDA 编程系列(二)《性能模型与逐元素优化》
  • 定位诗学:亚马逊时代从“产品咏叹”到“心智信号”的广告进化