如何使用group by进行去重

因为mysql的distinct在结果集中,全部不同,才可以去重。
所以,当我们进行去重处理的时候,需要单独对某列进行去重,可以使用group by子句进行分组去重
select _auto_id from account_login group by _auto_id; 该语句可以对_auto_id列进行去重。

在使用group by进行去重效率分析

无索引
0.23s

mysql> explain select _auto_id from account_login group by _auto_id;
+----+-------------+---------------+------+---------------+------+---------+------+--------+---------------------------------+
| id | select_type | table         | type | possible_keys | key  | key_len | ref  | rows   | Extra                           |
+----+-------------+---------------+------+---------------+------+---------+------+--------+---------------------------------+
|  1 | SIMPLE      | account_login | ALL  | NULL          | NULL | NULL    | NULL | 133257 | Using temporary; Using filesort |
+----+-------------+---------------+------+---------------+------+---------+------+--------+---------------------------------+

mysql> show profile;
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000154 |
| checking permissions | 0.000012 |
| Opening tables       | 0.000029 |
| init                 | 0.000029 |
| System lock          | 0.000014 |
| optimizing           | 0.000010 |
| statistics           | 0.000021 |
| preparing            | 0.000020 |
| Creating tmp table   | 0.000036 |
| Sorting result       | 0.000007 |
| executing            | 0.000005 |
| Sending data         | 0.207841 |
| Creating sort index  | 0.021024 |
| end                  | 0.000010 |
| removing tmp table   | 0.000130 |
| end                  | 0.000010 |
| query end            | 0.000016 |
| closing tables       | 0.000019 |
| freeing items        | 0.000035 |
| cleaning up          | 0.000039 |
+----------------------+----------+
20 rows in set, 1 warning (0.00 sec)

此处创建了sort index进行排序,说明对MySQL使用了内存临时表,group by后面的排序过程是使用sort index来完成的,而且该内存临时表的大小是由MAX_HEAP_TABLE_SIZE来控制。

Sending data 显示的这个时间 = Time(Sending data) + Time (Sorting result), 这样其实应该是排序所用的时间

因为在group by后会进行自动排序,如果该我们仅仅想去重,而不需要排序,可以使用

mysql> explain select _auto_id from account_login group by _auto_id order by null;
+----+-------------+---------------+------+---------------+------+---------+------+--------+-----------------+
| id | select_type | table         | type | possible_keys | key  | key_len | ref  | rows   | Extra           |
+----+-------------+---------------+------+---------------+------+---------+------+--------+-----------------+
|  1 | SIMPLE      | account_login | ALL  | NULL          | NULL | NULL    | NULL | 133257 | Using temporary |
+----+-------------+---------------+------+---------------+------+---------+------+--------+-----------------+

+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000155 |
| checking permissions | 0.000012 |
| Opening tables       | 0.000029 |
| init                 | 0.000029 |
| System lock          | 0.000014 |
| optimizing           | 0.000009 |
| statistics           | 0.000022 |
| preparing            | 0.000020 |
| Creating tmp table   | 0.000042 |
| executing            | 0.000006 |
| Sending data         | 0.219640 |
| end                  | 0.000021 |
| removing tmp table   | 0.000014 |
| end                  | 0.000008 |
| query end            | 0.000014 |
| closing tables       | 0.000020 |
| freeing items        | 0.000033 |
| cleaning up          | 0.000020 |
+----------------------+----------+

可以发现,在加入order by null子句后,MySQL并没有创建sort index进行排序(内存排序非常快,优化效果并不明显,并且这个阶段只是每个数据块的排序,)。但是在group by后添加多列,并且不能进行

有索引
mysql> explain select _auto_id from account_login group by _auto_id;
使用时间 0.11s
执行计划

+----+-------------+---------------+-------+---------------+---------+---------+------+--------+-------------+
| id | select_type | table         | type  | possible_keys | key     | key_len | ref  | rows   | Extra       |
+----+-------------+---------------+-------+---------------+---------+---------+------+--------+-------------+
|  1 | SIMPLE      | account_login | index | idx_acc       | idx_acc | 4       | NULL | 133257 | Using index |
+----+-------------+---------------+-------+---------------+---------+---------+------+--------+-------------+

