• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    MySQL去重该使用distinct还是group by?

    前言

    关于group by 与distinct 性能对比:网上结论如下,不走索引少量数据distinct性能更好,大数据量group by 性能好,走索引group by性能好。走索引时分组种类少distinct快。关于网上的结论做一次验证。

    准备阶段屏蔽查询缓存

    查看MySQL中是否设置了查询缓存。为了不影响测试结果,需要关闭查询缓存。

    show variables like '%query_cache%';

    查看是否开启查询缓存决定于query_cache_typequery_cache_size

    set global query_cache_size = 0;

    方法三:如果你不想关闭查询缓存,也可以在使用RESET QUERY CACHE

    现在测试环境中query_cache_type=2代表按需进行查询缓存,默认的查询方式是不会进行缓存,如需缓存则需要在查询语句中加上sql_cache

    数据准备

    t0表存放10W少量种类少的数据

    drop table if exists t0;
    create table t0(
    id bigint primary key auto_increment,
    a varchar(255) not null
    ) engine=InnoDB default charset=utf8mb4 collate=utf8mb4_bin;
    1
    2
    3
    4
    5
    drop procedure insert_t0_simple_category_data_sp;
    delimiter //
    create procedure insert_t0_simple_category_data_sp(IN num int)
    begin
    set @i = 0;
    while @i  num do
    	insert into t0(a) value(truncate(@i/1000, 0));
     set @i = @i + 1;
    end while;
    end
    //
    call insert_t0_simple_category_data_sp(100000);

    t1表存放1W少量种类多的数据

    drop table if exists t1;
    create table t1 like t0;
    1
    2
    drop procedure insert_t1_complex_category_data_sp;
    delimiter //
    create procedure insert_t1_complex_category_data_sp(IN num int)
    begin
    set @i = 0;
    while @i  num do
    	insert into t1(a) value(truncate(@i/10, 0));
     set @i = @i + 1;
    end while;
    end
    //
    call insert_t1_complex_category_data_sp(10000);

    t2表存放500W大量种类多的数据

    drop table if exists t2;
    create table t2 like t1;
    1
    2
    drop procedure insert_t2_complex_category_data_sp;
    delimiter //
    create procedure insert_t2_complex_category_data_sp(IN num int)
    begin
    set @i = 0;
    while @i  num do
    	insert into t1(a) value(truncate(@i/10, 0));
     set @i = @i + 1;
    end while;
    end
    //
    call insert_t2_complex_category_data_sp(5000000);

    测试阶段

    验证少量种类少数据

    未加索引

    set profiling = 1;
    select distinct a from t0;
    show profiles;
    select a from t0 group by a;
    show profiles;
    alter table t0 add index `a_t0_index`(a);

    由此可见:少量种类少数据下,未加索引,distinct和group by性能相差无几。

    加索引

    alter table t0 add index `a_t0_index`(a);

    执行上述类似查询后

    由此可见:少量种类少数据下,加索引,distinct和group by性能相差无几。

    验证少量种类多数据未加索引

    执行上述类似未加索引查询后

    由此可见:少量种类多数据下,未加索引,distinct比group by性能略高,差距并不大。

    加索引

    alter table t1 add index `a_t1_index`(a);

    执行类似未加索引查询后

    由此可见:少量种类多数据下,加索引,distinct和group by性能相差无几。

    验证大量种类多数据

    未加索引

    SELECT count(1) FROM t2;


    执行上述类似未加索引查询后


    由此可见:大量种类多数据下,未加索引,distinct比group by性能高。

    加索引

    alter table t2 add index `a_t2_index`(a);

    执行上述类似加索引查询后

    由此可见:大量种类多数据下,加索引,distinct和group by性能相差无几。

    总结

    性能比 少量种类少 少量种类多 大量种类多未加索引相差无几distinct略优distinct更优加索引相差无几相差无几相差无几

    去重场景下,未加索引时,更偏向于使用distinct,而加索引时,distinct和group by两者都可以使用。

    总结

    到此这篇关于MySQL去重该使用distinct还是group by?的文章就介绍到这了,更多相关mysql 去重distinct group by内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    您可能感兴趣的文章:
    • MySQL优化GROUP BY(松散索引扫描与紧凑索引扫描)
    • MySQL优化GROUP BY方案
    • mysql group by 对多个字段进行分组操作
    • 基于mysql实现group by取各分组最新一条数据
    • Mysql5.7及以上版本 ONLY_FULL_GROUP_BY报错的解决方法
    • MySQL中使用group by 是总是出现1055的错误(推荐)
    • mysql case when group by 实例详解
    • Mysql升级到5.7后遇到的group by查询问题解决
    • MySQL group by语句如何优化
    上一篇:MySQL对window函数执行sum函数可能出现的一个Bug
    下一篇:mysql迁移至8.0时的注意事项(小结)
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

    MySQL去重该使用distinct还是group by? MySQL,去重,该,使用,distinct,