012--Explain實戰(zhàn)詳解:Extra分析

Paste_Image.png

Extra分析:包含不在其他屬性顯示雳窟,但是又非常重要的信息

1.Using FileSort:說明MySQL會對數(shù)據(jù)使用一個外部的索引排序,而不是按照表內的索引順序進行讀取,即MySQL無法使用索引完成的排序稱為"文件排序"

2.Using temporary:使用了臨時表來保存中間結果,MYSQL在對查詢結果進行排序的時候使用了臨時表,常見于排序OrderBy 和分組查詢GroupBy

Using FileSort只是不能按照索引方法進行排序淤击,但是Using temporary會創(chuàng)建一張臨時表,將緩存數(shù)據(jù)存放在臨時表中故源,然后再刪除臨時表污抬,操作變得更兇險了

3.Using Index:

  • 表示相應的select操作中使用了覆蓋索引(Covering Index),避免訪問了表的數(shù)據(jù)行绳军,效率不錯
  • 如果同時出現(xiàn)Using Where印机,表明索引被用來執(zhí)行索引鍵值的查找
  • 如果沒有同時出現(xiàn)Using Where,表明索引用來讀取數(shù)據(jù)而非執(zhí)行查找工作

4.覆蓋索引:

  • select 查詢的數(shù)據(jù)列只用ongoing索引中就能夠取得门驾,不必讀取數(shù)據(jù)行射赛,MySQL可以利用索引返回select列表中的字段,而不必根據(jù)索引再次讀取數(shù)據(jù)文件奶是,換句話說:索引列要被所建的索引覆蓋

如果想使用覆蓋索引楣责,在select的時候不能使用select * from t,具體寫出對應的字段

5.Using Where:使用where過濾條件

6.Using Join Buffer:使用了連接緩存(join太多個表聂沙,配置文件里面的JoinBuffer的值可以調大一點)

7.Impossible Where:where子句的值總是false秆麸,不能獲取任何元組

8.Select tables optimized away:在沒有GroupBy子句的情況下,基于索引優(yōu)化Min/Max操作或者對于MyISAM存儲引擎優(yōu)化Count(*)操作及汉,不必等到執(zhí)行階段再進行計算沮趣,查詢執(zhí)行計劃生成的階段即完成優(yōu)化

9.distinct:優(yōu)化distinct操作,在找到第一個匹配的元組后即停止找同樣值的動作

1.主鍵自動創(chuàng)建唯一索引
2.頻繁作為查詢的條件的字段應該創(chuàng)建索引
3.查詢中與其他表關聯(lián)的字段坷随,外鍵關系建立索引
4.頻繁更新的字段不應該創(chuàng)建索引房铭,因為每次更新不單單是更新了記錄,還會更新索引温眉,非常耗時
5.where條件中用不到的字段不適合創(chuàng)建索引
6.單鍵育叁、組合索引的選擇問題,(在高并發(fā)下傾向于創(chuàng)建組合索引)
7.查詢中排序的字段芍殖,排序字段若通過索引去訪問將大大的提高排序速度
8.查選中統(tǒng)計或分組的字段

索引和排序是有關系的:建完索引之后,排序也要按照索引建立的順序進行排序谴蔑,要不然會造成索引失效現(xiàn)象

1.1(A)沒有創(chuàng)建關聯(lián)索引

mysql>  explain SELECT * from t4 ORDER BY t4.id;
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
|  1 | SIMPLE      | t4    | index | NULL          | PRIMARY | 4       | NULL |    8 |       |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 ORDER BY t4.id,t4.`name`;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | t4    | ALL  | NULL          | NULL | NULL    | NULL |    8 | Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 ORDER BY t4.id,t4.`name`,t4.age;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | t4    | ALL  | NULL          | NULL | NULL    | NULL |    8 | Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)

1.1(B)關聯(lián)索引

mysql> explain SELECT * from t4 ORDER BY t4.id;
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
|  1 | SIMPLE      | t4    | index | NULL          | PRIMARY | 4       | NULL |    8 |       |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 ORDER BY t4.id,t4.`name`;
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-------------+
| id | select_type | table | type  | possible_keys | key               | key_len | ref  | rows | Extra       |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-------------+
|  1 | SIMPLE      | t4    | index | NULL          | index_id_name_age | 42      | NULL |    8 | Using index |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 ORDER BY t4.id,t4.`name`,t4.age;
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-------------+
| id | select_type | table | type  | possible_keys | key               | key_len | ref  | rows | Extra       |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-------------+
|  1 | SIMPLE      | t4    | index | NULL          | index_id_name_age | 42      | NULL |    8 | Using index |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-------------+
1 row in set (0.00 sec)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

1.2(A)沒有創(chuàng)建關聯(lián)索引

mysql> explain SELECT * from t4 ORDER BY t4.`name`,t4.id;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | t4    | ALL  | NULL          | NULL | NULL    | NULL |    8 | Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 ORDER BY t4.`name`,t4.id,t4.age;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | t4    | ALL  | NULL          | NULL | NULL    | NULL |    8 | Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 ORDER BY t4.`name`,t4.age,t4.id;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | t4    | ALL  | NULL          | NULL | NULL    | NULL |    8 | Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)

1.2(B)創(chuàng)建關聯(lián)索引

