MySQL中distinct與group by之間的性能進(jìn)行比較
最近在網(wǎng)上看到了一些測(cè)試,感覺不是很準(zhǔn)確,今天親自測(cè)試了一番。得出了結(jié)論,測(cè)試過程在個(gè)人計(jì)算機(jī)上,可能不夠全面,僅供參考。
測(cè)試過程:
準(zhǔn)備一張測(cè)試表
CREATE TABLE `test_test` ( `id` int(11) NOT NULL auto_increment, `num` int(11) NOT NULL default '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
建個(gè)儲(chǔ)存過程向表中插入10W條數(shù)據(jù)
create procedure p_test(pa int(11)) begin declare max_num int(11) default 100000; declare i int default 0; declare rand_num int; select count(id) into max_num from test_test; while i < pa do if max_num < 100000 then select cast(rand()*100 as unsigned) into rand_num; insert into test_test(num)values(rand_num); end if; set i = i +1; end while; end
調(diào)用存儲(chǔ)過程插入數(shù)據(jù)
call p_test(100000);
開始測(cè)試:(不加索引)
select distinct num from test_test; select num from test_test group by num; [SQL] select distinct num from test_test; 受影響的行: 0 時(shí)間: 0.078ms [SQL] select num from test_test group by num; 受影響的行: 0 時(shí)間: 0.031ms

二、num字段上創(chuàng)建索引
ALTER TABLE `test_test` ADD INDEX `num_index` (`num`) ;
再次查詢
select distinct num from test_test; select num from test_test group by num; [SQL] select distinct num from test_test; 受影響的行: 0 時(shí)間: 0.000ms [SQL] select num from test_test group by num; 受影響的行: 0 時(shí)間: 0.000ms

這時(shí)候我們發(fā)現(xiàn)時(shí)間太小了 0.000秒都無法精確了。
我們轉(zhuǎn)到命令行下測(cè)試
mysql> set profiling=1; mysql> select distinct(num) from test_test; mysql> select num from test_test group by num; mysql> show profiles; +----------+------------+----------------------------------------+ | Query_ID | Duration | Query | +----------+------------+----------------------------------------+ | 1 | 0.00072550 | select distinct(num) from test_test | | 2 | 0.00071650 | select num from test_test group by num | +----------+------------+----------------------------------------+

分析:
加了索引之后 distinct 比沒加索引的distinct 快了107倍。
加了索引之后 group by 比沒加索引的group by 快了43倍。
再來對(duì)比 :distinct 和group by
不管是加不加索引group by 都比distinct 快。
因此使用的時(shí)候建議選 group by。
以上就是在MySQL中distinct與group by之間的性能進(jìn)行比較的,通過以上比較是不是對(duì)distinct和group by有了更深入的了解,希望對(duì)大家的學(xué)習(xí)有所幫助。
- mysql中distinct和group?by的區(qū)別淺析
- MySQL中的distinct與group by比較使用方法
- MySQL去重該使用distinct還是group by?
- Mysql中distinct與group by的去重方面的區(qū)別
- MySQL中distinct與group by語句的一些比較及用法講解
- MySQL中distinct語句的基本原理及其與group by的比較
- MySQL中Distinct和Group By語句的基本使用教程
- 解析mysql中:單表distinct、多表group by查詢?nèi)コ貜?fù)記錄
- MySQL中distinct和group by去重的區(qū)別解析
相關(guān)文章
mysql alter table 修改表命令詳細(xì)介紹
MYSQL ALTER TABLE命令用于修改表結(jié)構(gòu),例如添加/修改/刪除字段、索引、主鍵等等,本文章通過實(shí)例向大家介紹MYSQL ALTER TABLE語句的使用方法,需要的朋友可以參考一下。2016-10-10
MySQL數(shù)據(jù)庫子查詢語法規(guī)則詳解
子查詢是在查詢語句里面再嵌套一個(gè)查詢,這是因?yàn)槲覀冊(cè)谔崛?shù)據(jù)的時(shí)候有很多不知道的數(shù)據(jù)產(chǎn)生了依賴關(guān)系。本文為大家總結(jié)了一下MySQL數(shù)據(jù)庫子查詢語法規(guī)則,感興趣的可以了解一下2022-08-08
分享幾道關(guān)于MySQL索引的重點(diǎn)面試題
這篇文章主要給大家介紹了幾道關(guān)于MySQL索引的重點(diǎn)面試題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05