profile

+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000140 |
| checking permissions | 0.000011 |
| Opening tables       | 0.000027 |
| init                 | 0.000028 |
| System lock          | 0.000014 |
| optimizing           | 0.000009 |
| statistics           | 0.000035 |
| preparing            | 0.000028 |
| Sorting result       | 0.000006 |
| executing            | 0.000005 |
| Sending data         | 0.105595 |
| end                  | 0.000012 |
| query end            | 0.000013 |
| closing tables       | 0.000015 |
| freeing items        | 0.000026 |
| cleaning up          | 0.000034 |
+----------------------+----------+

explain select _auto_id from account_login group by _auto_id   时间0.11s
explain select _auto_id from account_login group by _auto_id order by null  时间0.11s
在使用索情况下,因为使用了索引自身的有序性,所以不需MySQL再次创建临时表(create sort index)进行排序,可以直接输出有序结果,两者的计算时间相同。

正常使用场景效率分析

mysql> explain select _auto_id,max(date) from account_login group by _auto_id;
没有索引
用时 3.16s

+----+-------------+---------------+------+---------------+------+---------+------+--------+---------------------------------+
| id | select_type | table         | type | possible_keys | key  | key_len | ref  | rows   | Extra                           |
+----+-------------+---------------+------+---------------+------+---------+------+--------+---------------------------------+
|  1 | SIMPLE      | account_login | ALL  | NULL          | NULL | NULL    | NULL | 133257 | Using temporary; Using filesort |
+----+-------------+---------------+------+---------------+------+---------+------+--------+---------------------------------+

mysql> show profile;

+---------------------------+----------+
| Status                    | Duration |
+---------------------------+----------+
| starting                  | 0.000111 |
| checking permissions      | 0.000010 |
| Opening tables            | 0.000018 |
| init                      | 0.000030 |
| System lock               | 0.000011 |
| optimizing                | 0.000007 |
| statistics                | 0.000014 |
| preparing                 | 0.000013 |
| Creating tmp table        | 0.000037 |
| Sorting result            | 0.000007 |
| executing                 | 0.000005 |
| Sending data              | 0.545211 |
| converting HEAP to MyISAM | 1.307225 |
| Sending data              | 0.738511 |
| Creating sort index       | 0.573640 |
| end                       | 0.000020 |
| removing tmp table        | 0.001682 |
| end                       | 0.000009 |
| query end                 | 0.000012 |
| closing tables            | 0.000016 |
| freeing items             | 0.000030 |
| logging slow query        | 0.000051 |
| cleaning up               | 0.000018 |
+---------------------------+----------+

在group by过程中,先使用sort index对group by子句进行处理,然后创建临时表,然后转换到磁盘临时表使用文件排序取出max(date)
如果group by后面列数过多(即使不排序),也是会用converting HEAP to MyISAM
converting HEAP to MyISAM 该语句表明了在执行过程中,内存临时表转变成了硬盘临时表。可以使用 tmp_table_size,MAX_HEAP_TABLE_SIZE来改变内存临时表的最大大小,但是在该SQL下,因为要使用文件排序,所以无论内存临时表设置多大,都会进行内存临时表到文件临时表的转变。

有索引情况
时间 0.31s
mysql>  explain select _auto_id,max(date) from account_login group by _auto_id;

+----+-------------+---------------+-------+---------------+---------+---------+------+--------+-------+
| id | select_type | table         | type  | possible_keys | key     | key_len | ref  | rows   | Extra |
+----+-------------+---------------+-------+---------------+---------+---------+------+--------+-------+
|  1 | SIMPLE      | account_login | index | idx_acc       | idx_acc | 4       | NULL | 133257 | NULL  |
+----+-------------+---------------+-------+---------------+---------+---------+------+--------+-------+

profile

+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000109 |
| checking permissions | 0.000010 |
| Opening tables       | 0.000022 |
| init                 | 0.000031 |
| System lock          | 0.000012 |
| optimizing           | 0.000007 |
| statistics           | 0.000021 |
| preparing            | 0.000022 |
| Sorting result       | 0.000006 |
| executing            | 0.000005 |
| Sending data         | 0.314817 |
| end                  | 0.000024 |
| query end            | 0.000015 |
| closing tables       | 0.000032 |
| freeing items        | 0.000042 |
| cleaning up          | 0.000023 |
+----------------------+----------+
在有索引的情况下,仅仅靠索引本身就完成了全部需求。

