一、概述

优化拥有大量的分组和去重列的SQL时,这些排序列的次序,也是可以优化的地方。

测试数据结构

kingbase=# select count(distinct txt1 ) txt1, avg(length(txt1))::int ln1, count(distinct txt3 ) txt3 ,avg(length(txt3))::int ln3 from txt01;

 txt1 | ln1  |  txt3   | ln3
------+------+---------+-----
1000 | 1000 | 1000000 | 10
(1 行记录)

二、work_mem 满足排序情况

1、Distinct 语句

次序: txt1,txt3

kingbase=# explain (analyse ,buffers ) select /*+ set(work_mem 100MB) */ distinct txt1 ,txt3 from txt01 ;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
HashAggregate (cost=269287.33..269687.33 rows=40000 width=64) (actual time=1543.995..1877.527 rows=1000000 loops=1)
Group Key: txt1, txt3
Buffers: shared hit=142858
-> Seq Scan on txt01 (cost=0.00..227144.22 rows=8428622 width=64) (actual time=0.008..159.858 rows=1000000 loops=1)
Buffers: shared hit=142858
Planning Time: 0.081 ms
Execution Time: 1947.951 ms
(7 行记录)

次序: txt3,txt1

ingbase=# explain (analyse ,buffers ) select /*+ set(work_mem 100MB) */ distinct txt3 ,txt1 from txt01 ;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------
HashAggregate (cost=269287.33..269687.33 rows=40000 width=64) (actual time=1596.040..1812.380 rows=1000000 loops=1)
Group Key: txt3, txt1
Buffers: shared hit=142858
-> Seq Scan on txt01 (cost=0.00..227144.22 rows=8428622 width=64) (actual time=0.007..163.399 rows=1000000 loops=1)
Buffers: shared hit=142858
Planning Time: 0.075 ms
Execution Time: 1884.907 ms
(7 行记录)

2、Group by 语句

次序: txt1,txt3

kingbase=# explain (analyse ,buffers ) select /*+ set(work_mem 100MB) */  txt1 ,txt3 from txt01 group by txt1 ,txt3 ;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------
HashAggregate (cost=269287.33..269687.33 rows=40000 width=64) (actual time=1540.948..1875.917 rows=1000000 loops=1)
Group Key: txt1, txt3
Buffers: shared hit=142858
-> Seq Scan on txt01 (cost=0.00..227144.22 rows=8428622 width=64) (actual time=0.006..160.419 rows=1000000 loops=1)
Buffers: shared hit=142858
Planning Time: 0.084 ms
Execution Time: 1939.103 ms
(7 行记录)

次序: txt3,txt1

kingbase=# explain (analyse ,buffers ) select /*+ set(work_mem 100MB) */ txt1 ,txt3 from txt01 group by txt3 ,txt1 ;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------
HashAggregate (cost=269287.33..269687.33 rows=40000 width=64) (actual time=1557.257..1780.662 rows=1000000 loops=1)
Group Key: txt3, txt1
Buffers: shared hit=142858
-> Seq Scan on txt01 (cost=0.00..227144.22 rows=8428622 width=64) (actual time=0.018..165.221 rows=1000000 loops=1)
Buffers: shared hit=142858
Planning Time: 0.330 ms
Execution Time: 1844.664 ms
(7 行记录)

三、work_mem 不满足排序情况

1、Distinct 语句

次序: txt1,txt3

kingbase=# explain (analyse ,buffers ) select /*+ set(work_mem 1MB) */ distinct txt1 ,txt3 from txt01  ;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------
Unique (cost=2464313.08..2527527.74 rows=40000 width=64) (actual time=21031.092..22131.259 rows=1000000 loops=1)
Buffers: shared hit=142858, temp read=125368 written=125369
-> Sort (cost=2464313.08..2485384.63 rows=8428622 width=64) (actual time=21031.089..22002.850 rows=1000000 loops=1)
Sort Key: txt1, txt3
Sort Method: external merge Disk: 1002944kB
Buffers: shared hit=142858, temp read=125368 written=125369
-> Seq Scan on txt01 (cost=0.00..227144.22 rows=8428622 width=64) (actual time=0.039..272.327 rows=1000000 loops=1)
Buffers: shared hit=142858
Planning Time: 0.272 ms
Execution Time: 23648.185 ms
(10 行记录)

次序: txt3,txt1

