Group 和 Distinct 列的次序影响查询性能
一、概述
优化拥有大量的分组和去重列的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 列的次序影响查询性能的更多相关文章
- Sql Server查询性能优化之走出索引的误区
据了解绝大多数开发人员对于索引的理解都是一知半解,局限于大多数日常工作没有机会.也什么没有必要去关心.了解索引,实在哪天某个查询太慢了找到查询条件建个索引就ok,哪天又有个查询慢了,再建立个索引就是, ...
- 怎样group by一列 select多列
之前sql用的少 竟然不知道这个小技巧 1 将要查询的列 添加到group by后面(会影响查询结果) 2 使用聚合函数如 max select a.accounttitlecode, max(b.c ...
- SQL Server 执行计划利用统计信息对数据行的预估原理二(为什么复合索引列顺序会影响到执行计划对数据行的预估)
本文出处:http://www.cnblogs.com/wy123/p/6008477.html 关于统计信息对数据行数做预估,之前写过对非相关列(单独或者单独的索引列)进行预估时候的算法,参考这里. ...
- mysql经常使用查询:group by,左连接,子查询,having where
前几天去了两个比較牛的互联网公司面试.在sql这块都遇到问题了,哎.可惜呀,先把简单的梳理一下 成绩表 score 1.group by 使用 按某一个维度进行分组 比如: 求每一个同学的总分 SEL ...
- SQL Server-聚焦计算列或计算列持久化查询性能(二十二)
前言 上一节我们详细讲解了计算列以及计算列持久化的问题,本节我们依然如前面讲解来看看二者查询性能问题,简短的内容,深入的理解,Always to review the basics. 持久化计算列比非 ...
- 一种更高查询性能的列存储方式MaxMinT 第一部分
简介本文描述了一种列存储方式和对应的查询方法,这种存储方式具有更好的查询性能和更小的存储空间. And查询 本文先用直观的图形方式展示and查询时的方式,这也是算法要解决的问题核心.通常在OLAP数据 ...
- Phoenix表和索引分区数对插入和查询性能的影响
1. 概述 1.1 HBase概述 HBase由master节点和region server节点组成.在100-105集群上,100和101是master节点,102-105是region serve ...
- 高性能MySQL笔记 第6章 查询性能优化
6.1 为什么查询速度会慢 查询的生命周期大致可按照顺序来看:从客户端,到服务器,然后在服务器上进行解析,生成执行计划,执行,并返回结果给客户端.其中“执行”可以认为是整个生命周期中最重要的阶段. ...
- mysql笔记03 查询性能优化
查询性能优化 1. 为什么查询速度会慢? 1). 如果把查询看作是一个任务,那么它由一系列子任务组成,每个子任务都会消耗一定的时间.如果要优化查询,实际上要优化其子任务,要么消除其中一些子任务,要么减 ...
随机推荐
- Python Excel 操作
1.Excel Code import os import time import re import win32com.client def dealpath(pathname='') -> ...
- SAP JSON 格式化及解析。
一.首选:/ui2/cl_json {'key':'value'} /ui2/cl_json=>deserialize( EXPORTING json = json CHANGING d ...
- NC24083 [USACO 2017 Dec P]Greedy Gift Takers
NC24083 [USACO 2017 Dec P]Greedy Gift Takers 题目 题目描述 Farmer John's nemesis, Farmer Nhoj, has N cows ...
- 搭建企业级实时数据融合平台难吗?Tapdata + ES + MongoDB 就能搞定
摘要:如何打造一套企业级的实时数据融合平台?Tapdata 已经找到了最佳实践,下文将以 Tapdata 的零售行业客户为例,与您分享:基于 ES 和 MongoDB 来快速构建一套企业级的实时数 ...
- 跨模态语义关联对齐检索-图像文本匹配(Image-Text Matching)
论文介绍:Negative-Aware Attention Framework for Image-Text Matching (基于负感知注意力的图文匹配,CVPR2022) 代码主页:https: ...
- Linux快捷方式创建模板
1.创建快捷方式文件 sudo gedit /usr/share/applications/Navicat.desktop 模板: [Desktop Entry] Name=Navicat Exec= ...
- 2 Zookeeper 单击安装
(二)Zookeeper 本地模式安装 下载地址 镜像库地址:http://archive.apache.org/dist/zookeeper/ apache-zookeeper-3.6.0.tar. ...
- Docker安装及基础命令
一.docker安装 1.安装docker #关闭防火墙和selinux systemctl stop firewalld.service setenforce 0 #安装依赖包 yum ins ...
- jdbc 08: statement应用场景
jdbc连接mysql,statement的应用场景 package com.examples.jdbc.o8_statement应用场景; import java.sql.*; import jav ...
- 【PostgreSQL 15】PostgreSQL 15对UNIQUE和NULL的改进
用一句话来总结这种改进就是: 支持唯一性约束和索引将null值视为相同的值.之前是将null值索引成不同的值,现在可以通过使用unique nulls not distinct创建约束,将null值视 ...