mysql distinct和group by性能  
1,测试前的准备
  1. //准备一张测试表
  2. mysql> CREATE TABLE `test_test` (
  3. ->   `id` int(11) NOT NULL auto_increment,
  4. ->   `num` int(11) NOT NULL default '0',
  5. ->   PRIMARY KEY  (`id`)
  6. -> ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
  7. Query OK, 0 rows affected (0.05 sec)
  8. mysql> delimiter ||  //改变mysql命令结束符为||
  9. //建个储存过程向表中插入10W条数据
  10. mysql> create procedure p_test(pa int(11))
  11. -> begin
  12. ->
  13. ->  declare max_num int(11) default 100000;
  14. ->  declare i int default 0;
  15. ->  declare rand_num int;
  16. ->
  17. ->  select count(id) into max_num from test_test;
  18. ->
  19. ->  while i < pa do
  20. ->          if max_num < 100000 then
  21. ->                  select cast(rand()*100 as unsigned) into rand_num;
  22. ->                  insert into test_test(num)values(rand_num);
  23. ->          end if;
  24. ->          set i = i +1;
  25. ->  end while;
  26. -> end||
  27. Query OK, 0 rows affected (0.00 sec)
  28. mysql> call p_test(100000)||
  29. Query OK, 1 row affected (5.66 sec)
  30. mysql> delimiter ;//改变mysql命令结束符为;
  31. mysql> select count(id) from test_test;  //数据都进去了
  32. +-----------+
  33. | count(id) |
  34. +-----------+
  35. |    100000 |
  36. +-----------+
  37. 1 row in set (0.00 sec)
  38. mysql> show variables like "%pro%";   //查看一下,记录执行的profiling是不是开启动了,默认是不开启的
  39. +---------------------------+-------+
  40. | Variable_name             | Value |
  41. +---------------------------+-------+
  42. | profiling                 | OFF   |
  43. | profiling_history_size    | 15    |
  44. | protocol_version          | 10    |
  45. | slave_compressed_protocol | OFF   |
  46. +---------------------------+-------+
  47. 4 rows in set (0.00 sec)
  48. mysql> set profiling=1;           //开启
  49. Query OK, 0 rows affected (0.00 sec)
//准备一张测试表
mysql> CREATE TABLE `test_test` (
->   `id` int(11) NOT NULL auto_increment,
->   `num` int(11) NOT NULL default '0',
->   PRIMARY KEY  (`id`)
-> ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Query OK, 0 rows affected (0.05 sec) mysql> delimiter ||  //改变mysql命令结束符为|| //建个储存过程向表中插入10W条数据
mysql> create procedure p_test(pa int(11))
-> begin
->
->  declare max_num int(11) default 100000;
->  declare i int default 0;
->  declare rand_num int;
->
->  select count(id) into max_num from test_test;
->
->  while i < pa do
->          if max_num < 100000 then
->                  select cast(rand()*100 as unsigned) into rand_num;
->                  insert into test_test(num)values(rand_num);
->          end if;
->          set i = i +1;
->  end while;
-> end||
Query OK, 0 rows affected (0.00 sec) mysql> call p_test(100000)||
Query OK, 1 row affected (5.66 sec) mysql> delimiter ;//改变mysql命令结束符为;
mysql> select count(id) from test_test;  //数据都进去了
+-----------+
| count(id) |
+-----------+
|    100000 |
+-----------+
1 row in set (0.00 sec) mysql> show variables like "%pro%";   //查看一下,记录执行的profiling是不是开启动了,默认是不开启的
+---------------------------+-------+
| Variable_name             | Value |
+---------------------------+-------+
| profiling                 | OFF   |
| profiling_history_size    | 15    |
| protocol_version          | 10    |
| slave_compressed_protocol | OFF   |
+---------------------------+-------+
4 rows in set (0.00 sec) mysql> set profiling=1;           //开启
Query OK, 0 rows affected (0.00 sec)

2,测试

  1. //做了4组测试
  2. mysql> select distinct(num) from test_test;
  3. mysql> select num from test_test group by num;
  4. mysql> show profiles;    //查看结果
  5. +----------+------------+-------------------------------------------+
  6. | Query_ID | Duration   | Query                                     |
  7. +----------+------------+-------------------------------------------+
  8. |        1 | 0.07298225 | select distinct(num) from test_test       |
  9. |        2 | 0.07319975 | select num from test_test group by num    |
  10. |        3 | 0.07313525 | select num from test_test group by num    |
  11. |        4 | 0.07317725 | select distinct(num) from test_test       |
  12. |        5 | 0.07275200 | select distinct(num) from test_test       |
  13. |        6 | 0.07298600 | select num from test_test group by num    |
  14. |        7 | 0.07500700 | select num from test_test group by num    |
  15. |        8 | 0.07331325 | select distinct(num) from test_test       |
  16. |        9 | 0.57831575 | create index num_index on test_test (num) |  //在这儿的时候,我加了索引
  17. |       10 | 0.00243550 | select distinct(num) from test_test       |
  18. |       11 | 0.00121975 | select num from test_test group by num    |
  19. |       12 | 0.00116550 | select distinct(num) from test_test       |
  20. |       13 | 0.00107650 | select num from test_test group by num    |
  21. +----------+------------+-------------------------------------------+
  22. 13 rows in set (0.00 sec)
//做了4组测试
mysql> select distinct(num) from test_test;
mysql> select num from test_test group by num; mysql> show profiles;    //查看结果
+----------+------------+-------------------------------------------+
| Query_ID | Duration   | Query                                     |
+----------+------------+-------------------------------------------+
|        1 | 0.07298225 | select distinct(num) from test_test       |
|        2 | 0.07319975 | select num from test_test group by num    |
|        3 | 0.07313525 | select num from test_test group by num    |
|        4 | 0.07317725 | select distinct(num) from test_test       |
|        5 | 0.07275200 | select distinct(num) from test_test       |
|        6 | 0.07298600 | select num from test_test group by num    |
|        7 | 0.07500700 | select num from test_test group by num    |
|        8 | 0.07331325 | select distinct(num) from test_test       |
|        9 | 0.57831575 | create index num_index on test_test (num) | //在这儿的时候,我加了索引
|       10 | 0.00243550 | select distinct(num) from test_test       |
|       11 | 0.00121975 | select num from test_test group by num    |
|       12 | 0.00116550 | select distinct(num) from test_test       |
|       13 | 0.00107650 | select num from test_test group by num    |
+----------+------------+-------------------------------------------+
13 rows in set (0.00 sec)

上面的1-8是4组数据,并且是没有加索引的,从中我们可以看出,distinct比group by 会好一点点

10-13是2组数据,是加了索引以后的,从中我们可以看出,group by 比distinct 会好一点点

一般情况,数据量比较大的表,关联字段都会加索引的,,并且加索引后检索时间只有以前的六分之一左右。

http://www.myexception.cn/mysql/516305.html

mysql distinct跟group by性能的更多相关文章

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

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

  2. Mysql distinct、group by

    具体业务场景:根据某些字段组合去重得到所有字段结果. 遇到的error:sql_mode=only_full_group_by. 原因是mysql配置问题. distinct: distinct这个关 ...

  3. mysql下distinct和group by区别对比

    在数据表中记录了用户验证时使用的书目,现在想取出所有书目,用DISTINCT和group by都取到了我想要的结果,但我发现返回结果排列不同,distinct会按数据存放顺序一条条显示,而group ...

  4. 44 答疑(三)--join的写法/Simple nested loop join的性能问题/Distinct和group by的性能/备库自增主键问题

    44 答疑(三) Join的写法 35节介绍了join执行顺序,加了straight_join,两个问题: --1 如果用left join,左边的表一定是驱动表吗 --2 如果两个表的join包含多 ...

  5. distinct和group by的性能比较

    distinct和group by的性能比较 当去重复的字段 的个数比较多的时候,group by 比distinct要快很多 当去重复的字符 的个数比较少的时候,distinct 比group by ...

  6. mysql数据去重复distinct、group by

    使用distinct 和group by都可以实现数据去重. select distinct 字段 group by 一般放在where条件后

  7. Mysql视图的作用及其性能分析

    定义:视图是从一个或几个基本表导出的表,它与基本表不同,是一个虚表. 作用: 1.简化操作,不用进行多表查询. 2.当不同种类的用用户共享同一个数据库时,非常灵活,(用户以不同的 方式看待同一数据. ...

  8. 高性能mysql 第6章 查询性能优化

    查询缓存: 在解析一个sql之前,如果查询缓存是打开的,mysql会去检查这个查询(根据sql的hash作为key)是否存在缓存中,如果命中的话,那么这个sql将会在解析,生成执行计划之前返回结果. ...

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

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

随机推荐

  1. 【转】iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) -- 不错不错

    原文网址:http://blog.csdn.net/totogo2010/article/details/8615940 1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手 ...

  2. jquery的一些select操作小记

    添加option $("#ID option").each(function(){ if($(this).val() == 111){ $(this).remove(); } }) ...

  3. HDU 4393 Throw nails

    Throw nails Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. JMX学习笔记(三)-MXBean

    在MBean中有只要遵循以下两个规则,我们就可以在jconsole中动态的改变MBean中的属性值 1. JMX中要定义接口必须以xxxMBean的规范定义 2. 得有类实现xxxMBean接口 例如 ...

  5. Doing Homework(HDU 1074状压dp)

    题意:给你n个要做的作业,它们的名字.期限.可完成所需天数(必须连续)在规定期限不能完成要扣分(每天一分)求做作业顺序使扣分最少. 分析:作业数量较少,用状态压缩,做到第i种作业花费的天数dp[i]. ...

  6. 单元测试之获取Spring下所有Bean

    单元测试中,针对接口的测试是必须的,但是如何非常方便的获取Spring注册的Bean呢? 如果可以获取所有的Bean,这样就可以将这个方法放到基类中,方便后面所有单元测试类的使用,具体实现如下: im ...

  7. 优分享VR开源啦,优分享VR是基于Google VR开发的一款手机VR视频资源的聚合软件

    欢迎来到优分享VR开源项目 优分享VR 开源中国Git地址: http://git.oschina.net/xumingwang/youkes_vr 优分享VR是 优分享安卓APP VR视频播放开源部 ...

  8. (原创)win7自带IIS7.5+php7.0.10安装教程(图)

    php在上周8月18日发布了PHP 7.0 (7.0.10)版本.详细下载页面http://windows.php.net/download/,根据自身电脑配置情况酌情下载版本.win7旗舰版,iis ...

  9. Python第一个入门程序

    #!usr/bin/env python3 #在UNIX上,当某程序在控制台中被引用时,该文件的头两个字节先被读入.如果这两个字节是ASCII字符 #!, #shell就会认为该文件将要由解释器执行, ...

  10. RAM, SDRAM ,ROM, NAND FLASH, NOR FLASH

    在看上面2440的内存映射的时候,对其中的有些名字,不是完全太懂,所以到网上找了相关的信息. 对于mini2440来说,SDRAM,即内存,程序运行时的地方.选择连接SDRAM的为bank6. 1)S ...