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執行計劃explainMySqlAI
- 讀懂MySQL執行計劃MySql
- mysql explain 執行計劃詳解MySqlAI
- 十六、Mysql之Explain執行計劃MySqlAI
- MySQL Explain執行計劃 - 詳解MySqlAI
- 帶你看懂MySQL執行計劃MySql
- 【MySQL】MySQL的執行計劃及索引優化MySql索引優化
- (4) MySQL中EXPLAIN執行計劃分析MySqlAI
- mysql主從庫執行計劃不同MySql
- mysql 執行計劃索引分析筆記MySql索引筆記
- mysql調優之——執行計劃explainMySqlAI
- 執行計劃-1:獲取執行計劃
- MySQL explain執行計劃詳細解釋MySqlAI
- MySQL 5.7 優化不能只看執行計劃MySql優化
- 理解索引:MySQL執行計劃詳細介紹索引MySql
- MySQL——通過EXPLAIN分析SQL的執行計劃MySqlAI
- MySql中執行計劃如何來的——Optimizer TraceMySql
- Sqlserver執行計劃中表的四種連線方式SQLServer
- 分析執行計劃優化SQLORACLE的執行計劃(轉)優化SQLOracle
- SYBASE執行計劃
- 【執行計劃】Oracle獲取執行計劃的幾種方法Oracle
- MySQL調優篇 | EXPLAIN執行計劃解讀(4)MySqlAI
- explain執行計劃分析AI
- oracle 固定執行計劃Oracle
- Oracle sql執行計劃OracleSQL
- 執行計劃執行步驟原則
- MySQL Cases-執行計劃向Oracle看齊(FORMAT=tree)MySqlOracleORM
- 在MySQL中使用explain查詢SQL的執行計劃MySqlAI
- 【PG執行計劃】Postgresql資料庫執行計劃統計資訊簡述SQL資料庫
- Docker筆記四之執行MySQLDocker筆記MySql
- TiDB與MySQL的SQL差異及執行計劃簡析TiDBMySql
- MySQL優化從執行計劃開始(explain超詳細)MySql優化AI
- mongodb執行計劃解釋MongoDB
- 檢視 OceanBase 執行計劃