schedual定時任務執行報錯 explain執行計劃詳解( 三 )

mysql> explain select * from t1 join (select distinct a from t2) as derived_t2 on t1.a=derived_t2.a;---- ------------- ------------ ------------ ------- --------------- ------------- --------- ---------------- ------ ---------- ------------- | id | select_type | table| partitions | type| possible_keys | key| key_len | ref| rows | filtered | Extra| ---- ------------- ------------ ------------ ------- --------------- ------------- --------- ---------------- ------ ---------- ------------- |1 | PRIMARY| t1| NULL| ALL| a| NULL| NULL| NULL|100 |100.00 | Using where ||1 | PRIMARY| | NULL| ref||| 5| join_test.t1.a |10 |100.00 | Using index ||2 | DERIVED| t2| NULL| index | a| a| 5| NULL| 1000 |100.00 | Using index | ---- ------------- ------------ ------------ ------- --------------- ------------- --------- ---------------- ------ ---------- ------------- 關于子查詢和派生表的優化策略,可具體查看對應的文章:MySQL 子查詢優化、SQL 優化:derived 派生表優化 。
typetype 顯示的是訪問類型,是較為重要的一個指標,結果值從好到壞依次是:system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL ,一般來說,得保證查詢至少達到 range 級別,最好能達到 ref 。
1. system
當表中只有一條記錄并且該表使用的存儲引擎的統計數據是精確的,比如 MyISAM、Memory,那么對該表的訪問方法就是 system:
mysql>explain select * from t_myisam; ---- ------------- ---------- ------------ -------- --------------- ------ --------- ------ ------ ---------- ------- | id | select_type | table| partitions | type| possible_keys | key| key_len | ref| rows | filtered | Extra | ---- ------------- ---------- ------------ -------- --------------- ------ --------- ------ ------ ---------- ------- |1 | SIMPLE| t_myisam | NULL| system | NULL| NULL | NULL| NULL |1 |100.00 | NULL| ---- ------------- ---------- ------------ -------- --------------- ------ --------- ------ ------ ---------- ------- InnoDB 表即使只有一行,也不是 system,而是 ALL:
mysql> explain select * from t5;---- ------------- ------- ------------ ------ --------------- ------ --------- ------ ------ ---------- ------- | id | select_type | table | partitions | type | possible_keys | key| key_len | ref| rows | filtered | Extra | ---- ------------- ------- ------------ ------ --------------- ------ --------- ------ ------ ---------- ------- |1 | SIMPLE| t5| NULL| ALL| NULL| NULL | NULL| NULL |1 |100.00 | NULL| ---- ------------- ------- ------------ ------ --------------- ------ --------- ------ ------ ---------- ------- 2. const
根據主鍵或者唯一二級索引列與單個常數進行等值匹配時(不能有多個條件用 or 連接,這屬于范圍查詢),對單表的訪問方法就是 const,舉例:
mysql> explain select * from t1 where id=100; ---- ------------- ------- ------------ ------- --------------- --------- --------- ------- ------ ---------- ------- | id | select_type | table | partitions | type| possible_keys | key| key_len | ref| rows | filtered | Extra | ---- ------------- ------- ------------ ------- --------------- --------- --------- ------- ------ ---------- ------- |1 | SIMPLE| t1| NULL| const | PRIMARY| PRIMARY | 4| const |1 |100.00 | NULL| ---- ------------- ------- ------------ ------- --------------- --------- --------- ------- ------ ---------- ------- 3. eq_ref
在連接查詢時,如果被驅動表是通過主鍵或者唯一二級索引列等值匹配的方式進行訪問的(如果該主鍵或者唯一二級索引是聯合索引的話,所有的索引列都必須進行等值比較),則對該被驅動表的訪問方法就是 eq_ref,舉例:
mysql> explain select * from t1 join t2 on t1.id=t2.id where t1.a<50; ---- ------------- ------- ------------ -------- --------------- --------- --------- ------------ ------ ---------- ------------- | id | select_type | table | partitions | type| possible_keys | key| key_len | ref| rows | filtered | Extra| ---- ------------- ------- ------------ -------- --------------- --------- --------- ------------ ------ ---------- ------------- |1 | SIMPLE| t2| NULL| ALL| PRIMARY| NULL| NULL| NULL|1 |100.00 | NULL||1 | SIMPLE| t1| NULL| eq_ref | PRIMARY,a| PRIMARY | 4| hucq.t2.id |1 |5.00 | Using where | ---- ------------- ------- ------------ -------- --------------- --------- --------- ------------ ------ ---------- -------------

推薦閱讀