mysql> explain SELECT * from t4 ORDER BY t4.`name`,t4.id;
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-----------------------------+
| id | select_type | table | type  | possible_keys | key               | key_len | ref  | rows | Extra                       |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-----------------------------+
|  1 | SIMPLE      | t4    | index | NULL          | index_id_name_age | 42      | NULL |    8 | Using index; Using filesort |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-----------------------------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 ORDER BY t4.`name`,t4.id,t4.age;
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-----------------------------+
| id | select_type | table | type  | possible_keys | key               | key_len | ref  | rows | Extra                       |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-----------------------------+
|  1 | SIMPLE      | t4    | index | NULL          | index_id_name_age | 42      | NULL |    8 | Using index; Using filesort |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-----------------------------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 ORDER BY t4.`name`,t4.age,t4.id;
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-----------------------------+
| id | select_type | table | type  | possible_keys | key               | key_len | ref  | rows | Extra                       |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-----------------------------+
|  1 | SIMPLE      | t4    | index | NULL          | index_id_name_age | 42      | NULL |    8 | Using index; Using filesort |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-----------------------------+
1 row in set (0.00 sec)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

1.3(A)沒有創(chuàng)建關聯(lián)索引

mysql> explain SELECT * from t4  where t4.id = '1' ORDER BY t4.id;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | t4    | const | PRIMARY       | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4  where t4.id = '1' ORDER BY t4.id,t4.`name`;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | t4    | const | PRIMARY       | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4  where t4.id = '1' ORDER BY t4.id,t4.`name`,t4.age;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | t4    | const | PRIMARY       | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)

1.3(B)創(chuàng)建關聯(lián)索引

mysql> explain SELECT * from t4  where t4.id = '1' ORDER BY t4.id;
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
| id | select_type | table | type  | possible_keys             | key     | key_len | ref   | rows | Extra |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | t4    | const | PRIMARY,index_id_name_age | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4  where t4.id = '1' ORDER BY t4.id,t4.`name`;
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
| id | select_type | table | type  | possible_keys             | key     | key_len | ref   | rows | Extra |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | t4    | const | PRIMARY,index_id_name_age | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4  where t4.id = '1' ORDER BY t4.id,t4.`name`,t4.age;
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
| id | select_type | table | type  | possible_keys             | key     | key_len | ref   | rows | Extra |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | t4    | const | PRIMARY,index_id_name_age | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

1.4(A)沒有創(chuàng)建關聯(lián)索引

mysql> explain SELECT * from t4  where t4.id = '1' ORDER BY t4.`name`,t4.id;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | t4    | const | PRIMARY       | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4  where t4.id = '1' ORDER BY t4.`name`,t4.id,t4.age;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | t4    | const | PRIMARY       | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4  where t4.id = '1' ORDER BY t4.`name`,t4.age,t4.id;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | t4    | const | PRIMARY       | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)

1.4(B)創(chuàng)建關聯(lián)索引

mysql> explain SELECT * from t4  where t4.id = '1' ORDER BY t4.`name`,t4.id;
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
| id | select_type | table | type  | possible_keys             | key     | key_len | ref   | rows | Extra |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | t4    | const | PRIMARY,index_id_name_age | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4  where t4.id = '1' ORDER BY t4.`name`,t4.id,t4.age;
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
| id | select_type | table | type  | possible_keys             | key     | key_len | ref   | rows | Extra |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | t4    | const | PRIMARY,index_id_name_age | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4  where t4.id = '1' ORDER BY t4.`name`,t4.age,t4.id;
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
| id | select_type | table | type  | possible_keys             | key     | key_len | ref   | rows | Extra |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | t4    | const | PRIMARY,index_id_name_age | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)

2.創(chuàng)建索引代碼

CREATE INDEX index_id_name_age ON t4 (id,`name`,age)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末豌骏,一起剝皮案震驚了整個濱河市龟梦,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌窃躲,老刑警劉巖计贰,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異蒂窒,居然都是意外死亡躁倒,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進店門洒琢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來秧秉,“玉大人,你說我怎么就攤上這事衰抑∠笥” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵呛踊,是天一觀的道長砾淌。 經(jīng)常有香客問我,道長谭网,這世上最難降的妖魔是什么汪厨? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮愉择,結果婚禮上劫乱,老公的妹妹穿的比我還像新娘。我一直安慰自己薄辅,他們只是感情好要拂,可當我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著站楚,像睡著了一般脱惰。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上窿春,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天拉一,我揣著相機與錄音,去河邊找鬼旧乞。 笑死蔚润,一個胖子當著我的面吹牛,可吹牛的內容都是我干的尺栖。 我是一名探鬼主播嫡纠,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了除盏?” 一聲冷哼從身側響起叉橱,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎者蠕,沒想到半個月后窃祝,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡踱侣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年粪小,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片抡句。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡粤攒,死狀恐怖睬愤,靈堂內的尸體忽然破棺而出梨熙,到底是詐尸還是另有隱情沃琅,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布究抓,位于F島的核電站猾担,受9級特大地震影響,放射性物質發(fā)生泄漏刺下。R本人自食惡果不足惜绑嘹,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望橘茉。 院中可真熱鬧工腋,春花似錦、人聲如沸畅卓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽翁潘。三九已至趁冈,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間拜马,已是汗流浹背渗勘。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留俩莽,地道東北人旺坠。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像扮超,于是被迫代替她去往敵國和親取刃。 傳聞我的和親對象是個殘疾皇子蹋肮,可洞房花燭夜當晚...
    茶點故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內容