DISTINCT 實際上和 GROUP BY 操作的實現非常相似,只不過是在 GROUP BY 之後的每組中只取出一條記錄而已。所以,DISTINCT 的實現和 GROUP BY 的實現也基本差不多,沒有太大的區別。同樣可以通過鬆散索引掃描或者是緊湊索引掃描來實現,當然,在無法僅僅使用索引即能完成 DISTINCT 的時候,MySQL 只能通過暫存資料表來完成。但是,和 GROUP BY 有一點差別的是,DISTINCT 並不需要進行排序。也就是說,在僅僅只是 DISTINCT 操作的 Query 如果無法僅僅利用索引完成操作的時候,MySQL 會利用暫存資料表來做一次資料的“緩衝”,但是不會對暫存資料表中的資料進行 filesort 操作。當然,如果我們在進行 DISTINCT 的時候還使用了 GROUP BY 並進行了分組,並使用了類似於 MAX 之類的彙總函式操作,就無法避免 filesort 了。
下面我們就通過幾個簡單的 Query 樣本來展示一下 DISTINCT 的實現。
1.首先看看通過鬆散索引掃描完成 DISTINCT 的操作:
sky@localhost : example 11:03:41> EXPLAIN SELECT DISTINCT group_id -> FROM group_messageG
*************************** 1. row *************************** id: 1 SELECT_type: SIMPLE table: group_message type: rangepossible_keys: NULL key: idx_gid_uid_gc key_len: 4 ref: NULL rows: 10 Extra: Using index for group-by1 row in set (0.00 sec)
我們可以很清晰的看到,執行計畫中的 Extra 資訊為“Using index for group-by”,這代表什麼意思?為什麼我沒有進行 GROUP BY 操作的時候,執行計畫中會告訴我這裡通過索引進行了 GROUP BY 呢?其實這就是於 DISTINCT 的實現原理相關的,在實現 DISTINCT的過程中,同樣也是需要分組的,然後再從每組資料中取出一條返回給用戶端。而這裡的 Extra 資訊就告訴我們,MySQL 利用鬆散索引掃描就完成了整個操作。當然,如果 MySQL Query Optimizer 要是能夠做的再人性化一點將這裡的資訊換成“Using index for distinct”那就更好更容易讓人理解了,呵呵。
2.我們再來看看通過緊湊索引掃描的樣本:
sky@localhost : example 11:03:53> EXPLAIN SELECT DISTINCT user_id -> FROM group_message -> WHERE group_id = 2G
*************************** 1. row *************************** id: 1 SELECT_type: SIMPLE table: group_message type: refpossible_keys: idx_gid_uid_gc key: idx_gid_uid_gc key_len: 4 ref: const rows: 4 Extra: Using WHERE; Using index1 row in set (0.00 sec)
這裡的顯示和通過緊湊索引掃描實現 GROUP BY 也完全一樣。實際上,這個 Query 的實現過程中,MySQL 會讓儲存引擎掃描 group_id = 2 的所有索引鍵,得出所有的 user_id,然後利用索引的已排序特性,每更換一個 user_id 的索引索引值的時候保留一條資訊,即可在掃描完所有 gruop_id = 2 的索引鍵的時候完成整個 DISTINCT 操作。
3.下面我們在看看無法單獨使用索引即可完成 DISTINCT 的時候會是怎樣:
sky@localhost : example 11:04:40> EXPLAIN SELECT DISTINCT user_id -> FROM group_message -> WHERE group_id > 1 AND group_id < 10G
*************************** 1. row *************************** id: 1 SELECT_type: SIMPLE table: group_message type: rangepossible_keys: idx_gid_uid_gc key: idx_gid_uid_gc key_len: 4 ref: NULL rows: 32 Extra: Using WHERE; Using index; Using temporary1 row in set (0.00 sec)
當 MySQL 無法僅僅依賴索引即可完成 DISTINCT 操作的時候,就不得不使用暫存資料表來進行相應的操作了。但是我們可以看到,在 MySQL 利用暫存資料表來完成 DISTINCT 的時候,和處理 GROUP BY 有一點區別,就是少了 filesort。實際上,在 MySQL 的分組演算法中,並不一定非要排序才能完成分組操作的,這一點在上面的 GROUP BY 最佳化小技巧中我已經提到過了。實際上這裡 MySQL 正是在沒有排序的情況下實現分組最後完成 DISTINCT 操作的,所以少了 filesort 這個排序操作。
4.最後再和 GROUP BY 結合試試看:
sky@localhost : example 11:05:06> EXPLAIN SELECT DISTINCT max(user_id) -> FROM group_message -> WHERE group_id > 1 AND group_id < 10 -> GROUP BY group_idG
*************************** 1. row *************************** id: 1 SELECT_type: SIMPLE table: group_message type: rangepossible_keys: idx_gid_uid_gc key: idx_gid_uid_gc key_len: 4 ref: NULL rows: 32 Extra: Using WHERE; Using index; Using temporary; Using filesort1 row in set (0.00 sec)
最後我們再看一下這個和 GROUP BY 一起使用帶有彙總函式的樣本,和上面第三個樣本相比,可以看到已經多了 filesort 排序操作了,正是因為我們使用了 MAX 函數的緣故。要取得分組後的 MAX 值,又無法使用索引完成操作,只能通過排序才行了。
mysql distinct和group by誰更好
1,測試前的準備
//準備一張測試表 mysql> 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 ;
Query OK, 0 rows affected (0.05 sec)
mysql> delimiter || //改變mysql命令結束符為|| //建個儲存過程向表中插入10W條資料 mysql> 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||
Query OK, 0 rows affected (0.00 sec)
mysql> call p_test(100000)||
Query OK, 1 row affected (5.66 sec)
mysql> delimiter ;//改變mysql命令結束符為; mysql> select count(id) from test_test; //資料都進去了
+-----------+ | count(id) | +-----------+ | 100000 | +-----------+ 1 row in set (0.00 sec)
mysql> show variables like "%pro%"; //查看一下,記錄執行的profiling是不是開啟動了,預設是不開啟的
+---------------------------+-------+ | Variable_name | Value | +---------------------------+-------+ | profiling | OFF | | profiling_history_size | 15 | | protocol_version | 10 | | slave_compressed_protocol | OFF | +---------------------------+-------+ 4 rows in set (0.00 sec)
mysql> set profiling=1; //開啟
Query OK, 0 rows affected (0.00 sec)
2,測試
//做了4組測試 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.07298225 | select distinct(num) from test_test | | 2 | 0.07319975 | select num from test_test group by num | | 3 | 0.07313525 | select num from test_test group by num | | 4 | 0.07317725 | select distinct(num) from test_test | | 5 | 0.07275200 | select distinct(num) from test_test | | 6 | 0.07298600 | select num from test_test group by num | | 7 | 0.07500700 | select num from test_test group by num | | 8 | 0.07331325 | select distinct(num) from test_test | | 9 | 0.57831575 | create index num_index on test_test (num) | //在這兒的時候,我加了索引 | 10 | 0.00243550 | select distinct(num) from test_test | | 11 | 0.00121975 | select num from test_test group by num | | 12 | 0.00116550 | select distinct(num) from test_test | | 13 | 0.00107650 | select num from test_test group by num | +----------+------------+-------------------------------------------+ 13 rows in set (0.00 sec)
上面的1-8是4組資料,並且是沒有加索引的,從中我們可以看出,distinct比group by 會好一點點
10-13是2組資料,是加了索引以後的,從中我們可以看出,group by 比distinct 會好一點點
一般情況,資料量比較大的表,關聯欄位都會加索引的,,並且加索引後檢索時間只有以前的六分之一左右。