kingbase=# explain (analyse ,buffers ) select /*+ set(work_mem 1MB) */ distinct txt3 ,txt1 from txt01  ;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------
Unique (cost=2464313.08..2527527.74 rows=40000 width=64) (actual time=4004.641..4367.218 rows=1000000 loops=1)
Buffers: shared hit=142858, temp read=125491 written=125492
-> Sort (cost=2464313.08..2485384.63 rows=8428622 width=64) (actual time=4004.639..4239.599 rows=1000000 loops=1)
Sort Key: txt3, txt1
Sort Method: external merge Disk: 1003928kB
Buffers: shared hit=142858, temp read=125491 written=125492
-> Seq Scan on txt01 (cost=0.00..227144.22 rows=8428622 width=64) (actual time=0.011..271.572 rows=1000000 loops=1)
Buffers: shared hit=142858
Planning Time: 0.086 ms
Execution Time: 4457.751 ms
(10 行记录)

2、Group by 语句

次序: txt1,txt3

kingbase=# explain (analyse ,buffers ) select /*+ set(work_mem 1MB) */ txt1 ,txt3 from txt01 group by txt1 ,txt3 ;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------
Group (cost=2464313.08..2527527.74 rows=40000 width=64) (actual time=21715.770..22796.166 rows=1000000 loops=1)
Group Key: txt1, txt3
Buffers: shared hit=142858, temp read=125368 written=125369
-> Sort (cost=2464313.08..2485384.63 rows=8428622 width=64) (actual time=21715.764..22658.413 rows=1000000 loops=1)
Sort Key: txt1, txt3
Sort Method: external merge Disk: 1002944kB
Buffers: shared hit=142858, temp read=125368 written=125369
-> Seq Scan on txt01 (cost=0.00..227144.22 rows=8428622 width=64) (actual time=0.029..271.335 rows=1000000 loops=1)
Buffers: shared hit=142858
Planning Time: 0.285 ms
Execution Time: 25365.012 ms
(11 行记录)

次序: txt3,txt1

kingbase=# explain (analyse ,buffers ) select /*+ set(work_mem 1MB) */ txt1 ,txt3 from txt01 group by txt3 ,txt1 ;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------
Group (cost=2464313.08..2527527.74 rows=40000 width=64) (actual time=4156.296..4541.315 rows=1000000 loops=1)
Group Key: txt3, txt1
Buffers: shared hit=142858, temp read=125368 written=125369
-> Sort (cost=2464313.08..2485384.63 rows=8428622 width=64) (actual time=4156.291..4402.265 rows=1000000 loops=1)
Sort Key: txt3, txt1
Sort Method: external merge Disk: 1002944kB
Buffers: shared hit=142858, temp read=125368 written=125369
-> Seq Scan on txt01 (cost=0.00..227144.22 rows=8428622 width=64) (actual time=0.008..270.872 rows=1000000 loops=1)
Buffers: shared hit=142858
Planning Time: 0.081 ms
Execution Time: 4632.567 ms
(11 行记录)

四、总结

次序 txt1,txt1 txt3,txt1
work_mem满足排序 1947.951 ms 1884.907 ms
work_mem不足排序 25365.012 ms 4632.567 ms

字节少数据值多的列,处于排序列的前列,可以带来性能的提升。当work_mem满足排序时,性能差异不大,当work_mem不足时,性能提升较大。

