MySQL執(zhí)行計(jì)劃解析(四)-創(chuàng)新互聯(lián)

本文是對于MySQL執(zhí)行計(jì)劃的解析,主要解釋了MySQL執(zhí)行計(jì)劃中的各個(gè)參數(shù)及含義。

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供達(dá)孜網(wǎng)站建設(shè)、達(dá)孜做網(wǎng)站、達(dá)孜網(wǎng)站設(shè)計(jì)、達(dá)孜網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、達(dá)孜企業(yè)網(wǎng)站模板建站服務(wù),10余年達(dá)孜做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

 十三、Extra

產(chǎn)生的值

存在六種情況:

Using filesort、Using temporary、use index、using where、using join buffer、impossible where

1、Using filesort

說明mysql會(huì)對數(shù)據(jù)使用一個(gè)外部的索引排序,而不是按照表內(nèi)的索引順序進(jìn)行,

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字段進(jìn)行排序,并且salary字段上沒有索引,那么此時(shí)會(huì)使用文件排序,extra為using filesort。

2、Using temporary

使用了臨時(shí)表保存中間結(jié)果,Mysql在對查詢結(jié)果排序時(shí), 使用了臨時(shí)表,常見于排序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進(jìn)行了group by,此時(shí)需要對中間的結(jié)果進(jìn)行保存,所以會(huì)使用臨時(shí)表,extra為using temporary。

3、use index

表示相應(yīng)的select中使用了覆蓋索引,避免訪問了表的數(shù)據(jù)行, 效率很好。

如果同時(shí)出現(xiàn)using where,表明索引被用來執(zhí)行索引鍵值的查找;

如果沒有同時(shí)出現(xiàn)using where,表明索引用來讀取數(shù)據(jù)而非執(zhí)行查找動(dòng)作。

對工資升序查找數(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)行排序,并且有索引,那么通過該列的索引即可查出數(shù)據(jù),索引只是用來讀取數(shù)據(jù)的,也避免了排序,

此時(shí)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)

當(dāng)有使用where進(jìn)行過濾時(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)

通過索引進(jì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

5、Using index condition

在使用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)

通過索引進(jìn)行訪問,標(biāo)記為Using index condition。

6、impossible where

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是主鍵,所以不會(huì)有空值,在進(jìn)行一個(gè)id為空的查詢時(shí),是沒有結(jié)果的,因此extra會(huì)表示為impossible where。

文章名稱:MySQL執(zhí)行計(jì)劃解析(四)-創(chuàng)新互聯(lián)
鏈接分享:http://muchs.cn/article48/dspjhp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站營銷、網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)