MySQL執行計劃解析(四)
本文是對於MySQL執行計劃的解析,主要解釋了MySQL執行計劃中的各個引數及含義。
十三、Extra
產生的值
存在六種情況:
Using filesort、Using temporary、use index、using where、using join buffer、impossible where
1、Using filesort
說明mysql會對資料使用一個外部的索引排序,而不是按照表內的索引順序進行,
Mysql中無法利用索引完成排序操作稱為"檔案排序"。
EXPLAIN SELECT * FROM EMPLOYEE ORDER BY SALARY; +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+----------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+----------------+ | 1 | SIMPLE | EMPLOYEE | NULL | ALL | NULL | NULL | NULL | NULL | 8 | 100.00 | Using filesort | +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+----------------+ 1 row in set, 1 warning (0.02 sec)
根據salary欄位進行排序,並且salary欄位上沒有索引,那麼此時會使用檔案排序,extra為using filesort。
2、Using temporary
使用了臨時表儲存中間結果,Mysql在對查詢結果排序時, 使用了臨時表,常見於排序orderby 和分組查詢group by。
檢視dep_id的資料分佈:
EXPLAIN SELECT DEP_ID ,COUNT(*) FROM EMPLOYEE GROUP BY DEP_ID; +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-----------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-----------------+ | 1 | SIMPLE | EMPLOYEE | NULL | ALL | NULL | NULL | NULL | NULL | 8 | 100.00 | Using temporary | +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-----------------+ 1 row in set, 1 warning (0.01 sec)
對dep_id進行了group by,此時需要對中間的結果進行儲存,所以會使用臨時表,extra為using temporary。
3、use index
表示相應的select中使用了覆蓋索引,避免訪問了表的資料行, 效率很好。
如果同時出現using where,表明索引被用來執行索引鍵值的查詢;
如果沒有同時出現using where,表明索引用來讀取資料而非執行查詢動作。
對工資升序查詢資料(有索引):
EXPLAIN SELECT SALARY FROM EMPLOYEE ORDER BY SALARY; +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+----------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+----------------+ | 1 | SIMPLE | EMPLOYEE | NULL | ALL | NULL | NULL | NULL | NULL | 8 | 100.00 | Using filesort | +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+----------------+ 1 row in set, 1 warning (0.00 sec)
使用salary進行排序,並且有索引,那麼通過該列的索引即可查出資料,索引只是用來讀取資料的,也避免了排序,
此時extra顯示為using index。
4、using where
表明使用了where過濾(過濾欄位沒有索引或者不能使用索引)。
Dep_id欄位沒有索引。
EXPLAIN SELECT * FROM EMPLOYEE WHERE DEP_ID = 4; +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+ | 1 | SIMPLE | EMPLOYEE | NULL | ALL | NULL | NULL | NULL | NULL | 8 | 12.50 | Using where | +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)
當有使用where進行過濾時,在extra裡使用using where表示。
dep_id建立索引:
CREATE INDEX idx_emp_02 ON employee ( dep_id ); 當可用正常使用索引時: EXPLAIN SELECT * FROM EMPLOYEE WHERE DEP_ID > 4; +----+-------------+----------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+----------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+ | 1 | SIMPLE | EMPLOYEE | NULL | range | idx_emp_02 | idx_emp_02 | 5 | NULL | 5 | 100.00 | Using index condition | +----+-------------+----------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+ 1 row in set, 1 warning (0.00 sec)
通過索引進行訪問,標記為Using index condition。
不可以使用索引時:
EXPLAIN SELECT * FROM EMPLOYEE WHERE DEP_ID + 1 > 4; +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+ | 1 | SIMPLE | EMPLOYEE | NULL | ALL | NULL | NULL | NULL | NULL | 8 | 100.00 | Using where | +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)
標記為Using where
5、Using index condition
在使用where條件的索引時,會標記為Using index condition。
EXPLAIN SELECT * FROM EMPLOYEE WHERE DEP_ID > 4; +----+-------------+----------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+----------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+ | 1 | SIMPLE | EMPLOYEE | NULL | range | idx_emp_02 | idx_emp_02 | 5 | NULL | 5 | 100.00 | Using index condition | +----+-------------+----------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+ 1 row in set, 1 warning (0.00 sec)
通過索引進行訪問,標記為Using index condition。
6、impossible where
where條件導致沒有返回的行。
EXPLAIN SELECT * FROM EMPLOYEE WHERE ID IS NULL; +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------+ | 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Impossible WHERE | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------+ 1 row in set, 1 warning (0.00 sec)
Id是主鍵,所以不會有空值,在進行一個id為空的查詢時,是沒有結果的,因此extra會表示為impossible where。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31536355/viewspace-2690667/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL執行計劃解析MySql
- MySQL 執行計劃MySql
- MySQL執行計劃MySql
- MySQL執行計劃explain輸出列結果解析MySqlAI
- MySQL執行計劃explain的key_len解析MySqlAI
- mysql執行計劃explainMySqlAI
- mysql explain 執行計劃MySqlAI
- Oracle檢視執行計劃(四)Oracle
- 讀懂MySQL執行計劃MySql
- 讀懂 MySQL 執行計劃MySql
- mysql索引和執行計劃MySql索引
- 帶你看懂MySQL執行計劃MySql
- MySQL Explain執行計劃 - 詳解MySqlAI
- 十六、Mysql之Explain執行計劃MySqlAI
- mysql explain 執行計劃詳解MySqlAI
- MySQL執行計劃EXPLAIN詳解MySqlAI
- 解析Oracle執行計劃的結果Oracle
- Oracle訪問索引的執行計劃(四)Oracle索引
- mysql調優之——執行計劃explainMySqlAI
- mysql 執行計劃索引分析筆記MySql索引筆記
- (4) MySQL中EXPLAIN執行計劃分析MySqlAI
- mysql主從庫執行計劃不同MySql
- MySQL中in(常量列表)的執行計劃MySql
- 【MySQL】MySQL的執行計劃及索引優化MySql索引優化
- 執行計劃-1:獲取執行計劃
- 控制執行計劃之-SPM BASELINE(四)
- 執行計劃
- MySQL explain執行計劃詳細解釋MySqlAI
- MySQL執行計劃裡面的key_lenMySql
- mysql之 explain、optimizer_trace 執行計劃MySqlAI
- mysql的執行計劃快取問題MySql快取
- mysql 基於規則的執行計劃MySql
- MySQL 5.7 檢視理解SQL執行計劃MySql
- MySQL 5.7獲取指定執行緒正在執行SQL的執行計劃資訊MySql執行緒
- Sqlserver執行計劃中表的四種連線方式SQLServer
- 一個執行計劃解析的小問題分析
- 分析執行計劃優化SQLORACLE的執行計劃(轉)優化SQLOracle
- oracle sqlprofile 固定執行計劃,並遷移執行計劃OracleSQL