本文是對(duì)于MySQL執(zhí)行計(jì)劃的解析,主要解釋了MySQL執(zhí)行計(jì)劃中的各個(gè)參數(shù)及含義。
網(wǎng)站建設(shè)公司,為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及定制網(wǎng)站建設(shè)服務(wù),專注于成都定制網(wǎng)站,高端網(wǎng)頁(yè)制作,對(duì)社區(qū)文化墻等多個(gè)行業(yè)擁有豐富的網(wǎng)站建設(shè)經(jīng)驗(yàn)的網(wǎng)站建設(shè)公司。專業(yè)網(wǎng)站設(shè)計(jì),網(wǎng)站優(yōu)化推廣哪家好,專業(yè)seo優(yōu)化排名優(yōu)化,H5建站,響應(yīng)式網(wǎng)站。
產(chǎn)生的值
存在六種情況:
Using filesort、Using temporary、use index、using where、using join buffer、impossible where
說(shuō)明mysql會(huì)對(duì)數(shù)據(jù)使用一個(gè)外部的索引排序,而不是按照表內(nèi)的索引順序進(jìn)行,
Mysql中無(wú)法利用索引完成排序操作稱為"文件排序"。
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字段進(jìn)行排序,并且salary字段上沒(méi)有索引,那么此時(shí)會(huì)使用文件排序,extra為using filesort。
使用了臨時(shí)表保存中間結(jié)果,Mysql在對(duì)查詢結(jié)果排序時(shí), 使用了臨時(shí)表,常見(jiàn)于排序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)
對(duì)dep_id進(jìn)行了group by,此時(shí)需要對(duì)中間的結(jié)果進(jìn)行保存,所以會(huì)使用臨時(shí)表,extra為using temporary。
表示相應(yīng)的select中使用了覆蓋索引,避免訪問(wèn)了表的數(shù)據(jù)行, 效率很好。
如果同時(shí)出現(xiàn)using where,表明索引被用來(lái)執(zhí)行索引鍵值的查找;
如果沒(méi)有同時(shí)出現(xiàn)using where,表明索引用來(lái)讀取數(shù)據(jù)而非執(zhí)行查找動(dòng)作。
對(duì)工資升序查找數(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進(jìn)行排序,并且有索引,那么通過(guò)該列的索引即可查出數(shù)據(jù),索引只是用來(lái)讀取數(shù)據(jù)的,也避免了排序,
此時(shí)extra顯示為using index。
表明使用了where過(guò)濾(過(guò)濾字段沒(méi)有索引或者不能使用索引)。
Dep_id字段沒(méi)有索引。
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進(jìn)行過(guò)濾時(shí),在extra里使用using where表示。
dep_id創(chuàng)建索引:
CREATE INDEX idx_emp_02 ON employee ( dep_id ); 當(dāng)可用正常使用索引時(shí): 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)
通過(guò)索引進(jìn)行訪問(wèn),標(biāo)記為Using index condition。
不可以使用索引時(shí):
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條件的索引時(shí),會(huì)標(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)
通過(guò)索引進(jìn)行訪問(wèn),標(biāo)記為Using index condition。
where條件導(dǎo)致沒(méi)有返回的行。
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是主鍵,所以不會(huì)有空值,在進(jìn)行一個(gè)id為空的查詢時(shí),是沒(méi)有結(jié)果的,因此extra會(huì)表示為impossible where。