• mysql下distinct和group by区别对比


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

  • 相关阅读:
    关于.net core https支持的整理
    数据库动态字段 个人设计的思考
    解决PowerDesigner中table的code长度限制和column的code长度限制
    Git内网服务器搭建
    解决.net core读取appSetting.json文件中文字符乱码
    excel 转换成pdf 总结
    .net生成荣誉证书
    Adobe Acrobat DC 安装
    swagger生成错误问题 汇总解决
    svn或git 提交文件排除
  • 原文地址:https://www.cnblogs.com/xiaoleiel/p/8324066.html
Copyright © 2020-2023  润新知