av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

前言

關(guān)于group by 與distinct 性能對比:網(wǎng)上結(jié)論如下,不走索引少量數(shù)據(jù)distinct性能更好,大數(shù)據(jù)量group by 性能好,走索引group by性能好。走索引時(shí)分組種類少distinct快。關(guān)于網(wǎng)上的結(jié)論做一次驗(yàn)證。

準(zhǔn)備階段屏蔽查詢緩存

查看MySQL中是否設(shè)置了查詢緩存。為了不影響測試結(jié)果,需要關(guān)閉查詢緩存。

show variables like ’%query_cache%’;

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

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

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

set global query_cache_size = 0;

方法三:如果你不想關(guān)閉查詢緩存,也可以在使用RESET QUERY CACHE。

現(xiàn)在測試環(huán)境中query_cache_type=2代表按需進(jìn)行查詢緩存,默認(rèn)的查詢方式是不會(huì)進(jìn)行緩存,如需緩存則需要在查詢語句中加上sql_cache。

數(shù)據(jù)準(zhǔn)備

t0表存放10W少量種類少的數(shù)據(jù)

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少量種類多的數(shù)據(jù)

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大量種類多的數(shù)據(jù)

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);

測試階段

驗(yàn)證少量種類少數(shù)據(jù)

未加索引

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?

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

加索引

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

執(zhí)行上述類似查詢后

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

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

驗(yàn)證少量種類多數(shù)據(jù)未加索引

執(zhí)行上述類似未加索引查詢后

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

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

加索引

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

執(zhí)行類似未加索引查詢后

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

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

驗(yàn)證大量種類多數(shù)據(jù)

未加索引

SELECT count(1) FROM t2;

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

執(zhí)行上述類似未加索引查詢后

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

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

加索引

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

執(zhí)行上述類似加索引查詢后

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

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

總結(jié)性能比少量種類少少量種類多大量種類多未加索引相差無幾distinct略優(yōu)distinct更優(yōu)加索引相差無幾相差無幾相差無幾

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

總結(jié)

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

標(biāo)簽: MySQL 數(shù)據(jù)庫
相關(guān)文章:
主站蜘蛛池模板: 日本亚洲欧美 | 国产欧美日韩二区 | 99免费在线视频 | 久久精品| 99热精品在线 | 亚洲欧美日韩精品久久亚洲区 | 久久精品一二三影院 | 欧美极品在线 | 成人a免费| 久久精品网 | 精品一区二区电影 | 在线视频亚洲 | 日韩亚洲一区二区 | 一区二区三区国产 | 中文字幕在线视频一区二区三区 | 亚洲精品一区二区网址 | 国产精品色一区二区三区 | 亚洲精品1区 | 日本三级全黄三级三级三级口周 | 亚洲欧美日韩一区二区 | 久久99久久99精品免视看婷婷 | 在线免费观看成年人视频 | 国内精品成人 | 国产一区二区三区四区 | 免费精品视频 | 中文字幕国产视频 | 一区二区日韩 | 欧美www在线 | 国产精品特级片 | 国产精品国产三级国产aⅴ中文 | 国产精品久久久久久久久动漫 | 欧美黑人一级爽快片淫片高清 | 久久99深爱久久99精品 | 久久成人久久 | 国产在线观 | 99精品视频免费观看 | 欧美一区二 | 日本精品视频一区二区 | 国产精品国产三级国产aⅴ原创 | 狠狠做六月爱婷婷综合aⅴ 国产精品视频网 | 亚洲精品国产偷自在线观看 |