MySQL中distinct和group by性能比較

一、不加索引

select distinct num from test_test;
時間: 0.078ms

select num from test_test group by num;
時間: 0.031ms

二、加上索引

1   ALTER TABLE `test_test` ADD INDEX `num_index` (`num`) ;

再次查詢

select distinct num from test_test;
時間: 0.000ms

select num from test_test group by num;
時間: 0.000ms

三、結論

不管是加不加索引 group by 都比 distinct 快。因此使用的時候建議選 group by
加了索引之後 distinct 比沒加索引的 distinct 快了 107倍。
加了索引之後 group by 比沒加索引的 group by 快了 43倍。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章