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


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


執行計劃字段概要說明id查詢語句中每出現一個 SELECT 關鍵字,MySQL 就會為它分配一個唯一的 id 值 。也有例外,比如優化器對子查詢做了 semi-join 優化時,和關聯查詢一樣兩個查詢的 id 是一樣的:
mysql> explain select * from t1 where a in (select b from t2 where t2.b=100); ---- ------------- ------- ------------ ------ --------------- ------ --------- ------- ------ ---------- -------------------------------------------------------------------- | id | select_type | table | partitions | type | possible_keys | key| key_len | ref| rows | filtered | Extra| ---- ------------- ------- ------------ ------ --------------- ------ --------- ------- ------ ---------- -------------------------------------------------------------------- |1 | SIMPLE| t1| NULL| ref| a| a| 5| const |1 |100.00 | NULL||1 | SIMPLE| t2| NULL| ALL| NULL| NULL | NULL| NULL|1 |100.00 | Using where; FirstMatch(t1); Using join buffer (Block Nested Loop) | ---- ------------- ------- ------------ ------ --------------- ------ --------- ------- ------ ---------- -------------------------------------------------------------------- 另外一個比較特殊的是 id 為 NULL,比如:
mysql> explain select * from t1 union select * from t2; ---- -------------- ------------ ------------ ------ --------------- ------ --------- ------ ------ ---------- ----------------- | id | select_type| table| partitions | type | possible_keys | key| key_len | ref| rows | filtered | Extra| ---- -------------- ------------ ------------ ------ --------------- ------ --------- ------ ------ ---------- ----------------- |1 | PRIMARY| t1| NULL| ALL| NULL| NULL | NULL| NULL | 1000 |100.00 | NULL||2 | UNION| t2| NULL| ALL| NULL| NULL | NULL| NULL |1 |100.00 | NULL|| NULL | UNION RESULT | | NULL| ALL| NULL| NULL | NULL| NULL | NULL |NULL | Using temporary | ---- -------------- ------------ ------------ ------ --------------- ------ --------- ------ ------ ---------- ----------------- 這是因為 union 結果是要去重的,內部創建了一個 名字的臨時表,把查詢 1 和查詢 2 的結果集都合并到這個臨時表中,利用唯一鍵進行去重,這種情況下查詢 id 就為 NULL 。
select_type表示查詢的類型,
1. SIMPLE
查詢語句中不包含 UNION 或者子查詢的查詢都算作是 SIMPLE 類型,比方說下邊這個單表查詢的 select_type 的值就是 SIMPLE:
mysql> explain select * from t1 where b=1 order by a; ---- ------------- ------- ------------ ------ --------------- ------- --------- ------- ------ ---------- --------------------------------------- | id | select_type | table | partitions | type | possible_keys | key| key_len | ref| rows | filtered | Extra| ---- ------------- ------- ------------ ------ --------------- ------- --------- ------- ------ ---------- --------------------------------------- |1 | SIMPLE| t1| NULL| ref| idx_b| idx_b | 5| const |1 |100.00 | Using index condition; Using filesort | ---- ------------- ------- ------------ ------ --------------- ------- --------- ------- ------ ---------- --------------------------------------- 關聯查詢也是 SIMPLE 類型:
mysql> explain select * from t1 join t2 on t1.a=t2.a; ---- ------------- ------- ------------ ------ --------------- ------ --------- ----------- ------ ---------- ------------- | id | select_type | table | partitions | type | possible_keys | key| key_len | ref| rows | filtered | Extra| ---- ------------- ------- ------------ ------ --------------- ------ --------- ----------- ------ ---------- ------------- |1 | SIMPLE| t2| NULL| ALL| a| NULL | NULL| NULL|1 |100.00 | Using where ||1 | SIMPLE| t1| NULL| ref| a| a| 5| hucq.t2.a |1 |100.00 | NULL| ---- ------------- ------- ------------ ------ --------------- ------ --------- ----------- ------ ---------- -------------

推薦閱讀