distinct进行分析

explain select distinct(_auto_id) from account_login;

+----+-------------+---------------+------+---------------+------+---------+------+--------+-----------------+
| id | select_type | table         | type | possible_keys | key  | key_len | ref  | rows   | Extra           |
+----+-------------+---------------+------+---------------+------+---------+------+--------+-----------------+
|  1 | SIMPLE      | account_login | ALL  | NULL          | NULL | NULL    | NULL | 133257 | Using temporary |
+----+-------------+---------------+------+---------------+------+---------+------+--------+-----------------+

mysql> show profile;

+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000087 |
| checking permissions | 0.000009 |
| Opening tables       | 0.000016 |
| init                 | 0.000016 |
| System lock          | 0.000011 |
| optimizing           | 0.000007 |
| statistics           | 0.000013 |
| preparing            | 0.000014 |
| Creating tmp table   | 0.000026 |
| executing            | 0.000006 |
| Sending data         | 0.221214 |
| end                  | 0.000024 |
| removing tmp table   | 0.000190 |
| end                  | 0.000011 |
| query end            | 0.000014 |
| closing tables       | 0.000019 |
| freeing items        | 0.000036 |
| cleaning up          | 0.000024 |
+----------------------+----------+

select distinct _auto_id,sid,uid from account_login;
+---------------------------+----------+
| Status                    | Duration |
+---------------------------+----------+
| starting                  | 0.000095 |
| checking permissions      | 0.000010 |
| Opening tables            | 0.000019 |
| init                      | 0.000019 |
| System lock               | 0.000010 |
| optimizing                | 0.000006 |
| statistics                | 0.000015 |
| preparing                 | 0.000016 |
| Creating tmp table        | 0.000030 |
| executing                 | 0.000006 |
| Sending data              | 0.529466 |
| converting HEAP to MyISAM | 1.928813 |
| Sending data              | 0.157253 |
| end                       | 0.000020 |
| removing tmp table        | 0.002778 |
| end                       | 0.000009 |
| query end                 | 0.000012 |
| closing tables            | 0.000016 |
| freeing items             | 0.000031 |
| logging slow query        | 0.000062 |
| cleaning up               | 0.000033 |
+---------------------------+----------+

发现distinct和没有排序的group by几乎是一样的,并且在进行多列的去重的时候也使用了 converting HEAP to MyISAM进行汇总

总结:
create sort index 使用内存临时表进行分块排序,分块排序后再进入磁盘进行汇总排序
converting HEAP to MyISAM 是进入硬盘进行汇总排序,如果group by数据列过多,即使不排序,也需要使用磁盘临时表进行汇总数据。
group by的主要消耗是在临时表排序阶段,而不是分组阶段。
所以制约group by性能的问题,就是临时表+排序,尽量减少磁盘排序,较少磁盘临时表的创建,是比较有用的处理办法。
最好的办法就是在group by条件后,添加索引或者复合索引,这样MySQL就会利用索引完成排序,分组

原文:https://blog.csdn.net/u013983450/article/details/52190699

