MySQL EXPLAIN Extra列的信息html
這一列包含的是不適合在其餘列顯示的額外信息。mysql
這意味着mysql服務器將在存儲引擎檢索行後再進行過濾。許多where條件裏涉及索引中的列,當它若是而且讀取索引時,就能背存儲引擎檢驗,所以不是全部帶有where子句的查詢都會顯示using where。算法
> explain select * from article where createTime = '1991-10-10 10:10:10' ******************** 1. row ********************* id: 1 select_type: SIMPLE table: article type: ALL possible_keys: key: key_len: ref: rows: 5 Extra: Using where 1 rows in set
這個值表示mysql將使用覆蓋索引,以免訪問表。不要把覆蓋索引和type:index訪問類型混淆了。sql
> explain select title,shortName from article ******************** 1. row ********************* id: 1 select_type: SIMPLE table: article type: index possible_keys: key: idx_short_name_title key_len: 514 ref: rows: 5 Extra: Using index 1 rows in set
這意味着mysql會對結果使用一個外部索引排序,而不是按照索引次序從表裏讀取行。mysql有兩種文件排序算法,兩種方式均可以在內存或磁盤文件上完成。EXPLAIN不會告訴你mysql將使用哪種文件排序,也不會告訴你排序會在內存裏仍是磁盤上完成。
服務器
> explain select * from article order by title,shortName asc ******************** 1. row ********************* id: 1 select_type: SIMPLE table: article type: ALL possible_keys: key: key_len: ref: rows: 5 Extra: Using filesort 1 rows in set
下面的查詢和排序都使用了覆蓋索引,因此不會出現using filesort,oop
> explain select title,shortName from article order by title,shortName asc ******************** 1. row ********************* id: 1 select_type: SIMPLE table: article type: index possible_keys: key: idx_short_name_title key_len: 514 ref: rows: 5 Extra: Using index 1 rows in set
Index Condition Pushdown (ICP)是MySQL 5.6版本中的新特性,是一種在存儲引擎層使用索引過濾數據的一種優化方式。性能
a 當關閉ICP時,index 僅僅是data access 的一種訪問方式,存儲引擎經過索引回表獲取的數據會傳遞到MySQL Server層進行where條件過濾。優化
b 當打開ICP時,若是部分where條件能使用索引中的字段,MySQL Server會把這部分下推到引擎層,能夠利用index過濾的where條件在存儲引擎層進行數據過濾,而非將全部經過index access的結果傳遞到MySQL server層進行where過濾。spa
優化效果:ICP能減小引擎層訪問基表的次數和MySQL Server訪問存儲引擎的次數,減小IO次數,提升查詢語句性能。code
> explain select * from article where title = 'hello' and shortName like '%hello%' ******************** 1. row ********************* id: 1 select_type: SIMPLE table: article type: ref possible_keys: idx_short_name_title key: idx_short_name_title key_len: 257 ref: const rows: 1 Extra: Using index condition 1 rows in set
這意味着mysql對查詢結果排序時會使用一個臨時表。
mysql什麼時候會使用臨時表https://dev.mysql.com/doc/refman/5.6/en/internal-temporary-tables.html
> explain select id , title from article a where a.id = 1 union select id ,title from article b where b.id = 2 order by id ******************** 1. row ********************* id: 1 select_type: PRIMARY table: a type: const possible_keys: PRIMARY key: PRIMARY key_len: 4 ref: const rows: 1 Extra: ******************** 2. row ********************* id: 2 select_type: UNION table: b type: const possible_keys: PRIMARY key: PRIMARY key_len: 4 ref: const rows: 1 Extra: ******************** 3. row ********************* id: select_type: UNION RESULT table: <union1,2> type: ALL possible_keys: key: key_len: ref: rows: Extra: Using temporary; Using filesort 3 rows in set
單獨講
單獨講
單獨講
============END============