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)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?