MySQL通常使用GROUPBY(本質上是排序動作)完成DISTINCT操作,如果DISTINCT操作和ORDERBY操作組合使用,通常會用到臨時表.這樣會影響性能. 在一些情況下,MySQL可以使用索引優化DISTINCT操作,但需要活學活用.本文涉及一個不能利用索引完成DISTINCT操作的實例.
實例1 使用索引優化DISTINCT操作
create table m11 (a int, b int, c int, d int, primary key(a)) engine=INNODB; insert into m11 values (1,1,1,1),(2,2,2,2),(3,3,3,3),(4,4,4,4),(5,5,5,5),(6,6,6,6),(7,7,7,7),(8,8,8,8); explain select distinct(a) from m11;
mysql> explain select distinct(a) from m11;
復制代碼 代碼如下:
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+| 1 | SIMPLE | m11 | NULL | index | PRIMARY | PRIMARY | 4 | NULL | 1 | 100.00 | Using index |+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
說明:
1 'a'列上存在主鍵索引,MySQL可以利用索引(key列值表明使用了主鍵索引)完成了DISTINCT操作.
2 這是使用索引優化DISTINCT操作的典型實例.
實例2 使用索引不能優化DISTINCT操作
create table m31 (a int, b int, c int, d int, primary key(a)) engine=MEMORY; insert into m31 values (1,1,1,1),(2,2,2,2),(3,3,3,3),(4,4,4,4),(5,5,5,5),(6,6,6,6),(7,7,7,7),(8,8,8,8); explain select distinct(a) from m31;
mysql> explain select distinct(a) from m31;
復制代碼 代碼如下:
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+| 1 | SIMPLE | m31 | NULL | ALL | NULL | NULL | NULL | NULL | 8 | 100.00 | NULL |+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
說明:
1 從查詢執行計劃看,索引沒有被使用.
2 對比實例1的建表語句,只是存儲引擎不同.
3 為什麼主鍵索引沒有起作用? 難道MEMORY存儲引擎上的索引不可使用?
實例3 使用索引可以優化DISTINCT操作的Memory表
create table m33 (a int, b int, c int, d int, INDEX USING BTREE (a)) engine=MEMORY; insert into m33 values (1,1,1,1),(2,2,2,2),(3,3,3,3),(4,4,4,4),(5,5,5,5),(6,6,6,6),(7,7,7,7),(8,8,8,8); explain select distinct(a) from m33;
mysql> explain select distinct(a) from m33;
+----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+-------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+-------+| 1 | SIMPLE | m33 | NULL | index | NULL | a | 5 | NULL | 8 | 100.00 | NULL |+----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+-------+
說明:
1 'a'列上存在主鍵索引,MySQL可以利用索引(key列值表明使用了主鍵索引)完成了DISTINCT操作.
2 對比實例2,可以發現,二者都使用了Memory引擎. 但實例3指名使用Btree類型的索引.
3 實例2沒有指定使用什麼類型的索引,MySQL將采用默認值. MySQL手冊上說:
As indicated by the engine name, MEMORY tables are stored in memory. They use hash indexes by default, which makes them very fast for single-value lookups, and very useful for creating temporary tables.
結論:
1 看索引對查詢的影響,要注意索引的類型.
2 HASH索引適合等值查找,但不適合需要有序的場景,而Btree卻適合有序的場景.
3 看查詢執行計劃,發現索引沒有被使用,需要進一步考察索引的類型.
DISTINCT不能選擇多個字段的解決方法
在實際應用中,我們經常要選擇數據庫某表中重復數據,通常我們是使用DISTINCT函數。
但DISTINCT只能對一個字段有效,比如:
sql="select DISTINCT title from Table where id>0"
當我們需要列出數據中的另一列,比如:
sql="select DISTINCT title,posttime from Table where id>0"
得出的結果就不是我們想要的了,所以我們需要用另外的方法來解決這個問題。
下面的是我寫的SQL語句,我不知道是不是很好,但願有更好的人拿出來分享一下:
寫法一:
sql = "Select DISTINCT(title),posttime From Table1 Where id>0"
寫法二:
sql = "Select title,posttime From Table1 Where id>0 group by title,posttime"
寫法三:
sql="select title,posttime from Table where id in (select min(id) from Table group by title)"