group by与distinct效率分析及优化措施的更多相关文章

  1. Mysql 多表联合查询效率分析及优化

    1. 多表连接类型 1. 笛卡尔积(交叉连接) 在MySQL中可以为CROSS JOIN或者省略CROSS即JOIN,或者使用','  如: SELECT * FROM table1 CROSS JO ...

  2. SQL -去重Group by 和Distinct的效率

    经实际测试,同等条件下,5千万条数据,Distinct比Group by效率高,但是,这是有条件的,这五千万条数据中不重复的仅仅有三十多万条,这意味着,五千万条中基本都是重复数据. 为了验证,重复数据 ...

  3. 重新学习MySQL数据库5:根据MySQL索引原理进行分析与优化

    重新学习MySQL数据库5:根据MySQL索引原理进行分析与优化 一:Mysql原理与慢查询 MySQL凭借着出色的性能.低廉的成本.丰富的资源,已经成为绝大多数互联网公司的首选关系型数据库.虽然性能 ...

  4. Mysql慢SQL分析及优化

    为何对慢SQL进行治理 从数据库角度看:每个SQL执行都需要消耗一定I/O资源,SQL执行的快慢,决定资源被占用时间的长短.假设总资源是100,有一条慢SQL占用了30的资源共计1分钟.那么在这1分钟 ...

  5. MYSQL索引结构原理、性能分析与优化

    [转]MYSQL索引结构原理.性能分析与优化 第一部分:基础知识 索引 官方介绍索引是帮助MySQL高效获取数据的数据结构.笔者理解索引相当于一本书的目录,通过目录就知道要的资料在哪里, 不用一页一页 ...

  6. mysql性能优化-慢查询分析、优化索引和配置

    一.优化概述 二.查询与索引优化分析 1性能瓶颈定位 Show命令 慢查询日志 explain分析查询 profiling分析查询 2索引及查询优化 三.配置优化 1)      max_connec ...

  7. 高性能Linux服务器 第10章 基于Linux服务器的性能分析与优化

    高性能Linux服务器 第10章    基于Linux服务器的性能分析与优化 作为一名Linux系统管理员,最主要的工作是优化系统配置,使应用在系统上以最优的状态运行.但硬件问题.软件问题.网络环境等 ...

  8. in和exists的区别与SQL执行效率分析

    可总结为:当子查询表比主查询表大时,用Exists:当子查询表比主查询表小时,用in SQL中in可以分为三类: 1.形如select * from t1 where f1 in ('a','b'), ...

  9. [转]mysql性能优化-慢查询分析、优化索引和配置

    一. 优化概述 MySQL数据库是常见的两个瓶颈是CPU和I/O的瓶颈,CPU在饱和的时候一般发生在数据装入内存或从磁盘上读取数据时候.磁盘I/O瓶颈发生在装入数据远大于内存容量的时候,如果应用分布在 ...

随机推荐

  1. git---gui使用

    1.登陆的命令: git config –global user.email "1455971532@qq.com" git config –global user.name &q ...

  2. sqlserver创建存储过程返回table

    --创建存储过程test create procedure [dbo].[test] ( @I_MTR NVARCHAR (MAX), @I_TYPE NVARCHAR (MAX), @I_FAC N ...

  3. poj3083 Children of the Candy Corn BFS&&DFS

    Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11215   Acce ...

  4. chrome+postman测试rest请求

    1.在chrome安装postman 2.在chrome打开postman 浏览器输入:chrome://apps/ 选择你的postman 3.在你看到的postman中 normal中可以看到参数 ...

  5. LAMP第二部分apache的配置

    1. 下载discuz! mkdir /data/wwwcd /data/wwwmv /root/Discuz_X3.2_SC_GBK.zip .wget http://download.comsen ...

  6. Log4j官方文档翻译(七、日志格式化)

    apache log4j提供各种layout对象,然后根据自己指定的layouts对象转化日志信息.通常来说都是应用量身定制layout对象转换信息格式. 所有的layout对象从Appender对象 ...

  7. BZOJ3295 [Cqoi2011]动态逆序对 【CDQ分治】

    题目 对于序列A,它的逆序对数定义为满足i 输入格式 输入第一行包含两个整数n和m,即初始元素的个数和删除的元素个数.以下n行每行包含一个1到n之间的正整数,即初始排列.以下m行每行一个正整数,依次为 ...

  8. 【bzoj4699】树上的最短路(树剖+线段树优化建图)

    题意 给你一棵 $n$ 个点 $n-1$ 条边的树,每条边有一个通过时间.此外有 $m$ 个传送条件 $(x_1,y_1,x_2,y_2,c)$,表示从 $x_1$ 到 $x_2$ 的简单路径上的点可 ...

  9. BZOJ3612 [Heoi2014]平衡 整数划分

    [Heoi2014]平衡 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 348  Solved: 273[Submit][Status][Discus ...

  10. linux下头文件

    aio.h 异步I/Oassert.h 验证程序断言complex 复数类complex.h 复数处理cpio.h cpio归档值ctype.h 字符类型dirent.h 目录项,opendir(), ...