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

DISTINCT 实际上和 GROUP BY 操作的实现非常相似,只不过是在 GROUP BY 之后的每组中只取出一条记录而已。所以,DISTINCT 的实现和 GROUP BY 的实现也基本差不多,没有太大的区别,同样可以通过松散索引扫描或者是紧凑索引扫描来实现。

那DISTINCT 和GROUP BY哪个效率更高?

DISTINCT操作只需要找出所有不同的值就可以了。而GROUP BY操作还要为其他聚集函数进行准备工作。从这一点上将,GROUP BY操作做的工作应该比DISTINCT所做的工作要多一些。
但实际上,GROUP BY 效率会更高点,为什么呢?对于DISTINCT操作,它会读取了所有记录,而GROUP BY需要读取的记录数量与分组的组数量一样多,也就是说比实际存在的记录数目要少很多。

例子  aa表       a          b
                  123        10
                  123        12
                   1234       11
                   1234         14
首先 group 是用来分组的  不是过滤重复项的。重复项删除语句 DISTINCT用这个 。 select  DISTINCT(a) from aa
结果就是     a
                  123
                    1234

group by用来分组的

select a, sum(b) from aa group by a

sum意思是总和。结果就是
     a         b
    123      22
    1234    25
语句的目的是以a为目标 需要知道 相同名字的物品   在b列一共有多少数量总和

select a,count(b)  from aa group by a

count 意思行数总和   结果就是
      a         b
    123      2
    1234    2

语句目的是  相同名字的物品 一共有几行

MySQL中distinct和group by性能比较

测试过程:

准备一张测试表

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 ;

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

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

调用存储过程插入数据

1 call p_test(100000);

开始测试:(不加索引)

select distinct num from test_test; 
select num from test_test group by num; 
 
[SQL] select distinct num from test_test;
受影响的行: 0
时间: 0.078ms

select num from test_test group by num;
受影响的行: 0
时间: 0.031ms

二、num字段上创建索引

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

再次查询

select distinct num from test_test; 
select num from test_test group by num; 
[SQL] select distinct num from test_test;
受影响的行: 0
时间: 0.000ms
  
select num from test_test group by num;
受影响的行: 0
时间: 0.000ms

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

我们转到命令行下 测试

mysql> set profiling=1;
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.00072550 | select distinct(num) from test_test    |
|        2 | 0.00071650 | select num from test_test group by num |
+----------+------------+----------------------------------------+

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

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

再来对比 :distinct  和 group by

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

mysql下distinct和group by区别对比的更多相关文章

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

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

  2. MySQL 里面的Where 和Having和Count 和distinct和Group By对比

    mysql> select accid as uid,date(datetime) AS datetime from game.logLogin GROUP BY accid HAVING da ...

  3. Oracle与MySQL的区别对比

    本文对数据库Oracle与MySQL进行了区别对比,其中从并发性.一致性.事务.数据持久性等十三方面进行了对比. 本文摘自 51cto 一.并发性 并发性是oltp数据库最重要的特性,但并发涉及到资源 ...

  4. mysql distinct跟group by性能

    mysql distinct和group by性能   1,测试前的准备 //准备一张测试表 mysql> CREATE TABLE `test_test` ( ->   `id` int ...

  5. 开源数据库 H2, HSQLDB, DERBY, PostgreSQL, MySQL区别/对比图表( 附加翻译) h2数据库

    开源数据库 H2, HSQLDB, DERBY, PostgreSQL, MySQL区别/对比图表 浪天涯博主翻译: referential integrity 参考完整性transactions 事 ...

  6. (转)数据库 distinct 和 group by 的区别

    这两者本质上应该没有可比性,distinct 取出唯一列,group by 是分组,但有时候在优化的时候,在没有聚合函数的时候,他们查出来的结果也一样. 举例来说可能方便一点. A表 id num a ...

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

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

  8. 总结distinct、group by 、row_number()over函数用法及区别

    distinct和group by 是一样的,查询去重,只能是全部重复的,也可以理解为针对单例,因为一行有一个字段不一样,他们就会认为这两行内容是不重复的.但是使用row_number()over这个 ...

  9. mysql中distinct的用法

    本事例实验用表task,结构如下 MySQL> desc task; +-------------+------------+------+-----+-------------------+- ...

随机推荐

  1. Wp8 读取手机信息

    /// <summary> /// 获取系统信息 /// </summary> private void GetSystemInfo() { lblMsg.Text = str ...

  2. STM32F407 ADC 个人笔记

    1. ADC概述(STM32F4xx系列) 3 个 ADC 可分别独立使用 也可使用双重/三重模式(提高采样率) 2 个通道组 规则通道:相当于正常运行的程序 注入通道:相当于中断(可以打断规则通道的 ...

  3. selenium之定位以及切换frame

    总有人看不明白,以防万一,先在开头大写加粗说明一下: frameset不用切,frame需层层切! 很多人在用selenium定位页面元素的时候会遇到定位不到的问题,明明元素就在那儿,用firebug ...

  4. IS-IS IGP

    is-is  是igp的一种    属于osi的协议 OSI的三层是网络层      包含两种服务  一种是面向连接服务CONS       另一种是无连接服务CLNS CLNS中包含CLNP     ...

  5. linux的自启动服务脚本的(/etc/rc.d/init.d或者其链接/etc/init.d)

    转载地址:http://www.cnblogs.com/diyunpeng/archive/2009/11/11/1600886.html Linux有自己一套完整的启动体系,抓住了linux启动的脉 ...

  6. jQuery获得页面元素的绝对/相对位置

    获取页面某一元素的绝对X,Y坐标,可以用offset()方法: var X = $('#DivID').offset().top; var Y = $('#DivID').offset().left; ...

  7. BZOJ 4753 [Jsoi2016]最佳团体 ——01分数规划 树形DP

    要求比值最大,当然用分数规划. 二分答案,转化为选取一个最大的联通块使得它们的和大于0 然后我们直接DP. 复杂度$O(n^2\log {n})$ #include <map> #incl ...

  8. 【2018.10.1】【JSOI2016】最佳团体(bzoj4753)

    一看到“比值”最大(性价比最高)就知道跟分数规划有关系了.(这里讲过分数规划) 然后看到 要选一个候选人 必须选他的前置,画画图就知道是一棵树. 所以这道题是二分比值,每个点的权值就是战斗力-费用*比 ...

  9. FreeMarker常用语法学习

    1.API网址 http://freemarker.sourceforge.net/docs/ 2.一个Table的例子 freemarker 对表格的控制 这里将所有需要在一个区域显示到数据全部ad ...

  10. 【SPOJ687&POJ3693】Maximum repetition substring(后缀数组)

    题意: n<=1e5 思路: From http://hzwer.com/6152.html 往后匹配多远 r 用ST表求lcp即可...往前 l 就把串反过来再做一下.. 但是有可能求出来的最 ...