mysql using filesort_Mysql执行计划中的Using filesort
Using filsort文檔中的解釋:
Mysql需要額外的一次傳遞,以找出如何按排序順序檢索行,通過根據聯接類型瀏覽所有行并為所有匹配where子句的行保存排序關鍵字和行的指針來完成排序,然后關鍵字被排序,并按排序順序檢索行。額外的傳遞是指什么?
Mysql> show create table test_filesort\G;
*************************** 1. row ***************************
Table: test_filesort
Create Table: CREATE TABLE `test_filesort` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
KEY `a_2` (`a`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
ERROR:
No query specified
Mysql> explain select * from test_filesort where a=1 order by b;
+—-+————-+—————+——+—————+——+———+——-+——+—————————–+
| id | select_type | table? ? ? ? ?| type | possible_keys | key? | key_len | ref? ?| rows | Extra? ? ? ? ? ? ? ? ? ? ? ?|
+—-+————-+—————+——+—————+——+———+——-+——+—————————–+
|? 1 | SIMPLE? ? ? | test_filesort | ref? | a_2? ? ? ? ? ?| a_2? | 5? ? ? ?| const |? ? 1 | Using where; Using filesort |
+—-+————-+—————+——+—————+——+———+——-+——+—————————–+
1 row in set (0.00 sec)
Mysql> alter table test_filesort add index(a,b);
Query OK, 6 rows affected (0.04 sec)
Records: 6? Duplicates: 0? Warnings: 0
Mysql> explain select * from test_filesort where a=1 order by b;
+—-+————-+—————+——+—————+——+———+——-+——+————-+
| id | select_type | table? ? ? ? ?| type | possible_keys | key? | key_len | ref? ?| rows | Extra? ? ? ?|
+—-+————-+—————+——+—————+——+———+——-+——+————-+
|? 1 | SIMPLE? ? ? | test_filesort | ref? | a_2,a? ? ? ? ?| a? ? | 5? ? ? ?| const |? ? 1 | Using where |
+—-+————-+—————+——+—————+——+———+——-+——+————-+
1 row in set (0.00 sec)
額外傳遞指的是多做了一次排序,很少人會把所有需要排序的字段都放到索引中,出現Using filesort不一定就會有什么性能問題。當然,我是希望盡量在應用中實現order by排序,如果放在數據庫這邊實現,查詢次數又非常大的話,盡量考慮把字段直接冗余到索引中去,避免Mysql自身的排序機制可能會引起的性能下降。
1.order by時可能會出現Using filesort。
2.order by b,如果b列不在索引中,不管b值是否相同,總會出現Using filesort。
3.并不是說所有的索引都可以避免Using filesort,hash索引是不按順序來保存數據的。
注:
key:顯示Mysql實際決定使用的索引
Using index:只從索引中檢索數據,不回表
key_len:顯示Mysql決定使用的鍵長度
rows:顯示Mysql認為它執行查詢時必須檢查的行數
ref:使用哪個列或常數與key一起從表中選擇行
possible_keys:指出Mysql可以使用哪個索引在該表中找到行
覺得文章有用?立即:
和朋友一起 共學習 共進步!
猜您喜歡
總結
以上是生活随笔為你收集整理的mysql using filesort_Mysql执行计划中的Using filesort的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dblinq mysql_DBLinq
- 下一篇: linux cmake编译源码,linu