濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > MySQL去重該使用distinct還是group by?

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

熱門(mén)標(biāo)簽:400電話到哪辦理優(yōu)惠 鄭州網(wǎng)絡(luò)外呼系統(tǒng)價(jià)錢(qián) 怎么更改高德地圖標(biāo)注 電話機(jī)器人是電腦呼號(hào)嗎 云南大數(shù)據(jù)外呼系統(tǒng) 上海市三維地圖標(biāo)注 博樂(lè)電銷機(jī)器人 南寧外呼系統(tǒng)招商 機(jī)器人打電銷電話

前言

關(guān)于group by 與distinct 性能對(duì)比:網(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è)置了查詢緩存。為了不影響測(cè)試結(jié)果,需要關(guān)閉查詢緩存。

show variables like '%query_cache%';

查看是否開(kāi)啟查詢緩存決定于query_cache_typequery_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í)行以下語(yǔ)句。
set global query_cache_size = 0;

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

現(xiàn)在測(cè)試環(huán)境中query_cache_type=2代表按需進(jìn)行查詢緩存,默認(rèn)的查詢方式是不會(huì)進(jìn)行緩存,如需緩存則需要在查詢語(yǔ)句中加上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);

測(cè)試階段

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

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

加索引

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

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

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

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

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

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

加索引

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

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

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

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

未加索引

SELECT count(1) FROM t2;


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


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

加索引

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

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

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

總結(jié)

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

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

總結(jié)

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

您可能感興趣的文章:
  • MySQL優(yōu)化GROUP BY(松散索引掃描與緊湊索引掃描)
  • MySQL優(yōu)化GROUP BY方案
  • mysql group by 對(duì)多個(gè)字段進(jìn)行分組操作
  • 基于mysql實(shí)現(xiàn)group by取各分組最新一條數(shù)據(jù)
  • Mysql5.7及以上版本 ONLY_FULL_GROUP_BY報(bào)錯(cuò)的解決方法
  • MySQL中使用group by 是總是出現(xiàn)1055的錯(cuò)誤(推薦)
  • mysql case when group by 實(shí)例詳解
  • Mysql升級(jí)到5.7后遇到的group by查詢問(wèn)題解決
  • MySQL group by語(yǔ)句如何優(yōu)化

標(biāo)簽:白銀 恩施 秦皇島 定西 澳門(mén) 杭州 益陽(yáng) 寧夏

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL去重該使用distinct還是group by?》,本文關(guān)鍵詞  MySQL,去重,該,使用,distinct,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySQL去重該使用distinct還是group by?》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于MySQL去重該使用distinct還是group by?的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    嘉善县| 高阳县| 安多县| 瑞安市| 保定市| 旬阳县| 苗栗市| 荣成市| 尖扎县| 桐庐县| 湖南省| 贵定县| 潍坊市| 元阳县| 莱阳市| 湖口县| 仁布县| 商洛市| 华阴市| 安化县| 芜湖县| 普陀区| 天柱县| 红安县| 桂林市| 綦江县| 松阳县| 彭州市| 拉萨市| 社会| 吉林省| 巴东县| 五常市| 桐梓县| 如东县| 旬邑县| 巴楚县| 仙桃市| 米泉市| 三原县| 道真|