国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

MySQL去重該使用distinct還是group by?

瀏覽:4日期:2023-10-15 09:04:48

前言

關于group by 與distinct 性能對比:網上結論如下,不走索引少量數據distinct性能更好,大數據量group by 性能好,走索引group by性能好。走索引時分組種類少distinct快。關于網上的結論做一次驗證。

準備階段屏蔽查詢緩存

查看MySQL中是否設置了查詢緩存。為了不影響測試結果,需要關閉查詢緩存。

show variables like ’%query_cache%’;

MySQL去重該使用distinct還是group by?

查看是否開啟查詢緩存決定于query_cache_type和query_cache_size。

方法一:關閉查詢緩存需要找到my.ini,修改query_cache_type需要修改C:ProgramDataMySQLMySQL Server 5.7my.ini配置文件,修改query_cache_type=0或2。 方法二:設置query_cache_size為0,執行以下語句。

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;12345drop procedure insert_t0_simple_category_data_sp;delimiter //create procedure insert_t0_simple_category_data_sp(IN num int)beginset @i = 0;while @i < num doinsert 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;12drop procedure insert_t1_complex_category_data_sp;delimiter //create procedure insert_t1_complex_category_data_sp(IN num int)beginset @i = 0;while @i < num doinsert 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;12drop procedure insert_t2_complex_category_data_sp;delimiter //create procedure insert_t2_complex_category_data_sp(IN num int)beginset @i = 0;while @i < num doinsert 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);

MySQL去重該使用distinct還是group by?

由此可見:少量種類少數據下,未加索引,distinct和group by性能相差無幾。

加索引

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

執行上述類似查詢后

MySQL去重該使用distinct還是group by?

由此可見:少量種類少數據下,加索引,distinct和group by性能相差無幾。

驗證少量種類多數據未加索引

執行上述類似未加索引查詢后

MySQL去重該使用distinct還是group by?

由此可見:少量種類多數據下,未加索引,distinct比group by性能略高,差距并不大。

加索引

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

執行類似未加索引查詢后

MySQL去重該使用distinct還是group by?

由此可見:少量種類多數據下,加索引,distinct和group by性能相差無幾。

驗證大量種類多數據

未加索引

SELECT count(1) FROM t2;

MySQL去重該使用distinct還是group by?

執行上述類似未加索引查詢后

MySQL去重該使用distinct還是group by?

由此可見:大量種類多數據下,未加索引,distinct比group by性能高。

加索引

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

執行上述類似加索引查詢后

MySQL去重該使用distinct還是group by?

由此可見:大量種類多數據下,加索引,distinct和group by性能相差無幾。

總結性能比少量種類少少量種類多大量種類多未加索引相差無幾distinct略優distinct更優加索引相差無幾相差無幾相差無幾

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

總結

到此這篇關于MySQL去重該使用distinct還是group by?的文章就介紹到這了,更多相關mysql 去重distinct group by內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: MySQL 數據庫
相關文章:
主站蜘蛛池模板: 一区二区中文字幕亚洲精品 | 亚洲人成网7777777国产 | 亚洲日韩中文字幕 | 欧美日韩精品一区二区免费看 | 久久精品国产精品亚洲精品 | 欧美成人免费观看bbb | 欧美日韩一区二区在线观看视频 | 日韩成人毛片高清视频免费看 | 草草影院在线播放 | 亚洲成人xxx | 国产精选一区二区 | 99久久一区二区精品 | 成人一级片在线观看 | 欧美成人毛片 | 欧美一区二三区 | 天码毛片一区二区三区入口 | 亚洲精品成人一区二区aⅴ 亚洲精品成人一区二区www | 99久久精品久久久久久清纯 | 亚洲免费色 | 免费观看成为人视频 | 亚洲人成网站在线观看播放 | 亚洲第一网站免费视频 | 亚州免费一级毛片 | 91精品欧美一区二区三区 | 亚洲欧美一区二区三区在线观看 | 日本一区二区三区免费视频 | 性欧美videos俄罗斯 | 久草久草视频 | 成人福利在线 | 中国一级做a爰片久久毛片 中日韩欧美一级毛片 | 久久久久久久免费 | 香蕉视频在线观看黄 | 亚洲三级在线 | 欧美日本一区二区三区道 | 亚洲第一黄色网 | 欧美另类久久久精品 | 国产女人成人精品视频 | 国产精品久久久久久久午夜片 | www.久草.com| 99久久精品免费观看国产 | 暴操女人 |