MySQL中索引優化distinct語句及distinct的多字段操作。本站提示廣大學習愛好者:(MySQL中索引優化distinct語句及distinct的多字段操作)文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL中索引優化distinct語句及distinct的多字段操作正文
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)"