本篇文章為大家展示了MySQL 中如何使用explain,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
站在用戶(hù)的角度思考問(wèn)題,與客戶(hù)深入溝通,找到河?xùn)|網(wǎng)站設(shè)計(jì)與河?xùn)|網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶(hù)體驗(yàn)好的作品,建站類(lèi)型包括:網(wǎng)站建設(shè)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊(cè)、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋河?xùn)|地區(qū)。
重點(diǎn)是第二種用法,需要深入的了解。
先看一個(gè)例子:
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+---------------+------+---------+------+--------+-------+
| 1 | SIMPLE | t_order | ALL | NULL | NULL | NULL | NULL | 100453 | |
+----+-------------+---------+------+---------------+------+---------+------+--------+-------+
1 row in set (0.03 sec)
加上extended后之后:
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------+---------------+------+---------+------+--------+----------+-------+
| 1 | SIMPLE | t_order | ALL | NULL | NULL | NULL | NULL | 100453 | 100.00 | |
+----+-------------+---------+------+---------------+------+---------+------+--------+----------+-------+
1 row in set, 1 warning (0.00 sec)
有必要解釋一下這個(gè)長(zhǎng)長(zhǎng)的表格里每一列的含義:
id | SELECT識(shí)別符。這是SELECT的查詢(xún)序列號(hào) |
select_type | SELECT類(lèi)型,可以為以下任何一種:
|
table | 輸出的行所引用的表 |
type | 聯(lián)接類(lèi)型。下面給出各種聯(lián)接類(lèi)型,按照從最佳類(lèi)型到最壞類(lèi)型進(jìn)行排序:
|
possible_keys | 指出MySQL能使用哪個(gè)索引在該表中找到行 |
key | 顯示MySQL實(shí)際決定使用的鍵(索引)。如果沒(méi)有選擇索引,鍵是NULL。 |
key_len | 顯示MySQL決定使用的鍵長(zhǎng)度。如果鍵是NULL,則長(zhǎng)度為NULL。 |
ref | 顯示使用哪個(gè)列或常數(shù)與key一起從表中選擇行。 |
rows | 顯示MySQL認(rèn)為它執(zhí)行查詢(xún)時(shí)必須檢查的行數(shù)。多行之間的數(shù)據(jù)相乘可以估算要處理的行數(shù)。 |
filtered | 顯示了通過(guò)條件過(guò)濾出的行數(shù)的百分比估計(jì)值。 |
Extra | 該列包含MySQL解決查詢(xún)的詳細(xì)信息
|
一.select_type的說(shuō)明
1.UNION:
當(dāng)通過(guò)union來(lái)連接多個(gè)查詢(xún)結(jié)果時(shí),第二個(gè)之后的select其select_type為UNION。
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------+------------+-------+---------------+---------+---------+-------+------+-------+
| 1 | PRIMARY | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | |
| 2 | UNION | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | |
| NULL | UNION RESULT | 3 rows in set (0.34 sec)
2.DEPENDENT UNION與DEPENDENT SUBQUERY:
當(dāng)union作為子查詢(xún)時(shí),其中第二個(gè)union的select_type就是DEPENDENT UNION。
第一個(gè)子查詢(xún)的select_type則是DEPENDENT SUBQUERY。
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+------------+-------+---------------+---------+---------+-------+--------+-------------+
| 1 | PRIMARY | t_order | ALL | NULL | NULL | NULL | NULL | 100453 | Using where |
| 2 | DEPENDENT SUBQUERY | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |
| 3 | DEPENDENT UNION | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |
| NULL | UNION RESULT | +----+--------------------+------------+-------+---------------+---------+---------+-------+--------+-------------+
4 rows in set (0.03 sec)
3.SUBQUERY:
子查詢(xún)中的第一個(gè)select其select_type為SUBQUERY。
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------+---------------+---------+---------+-------+------+-------------+
| 1 | PRIMARY | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | |
| 2 | SUBQUERY | t_order | const | PRIMARY | PRIMARY | 4 | | 1 | Using index |
+----+-------------+---------+-------+---------------+---------+---------+-------+------+-------------+
2 rows in set (0.03 sec)
4.DERIVED:
當(dāng)子查詢(xún)是from子句時(shí),其select_type為DERIVED。
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+
| 1 | PRIMARY | +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+
2 rows in set (0.03 sec)
二.type的說(shuō)明
1.system,const
見(jiàn)上面4.DERIVED的例子。其中第一行的type就是為system,第二行是const,這兩種聯(lián)接類(lèi)型是最快的。
2.eq_ref
在t_order表中的order_id是主鍵,t_order_ext表中的order_id也是主鍵,該表可以認(rèn)為是訂單表的補(bǔ)充信息表,他們的關(guān)系是1對(duì)1,在下面的例子中可以看到b表的連接類(lèi)型是eq_ref,這是極快的聯(lián)接類(lèi)型。
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+-----------------+------+-------------+
| 1 | SIMPLE | b | ALL | order_id | NULL | NULL | NULL | 1 | |
| 1 | SIMPLE | a | eq_ref | PRIMARY | PRIMARY | 4 | test.b.order_id | 1 | Using where |
+----+-------------+-------+--------+---------------+---------+---------+-----------------+------+-------------+
2 rows in set (0.00 sec)
3.ref
下面的例子在上面的例子上略作了修改,加上了條件。此時(shí)b表的聯(lián)接類(lèi)型變成了ref。因?yàn)樗信ca表中order_id=100的匹配記錄都將會(huì)從b表獲取。這是比較常見(jiàn)的聯(lián)接類(lèi)型。
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+----------+---------+-------+------+-------+
| 1 | SIMPLE | a | const | PRIMARY | PRIMARY | 4 | const | 1 | |
| 1 | SIMPLE | b | ref | order_id | order_id | 4 | const | 1 | |
+----+-------------+-------+-------+---------------+----------+---------+-------+------+-------+
2 rows in set (0.00 sec)
4.ref_or_null
user_id字段是一個(gè)可以為空的字段,并對(duì)該字段創(chuàng)建了一個(gè)索引。在下面的查詢(xún)中可以看到聯(lián)接類(lèi)型為ref_or_null,這是mysql為含有null的字段專(zhuān)門(mén)做的處理。在我們的表設(shè)計(jì)中應(yīng)當(dāng)盡量避免索引字段為NULL,因?yàn)檫@會(huì)額外的耗費(fèi)mysql的處理時(shí)間來(lái)做優(yōu)化。
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------------+---------------+---------+---------+-------+-------+-------------+
| 1 | SIMPLE | t_order | ref_or_null | user_id | user_id | 5 | const | 50325 | Using where |
+----+-------------+---------+-------------+---------------+---------+---------+-------+-------+-------------+
1 row in set (0.00 sec)
5.index_merge
經(jīng)常出現(xiàn)在使用一張表中的多個(gè)索引時(shí)。mysql會(huì)將多個(gè)索引合并在一起,如下例:
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+-------------------------------------------+
| 1 | SIMPLE | t_order | index_merge | PRIMARY,user_id | PRIMARY,user_id | 4,5 | NULL | 2 | Using union(PRIMARY,user_id); Using where |
+----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+-------------------------------------------+
1 row in set (0.09 sec)
6.unique_subquery
該聯(lián)接類(lèi)型用于替換value IN (SELECT primary_key FROM single_table WHERE some_expr)這樣的子查詢(xún)的ref。注意ref列,其中第二行顯示的是func,表明unique_subquery是一個(gè)函數(shù),而不是一個(gè)普通的ref。
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+---------+-----------------+-----------------+---------+---------+------+--------+-------------+
| 1 | PRIMARY | t_order | ALL | NULL | NULL | NULL | NULL | 100649 | Using where |
| 2 | DEPENDENT SUBQUERY | t_order | unique_subquery | PRIMARY,user_id | PRIMARY | 4 | func | 1 | Using where |
+----+--------------------+---------+-----------------+-----------------+---------+---------+------+--------+-------------+
2 rows in set (0.00 sec)
7.index_subquery
該聯(lián)接類(lèi)型與上面的太像了,唯一的差別就是子查詢(xún)查的不是主鍵而是非唯一索引。
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+---------+----------------+-----------------+---------+---------+------+--------+--------------------------+
| 1 | PRIMARY | t_order | ALL | NULL | NULL | NULL | NULL | 100649 | Using where |
| 2 | DEPENDENT SUBQUERY | t_order | index_subquery | PRIMARY,user_id | user_id | 5 | func | 50324 | Using index; Using where |
+----+--------------------+---------+----------------+-----------------+---------+---------+------+--------+--------------------------+
2 rows in set (0.00 sec)
8.range
按指定的范圍進(jìn)行檢索,很常見(jiàn)。
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | t_order | range | user_id | user_id | 5 | NULL | 3 | Using where |
+----+-------------+---------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)
9.index
在進(jìn)行統(tǒng)計(jì)時(shí)非常常見(jiàn),此聯(lián)接類(lèi)型實(shí)際上會(huì)掃描索引樹(shù),僅比ALL快些。
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------+---------------+---------+---------+------+--------+-------------+
| 1 | SIMPLE | t_order | index | NULL | user_id | 5 | NULL | 100649 | Using index |
+----+-------------+---------+-------+---------------+---------+---------+------+--------+-------------+
1 row in set (0.00 sec)
10.ALL
完整的掃描全表,最慢的聯(lián)接類(lèi)型,盡可能的避免。
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+---------------+------+---------+------+--------+-------+
| 1 | SIMPLE | t_order | ALL | NULL | NULL | NULL | NULL | 100649 | |
+----+-------------+---------+------+---------------+------+---------+------+--------+-------+
1 row in set (0.00 sec)
三.extra的說(shuō)明
1.Distinct
MySQL發(fā)現(xiàn)第1個(gè)匹配行后,停止為當(dāng)前的行組合搜索更多的行。對(duì)于此項(xiàng)沒(méi)有找到合適的例子,求指點(diǎn)。
2.Not exists
因?yàn)閎表中的order_id是主鍵,不可能為NULL,所以mysql在用a表的order_id掃描t_order表,并查找b表的行時(shí),如果在b表發(fā)現(xiàn)一個(gè)匹配的行就不再繼續(xù)掃描b了,因?yàn)閎表中的order_id字段不可能為NULL。這樣避免了對(duì)b表的多次掃描。
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+--------------+---------+-----------------+--------+--------------------------------------+
| 1 | SIMPLE | a | index | NULL | express_type | 1 | NULL | 100395 | Using index |
| 1 | SIMPLE | b | ref | order_id | order_id | 4 | test.a.order_id | 1 | Using where; Using index; Not exists |
+----+-------------+-------+-------+---------------+--------------+---------+-----------------+--------+--------------------------------------+
2 rows in set (0.01 sec)
3.Range checked for each record
這種情況是mysql沒(méi)有發(fā)現(xiàn)好的索引可用,速度比沒(méi)有索引要快得多。
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+----------------------+--------------+---------+------+------+------------------------------------------------+
| 1 | SIMPLE | t | range | PRIMARY,express_type | express_type | 1 | NULL | 1 | Using where |
| 1 | SIMPLE | s | ALL | order_id | NULL | NULL | NULL | 1 | Range checked for each record (index map: 0x1) |
+----+-------------+-------+-------+----------------------+--------------+---------+------+------+------------------------------------------------+
2 rows in set (0.00 sec)
4.Using filesort
在有排序子句的情況下很常見(jiàn)的一種情況。此時(shí)mysql會(huì)根據(jù)聯(lián)接類(lèi)型瀏覽所有符合條件的記錄,并保存排序關(guān)鍵字和行指針,然后排序關(guān)鍵字并按順序檢索行。
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+---------------+------+---------+------+--------+----------------+
| 1 | SIMPLE | t_order | ALL | NULL | NULL | NULL | NULL | 100395 | Using filesort |
+----+-------------+---------+------+---------------+------+---------+------+--------+----------------+
1 row in set (0.00 sec)
5.Using index
這是性能很高的一種情況。當(dāng)查詢(xún)所需的數(shù)據(jù)可以直接從索引樹(shù)中檢索到時(shí),就會(huì)出現(xiàn)。上面的例子中有很多這樣的例子,不再多舉例了。
6.Using temporary
發(fā)生這種情況一般都是需要進(jìn)行優(yōu)化的。mysql需要?jiǎng)?chuàng)建一張臨時(shí)表用來(lái)處理此類(lèi)查詢(xún)。
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+----------+---------+-----------------+--------+---------------------------------+
| 1 | SIMPLE | a | ALL | NULL | NULL | NULL | NULL | 100395 | Using temporary; Using filesort |
| 1 | SIMPLE | b | ref | order_id | order_id | 4 | test.a.order_id | 1 | |
+----+-------------+-------+------+---------------+----------+---------+-----------------+--------+---------------------------------+
2 rows in set (0.00 sec)
7.Using where
當(dāng)有where子句時(shí),extra都會(huì)有說(shuō)明。
8.Using sort_union(...)/Using union(...)/Using intersect(...)
下面的例子中user_id是一個(gè)檢索范圍,此時(shí)mysql會(huì)使用sort_union函數(shù)來(lái)進(jìn)行索引的合并。而當(dāng)user_id是一個(gè)固定值時(shí),請(qǐng)參看上面type說(shuō)明5.index_merge的例子,此時(shí)會(huì)使用union函數(shù)進(jìn)行索引合并。
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+------------------------------------------------+
| 1 | SIMPLE | t_order | index_merge | PRIMARY,user_id | user_id,PRIMARY | 5,4 | NULL | 2 | Using sort_union(user_id,PRIMARY); Using where |
+----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+------------------------------------------------+
1 row in set (0.00 sec)
對(duì)于Using intersect的例子可以參看下例,user_id與express_type發(fā)生了索引交叉合并。
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------------+----------------------+----------------------+---------+------+------+----------------------------------------------------+
| 1 | SIMPLE | t_order | index_merge | user_id,express_type | user_id,express_type | 5,1 | NULL | 1 | Using intersect(user_id,express_type); Using where |
+----+-------------+---------+-------------+----------------------+----------------------+---------+------+------+----------------------------------------------------+
1 row in set (0.00 sec)
9.Using index for group-by
表明可以在索引中找到分組所需的所有數(shù)據(jù),不需要查詢(xún)實(shí)際的表。
mysql | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------+---------------+---------+---------+------+------+--------------------------+
| 1 | SIMPLE | t_order | range | NULL | user_id | 5 | NULL | 3 | Using index for group-by |
+----+-------------+---------+-------+---------------+---------+---------+------+------+--------------------------+
1 row in set (0.00 sec)
上述內(nèi)容就是mysql 中如何使用explain,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文題目:mysql中如何使用explain
網(wǎng)頁(yè)路徑:http://muchs.cn/article42/jpicec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、服務(wù)器托管、全網(wǎng)營(yíng)銷(xiāo)推廣、標(biāo)簽優(yōu)化、關(guān)鍵詞優(yōu)化、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)