MySQL去重該使用distinct還是group by?
前言
關(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%';
查看是否開啟查詢緩存決定于query_cache_type
和query_cache_size
。
- 方法一:關(guān)閉查詢緩存需要找到my.ini,修改
query_cache_type
需要修改C:\ProgramData\MySQL\MySQL Server 5.7\my.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)的查詢方式是不會進(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; 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少量種類多
的數(shù)據(jù)
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大量種類多
的數(shù)據(jù)
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);
測試階段
驗(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);
由此可見:少量種類少數(shù)據(jù)下,未加索引,distinct和group by性能相差無幾。
加索引
alter table t0 add index `a_t0_index`(a);
執(zhí)行上述類似查詢后
由此可見:少量種類少數(shù)據(jù)下,加索引,distinct和group by性能相差無幾。
驗(yàn)證少量種類多數(shù)據(jù)未加索引
執(zhí)行上述類似未加索引查詢后
由此可見:少量種類多數(shù)據(jù)下,未加索引,distinct比group by性能略高,差距并不大。
加索引
alter table t1 add index `a_t1_index`(a);
執(zhí)行類似未加索引查詢后
由此可見:少量種類多數(shù)據(jù)下,加索引,distinct和group by性能相差無幾。
驗(yàn)證大量種類多數(shù)據(jù)
未加索引
SELECT count(1) FROM t2;
執(zhí)行上述類似未加索引查詢后
由此可見:大量種類多數(shù)據(jù)下,未加索引,distinct比group by性能高。
加索引
alter table t2 add index `a_t2_index`(a);
執(zhí)行上述類似加索引查詢后
由此可見:大量種類多數(shù)據(jù)下,加索引,distinct和group by性能相差無幾。
總結(jié)
去重場景下,未加索引時(shí),更偏向于使用distinct,而加索引時(shí),distinct和group by兩者都可以使用。
總結(jié)
到此這篇關(guān)于MySQL去重該使用distinct還是group by?的文章就介紹到這了,更多相關(guān)mysql 去重distinct group by內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mysql-connector-java與Mysql、Java的對應(yīng)版本問題
這篇文章主要介紹了mysql-connector-java與Mysql、Java的對應(yīng)版本問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11windows下mysql數(shù)據(jù)庫主從配置教程
這篇文章主要為大家詳細(xì)介紹了windows下mysql數(shù)據(jù)庫主從配置教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05MySQL安裝與創(chuàng)建用戶操作(新手入門指南)
這篇文章主要為大家介紹了MySQL安裝與創(chuàng)建用戶的使用講解是非常適合小白新手的入門學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05非常實(shí)用的MySQL函數(shù)全面總結(jié)詳解示例分析教程
這篇文章主要為大家介紹了非常實(shí)用的MySQL函數(shù)的詳解示例分析,文中全面的概括了MySQL函數(shù),并進(jìn)行了詳細(xì)的示例講解,有需要的朋友可以借鑒參考下2021-10-10centos 7系統(tǒng)下編譯安裝 mysql5.7教程
因?yàn)镸ysql5.7的更新特性還是非常多,所以這篇文章就給大家介紹以下在centos上面編譯安裝mysql5.7的教程。本文給大家介紹的步驟還是相對來說比較詳細(xì)的,相信對大家具有一定的參考借鑒價(jià)值,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-11-11MySQL刪除數(shù)據(jù),表文件大小依然沒變的原因
這篇文章主要介紹了MySQL刪除數(shù)據(jù),表文件大小依然沒變的原因,幫助大家更好的理解MySQL中的數(shù)據(jù)表,感興趣的朋友可以了解下2020-10-10mysql 5.7.18 Installer安裝下載圖文教程
這篇文章主要為大家詳細(xì)介紹了mysql 5.7.18 Installer安裝下載圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09解決windows service 2012阿里云服務(wù)器在搭建mysql時(shí)缺少msvcr100.dll文件的問題
這篇文章主要介紹了解決windows service 2012阿里云服務(wù)器在搭建mysql時(shí)缺少msvcr100.dll文件的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02