Group 和 Distinct 列的次序影响查询性能的更多相关文章

  1. Sql Server查询性能优化之走出索引的误区

    据了解绝大多数开发人员对于索引的理解都是一知半解,局限于大多数日常工作没有机会.也什么没有必要去关心.了解索引,实在哪天某个查询太慢了找到查询条件建个索引就ok,哪天又有个查询慢了,再建立个索引就是, ...

  2. 怎样group by一列 select多列

    之前sql用的少 竟然不知道这个小技巧 1 将要查询的列 添加到group by后面(会影响查询结果) 2 使用聚合函数如 max select a.accounttitlecode, max(b.c ...

  3. SQL Server 执行计划利用统计信息对数据行的预估原理二(为什么复合索引列顺序会影响到执行计划对数据行的预估)

    本文出处:http://www.cnblogs.com/wy123/p/6008477.html 关于统计信息对数据行数做预估,之前写过对非相关列(单独或者单独的索引列)进行预估时候的算法,参考这里. ...

  4. mysql经常使用查询:group by,左连接,子查询,having where

    前几天去了两个比較牛的互联网公司面试.在sql这块都遇到问题了,哎.可惜呀,先把简单的梳理一下 成绩表 score 1.group by 使用 按某一个维度进行分组 比如: 求每一个同学的总分 SEL ...

  5. SQL Server-聚焦计算列或计算列持久化查询性能(二十二)

    前言 上一节我们详细讲解了计算列以及计算列持久化的问题,本节我们依然如前面讲解来看看二者查询性能问题,简短的内容,深入的理解,Always to review the basics. 持久化计算列比非 ...

  6. 一种更高查询性能的列存储方式MaxMinT 第一部分

    简介本文描述了一种列存储方式和对应的查询方法,这种存储方式具有更好的查询性能和更小的存储空间. And查询 本文先用直观的图形方式展示and查询时的方式,这也是算法要解决的问题核心.通常在OLAP数据 ...

  7. Phoenix表和索引分区数对插入和查询性能的影响

    1. 概述 1.1 HBase概述 HBase由master节点和region server节点组成.在100-105集群上,100和101是master节点,102-105是region serve ...

  8. 高性能MySQL笔记 第6章 查询性能优化

    6.1 为什么查询速度会慢   查询的生命周期大致可按照顺序来看:从客户端,到服务器,然后在服务器上进行解析,生成执行计划,执行,并返回结果给客户端.其中“执行”可以认为是整个生命周期中最重要的阶段. ...

  9. mysql笔记03 查询性能优化

    查询性能优化 1. 为什么查询速度会慢? 1). 如果把查询看作是一个任务,那么它由一系列子任务组成,每个子任务都会消耗一定的时间.如果要优化查询,实际上要优化其子任务,要么消除其中一些子任务,要么减 ...

随机推荐

  1. iftop使用

    在linux中监控系统资源.进程.内存占用等信息,可以使用top命令. 查看网络状态可以使用netstat工具. 如果想查看实时的网络流量,监控TCP/IP连接等,则可以使用iftop工具. 一.if ...

  2. jieba分词的功能和性能分析

    jieba分词问题导引 用户词典大小最大可以有多大 用户词典大小对速度的影响 有相同前缀和后缀的词汇如何区分 对比百度分词的API 问题一:词典大小 从源码大小分析,整个jieba分词的源码总容量为8 ...

  3. sql-DQL-多表联查

    多表查询 笛卡尔积 左表的每条数据和右表的每条数据组合,这种效果称为笛卡尔乘积 select * from emp, dept; 笛卡尔积引入了很多无用的数据,要完成多表查询,需要设置过滤条件来消除无 ...

  4. Linux 安装Apche服务

    用yum 进行在线安装apche服务 yum install -y httpd 我这边是centos7 需要开启一下端口: 1 firewall-cmd --zone=public --add-por ...

  5. PTA(BasicLevel)-1031 查验身份证

    一.问题定义 一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下:首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,1 ...

  6. PoweJob高级特性-MapReduce完整示例

    由于网上搜索 PowerJob MapReduce 都是设计原理,demo也展示个空壳子,没有演示Map到Reduce结果怎么传递,对于没有MR开发经验的人来说并没有什么帮助,所以这里写了一个有完整计 ...

  7. Modeling Conversation Structure and Temporal Dynamics for Jointly Predicting Rumor Stance and Veracity(ACL-19)

    记录一下,论文建模对话结构和时序动态来联合预测谣言立场和真实性及其代码复现. 1 引言 之前的研究发现,公众对谣言消息的立场是识别流行的谣言的关键信号,这也能表明它们的真实性.因此,对谣言的立场分类被 ...

  8. 聊聊 C++ 中几类特殊成员函数

    一:背景 在 C# 中要说类默认给我们定义的特殊成员函数,莫过于 构造函数,但在 C++ 中这样的特殊函数高达 6 种,有必要整合一下聊一聊. 二:特殊成员函数 1. 默认构造函数 和 C# 一样,很 ...

  9. hive SQL 初学者题目,实战题目 字符串函数,日期拼接,开窗函数。。。。

    sql:Hive实现按照指定格式输出每七天的消费平均数输出格式:2018-06-01~2018-06-07 12.29...2018-08-10~2018-08-16 80.67 答案:-- 1.先将 ...

  10. Techempower web框架性能测试第21轮结果发布--asp.net core继续前进

    废话不说,直接上结果: Round 21 results - TechEmpower Framework Benchmarks Techempower benchmark是包含范围最广泛的web框架性 ...