MySQL中distinct和group by性能比较[转]

之前看了网上的一些测试,感觉不是很准确,今天亲自测试了一番。得出了结论(仅在个人计算机上测试,可能不全面,仅供参考)

测试过程:

准备一张测试表

1    CREATE TABLE `test_test` (
2      `id` int(11) NOT NULL auto_increment,
3      `num` int(11) NOT NULL default '0',
4      PRIMARY KEY  (`id`)
5     ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

建个储存过程向表中插入10W条数据

01    create procedure p_test(pa int(11))
02     begin
03
04      declare max_num int(11) default 100000;
05      declare i int default 0;
06      declare rand_num int;
07
08      select count(id) into max_num from test_test;
09
10      while i < pa do
11              if max_num < 100000 then
12                      select cast(rand()*100 as unsigned) into rand_num;
13                      insert into test_test(num)values(rand_num);
14              end if;
15              set i = i +1;
16      end while;
17     end

调用存储过程插入数据

1    call p_test(100000);

开始测试:(不加索引)

01    select distinct num from test_test;
02    select num from test_test group by num;
03
04    [SQL] select distinct num from test_test;
05    受影响的行: 0
06    时间: 0.078ms
07
08    [SQL]
09    select num from test_test group by num;
10    受影响的行: 0
11    时间: 0.031ms

二、num字段上创建索引

1    ALTER TABLE `test_test` ADD INDEX `num_index` (`num`) ;

再次查询

01    select distinct num from test_test;
02    select num from test_test group by num;
03    [SQL] select distinct num from test_test;
04    受影响的行: 0
05    时间: 0.000ms
06
07    [SQL]
08    select num from test_test group by num;
09    受影响的行: 0
10    时间: 0.000ms

这时候我们发现时间太小了 0.000秒都无法精确了。

我们转到命令行下 测试

01    mysql> set profiling=1;
02    mysql> select distinct(num) from test_test;
03    mysql> select num from test_test group by num;
04    mysql> show profiles;
05    +----------+------------+----------------------------------------+
06    | Query_ID | Duration   | Query                                  |
07    +----------+------------+----------------------------------------+
08    |        1 | 0.00072550 | select distinct(num) from test_test    |
09    |        2 | 0.00071650 | select num from test_test group by num |
10    +----------+------------+----------------------------------------+

加了索引之后 distinct 比没加索引的 distinct 快了 107倍。

加了索引之后 group by 比没加索引的 group by 快了 43倍。

再来对比 :distinct  和 group by

不管是加不加索引 group by 都比 distinct 快。因此使用的时候建议选 group by

转载自https://www.cnblogs.com/zox2011/archive/2012/09/12/2681797.html

MySQL中distinct和group by性能比较相关推荐

  1. MySQL中distinct和group by性能比较[转]

    之前看了网上的一些测试,感觉不是很准确,今天亲自测试了一番.得出了结论(仅在个人计算机上测试,可能不全面,仅供参考) 测试过程: 准备一张测试表 1 CREATE TABLE `test_test` ...

  2. distinct mysql性能_MySQL中distinct和group by性能比较

    MySQL中distinct和group by性能比较[转] 之前看了网上的一些测试,感觉不是很准确,今天亲自测试了一番.得出了结论(仅在个人计算机上测试,可能不全面,仅供参考) 测试过程: 准备一张 ...

  3. mysql+distinct+max_MySQL 中 distinct 和 group by 性能比较-Fun言

    看了网上的一些测试,感觉不是很准确,今天亲自测试了一番.得出了结论 仅在个人计算机上测试,可能不全面,仅供参考 测试过程 准备一张测试表 CREATE TABLE `test_test` ( `id` ...

  4. distinct mysql性能_MySQL中distinct与group by之间的性能进行比较

    最近在网上看到了一些测试,感觉不是很准确,今天亲自测试了一番.得出了结论,测试过程在个人计算机上,可能不够全面,仅供参考. 测试过程: 准备一张测试表 CREATE TABLE `test_test` ...

  5. mysql的group语句_MySQL中distinct与group by语句的一些比较及用法讲解

    这篇文章主要介绍了MySQL中distinct与group by的一些比较及用法讲解,二者在查询操作中的用法有很多相似之处,需要的朋友可以参考下 在数据表中记录了用户验证时使用的书目,现在想取出所有书 ...

  6. sql 中distinct和group by

    sql中distinct和group by都可以用于去重复,distinct是将重复的行从结果中出去,group by是按指定的列分组.区别是group by 中可以包含聚合函数. 如下所示的表结构, ...

  7. MySQL中distinct和distinctrow的区别

    引用官方文档的内容MySQL中distinct和distinctrow的区别 原文:The ALL and DISTINCT modifiers specify whether duplicate r ...

  8. mysql+distinct+max_MySQL中distinct与group by之间的性能进行比较

    最近在网上看到了一些测试,感觉不是很准确,今天亲自测试了一番.得出了结论,测试过程在个人计算机上,可能不够全面,仅供参考. 测试过程: 准备一张测试表 CREATE TABLE `test_test` ...

  9. mysql中distinct走索引吗_MySQL中索引优化distinct语句及distinct的多字段操作

    MySQL通常使用GROUPBY(本质上是排序动作)完成DISTINCT操作,如果DISTINCT操作和ORDERBY操作组合使用,通常会用到临时表.这样会影响性能. 在一些情况下,MySQL可以使用 ...

最新文章

  1. C++:名字空间的使用
  2. fastjson 不转某些字段_fastjson 过滤不需要的字段或者只要某些字段
  3. Java 接口和继承
  4. r语言向量代码如何创建函数c,R中向量基本操作:创建、运算、访问
  5. MySql学习19-----用户管理
  6. 【Java线程】线程同步—synchronized Lock
  7. samp服务器显示,如何测量我的(SAMP)服务器的带宽使用情况?
  8. 毕业设计之时钟模块设计DS1302
  9. CMSIS-DSP lib 矩阵运算示例和源码
  10. c语言———链表的创建
  11. android p 预览版壁纸,Android P预览版加入黑暗模式主题 iOS啥时候才能有呢?
  12. 噩梦遇地震,醒后忆岁月
  13. JS 测试网络速度与网络延迟
  14. Java C#分析WAV音频文件1Khz是否有声音
  15. [linux] linux sed命令删除一行/多行
  16. VMWare 虚拟机如何通过新增挂载点的方式扩展磁盘空间
  17. FPS游戏开发(3)
  18. win10系统Cortana搜索功能失效解决方法
  19. win11英文版安装中文输入法中文语言包切换到中文版
  20. Nacos 与其他注册中心对比

热门文章

  1. 最大最小背光亮度修改
  2. 【C#】byte[]数据转化相关操作
  3. Chrome在302重定向的时候对原请求产生2次请求的问题说明
  4. [jQuery1.9]Cannot read property ‘msie’ of undefined错误的解决方法
  5. DataGrid多层表头设计
  6. linux抓包命令tcptrace,每天学习一个命令:tcpdump 命令行下抓包
  7. php 小程序 活动弹幕,小程序:弹幕效果的消息提示
  8. (85)Vivado 多周期路径约束情况
  9. (20)Zynq FPGA Quad-SPI闪存控制器介绍
  10. java enum 泛型,Java Enum作为Enum中的泛型类型