在数据表中记录了用户验证时使用的书目,现在想取出所有书目,用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. wordpress 使用jquery需要主要的问题

    wordpress 使用jquery时,不能直接使用$, 而是用jQuery 代替$, 而且wordpress默认调用jquery

  2. Web开发细节搜集

    App_Data 百度百科:  App_Data文件夹应该包含应用程序的本地数据存储.它通常以文件(诸如Microsoft Access或Microsoft SQL Server Express数据库 ...

  3. Selenium WebDriver高级用法

    Selenium GitHub地址 选择合适的WebDrvier WebDriver是一个接口,它有几种实现,分别是HtmlUnitDrvier.FirefoxDriver.InternetExplo ...

  4. mvc框架简介

    1.mvc( mode  view controller)是什么?   m指业务代码,v指用户界面,c是控制器 使用MVC的目的是将M和V的实现代码分离,从而使同一个程序可以使用不同的表现形式 是一种 ...

  5. Spoj-NPC2015A Eefun Guessing Words

    Eefun Guessing Words Eefun is currently learning to read. His way of learning  is unique, by trying ...

  6. LA 3263 平面划分

    Little Joey invented a scrabble machine that he called Euler, after the great mathematician. In his ...

  7. Nk 1430 Divisors(因子数与质因数)

    Time Limit: 5000 ms    Memory Limit: 10000 kB   Total Submit : 432 (78 users)   Accepted Submit : 10 ...

  8. Codevs 2602 最短路径问题

     时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 Description 平面上有n个点(n<=100),每个点的坐标均在-10000~10000之间. ...

  9. 关于代码调试de那些事

    原文出处:http://www.wklken.me/posts/2014/11/23/how-to-debug.html 关于代码调试de那些事 1.你得明白你在做什么, 保持清醒 2.想清楚了再写代 ...

  10. plsql + 客户端 连接oracle数据库

    一. 目录结构D:\oracle\instantclient_11_2D:\oracle\instantclient_11_2\tnsnames.ora 二. 环境变量 NLS_LANG = SIMP ...