本文是對于MySQL執(zhí)行計劃的解析,主要解釋了MySQL執(zhí)行計劃中的各個參數(shù)及含義。
創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供達孜網(wǎng)站建設(shè)、達孜做網(wǎng)站、達孜網(wǎng)站設(shè)計、達孜網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、達孜企業(yè)網(wǎng)站模板建站服務(wù),10余年達孜做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。產(chǎn)生的值
存在六種情況:
Using filesort、Using temporary、use index、using where、using join buffer、impossible where
說明mysql會對數(shù)據(jù)使用一個外部的索引排序,而不是按照表內(nèi)的索引順序進行,
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)
根據(jù)salary字段進行排序,并且salary字段上沒有索引,那么此時會使用文件排序,extra為using filesort。
使用了臨時表保存中間結(jié)果,Mysql在對查詢結(jié)果排序時, 使用了臨時表,常見于排序orderby 和分組查詢group by。
查看dep_id的數(shù)據(jù)分布:
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,此時需要對中間的結(jié)果進行保存,所以會使用臨時表,extra為using temporary。
表示相應(yīng)的select中使用了覆蓋索引,避免訪問了表的數(shù)據(jù)行, 效率很好。
如果同時出現(xiàn)using where,表明索引被用來執(zhí)行索引鍵值的查找;
如果沒有同時出現(xiàn)using where,表明索引用來讀取數(shù)據(jù)而非執(zhí)行查找動作。
對工資升序查找數(shù)據(jù)(有索引):
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進行排序,并且有索引,那么通過該列的索引即可查出數(shù)據(jù),索引只是用來讀取數(shù)據(jù)的,也避免了排序,
此時extra顯示為using index。
表明使用了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)
當(dāng)有使用where進行過濾時,在extra里使用using where表示。
dep_id創(chuàng)建索引:
CREATE INDEX idx_emp_02 ON employee ( dep_id ); 當(dāng)可用正常使用索引時: 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)
通過索引進行訪問,標(biāo)記為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)
標(biāo)記為Using where
在使用where條件的索引時,會標(biāo)記為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)
通過索引進行訪問,標(biāo)記為Using index condition。
where條件導(dǎo)致沒有返回的行。
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為空的查詢時,是沒有結(jié)果的,因此extra會表示為impossible where。