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

4. ref
當通過普通的二級索引列與常量進行等值匹配時,那么對該表的訪問方法就是 ref,即使匹配到的值可能是多行,舉例:
mysql> explain select * from t11 where a=100; ---- ------------- ------- ------------ ------ --------------- ------ --------- ------- ------ ---------- ------- | id | select_type | table | partitions | type | possible_keys | key| key_len | ref| rows | filtered | Extra | ---- ------------- ------- ------------ ------ --------------- ------ --------- ------- ------ ---------- ------- |1 | SIMPLE| t11| NULL| ref| a| a| 5| const |500 |100.00 | NULL| ---- ------------- ------- ------------ ------ --------------- ------ --------- ------- ------ ---------- ------- 5. ref_or_null
當對普通二級索引進行等值匹配查詢,該索引列的值也可以是 NULL 值時,那么對該表的訪問方法就可能是 ref_or_null,舉例:
mysql> explain select * from t11 where a=100 or a is null; ---- ------------- ------- ------------ ------------- --------------- ------ --------- ------- ------ ---------- ----------------------- | id | select_type | table | partitions | type| possible_keys | key| key_len | ref| rows | filtered | Extra| ---- ------------- ------- ------------ ------------- --------------- ------ --------- ------- ------ ---------- ----------------------- |1 | SIMPLE| t11| NULL| ref_or_null | a| a| 5| const |501 |100.00 | Using index condition | ---- ------------- ------- ------------ ------------- --------------- ------ --------- ------- ------ ---------- ----------------------- 6. index_merge
一般情況下對于某個表的查詢只能使用到一個索引,在某些場景下可以使用 Intersection、Union、Sort-Union 這三種索引合并的方式來執行查詢,此時就顯示為 index_merge,舉例:
mysql> explain select * from t1 where a<50 or b=50; ---- ------------- ------- ------------ ------------- --------------- --------- --------- ------ ------ ---------- ---------------------------------------- | id | select_type | table | partitions | type| possible_keys | key| key_len | ref| rows | filtered | Extra| ---- ------------- ------- ------------ ------------- --------------- --------- --------- ------ ------ ---------- ---------------------------------------- |1 | SIMPLE| t1| NULL| index_merge | a,idx_b| a,idx_b | 5,5| NULL |50 |100.00 | Using sort_union(a,idx_b); Using where | ---- ------------- ------- ------------ ------------- --------------- --------- --------- ------ ------ ---------- ---------------------------------------- 7. unique_subquery
對一些包含 in 子查詢的查詢語句中,如果優化器無法使用 semi-join 或物化進行優化,最終將子查詢轉換為 EXISTS 子查詢,而且子查詢可以使用到主鍵或者唯一鍵進行等值匹配的話,那么該子查詢執行計劃的type列的值就是 unique_subquery 。舉例:
mysql> explain select * from t1 where a in(select id from t2 where t1.a=t2.a) or b=100; ---- -------------------- ------- ------------ ----------------- --------------- --------- --------- ------ ------ ---------- ------------- | id | select_type| table | partitions | type| possible_keys | key| key_len | ref| rows | filtered | Extra| ---- -------------------- ------- ------------ ----------------- --------------- --------- --------- ------ ------ ---------- ------------- |1 | PRIMARY| t1| NULL| ALL| idx_b| NULL| NULL| NULL | 1000 |100.00 | Using where ||2 | DEPENDENT SUBQUERY | t2| NULL| unique_subquery | PRIMARY,a| PRIMARY | 4| func |1 |100.00 | Using where | ---- -------------------- ------- ------------ ----------------- --------------- --------- --------- ------ ------ ---------- ------------- 8. range
如果使用索引獲取某些范圍區間的記錄,那么就可能使用到 range 訪問方法 。舉例:
mysql> explain select * from t1 where a<50 and a>20; ---- ------------- ------- ------------ ------- --------------- ------ --------- ------ ------ ---------- ----------------------- | id | select_type | table | partitions | type| possible_keys | key| key_len | ref| rows | filtered | Extra| ---- ------------- ------- ------------ ------- --------------- ------ --------- ------ ------ ---------- ----------------------- |1 | SIMPLE| t1| NULL| range | a| a| 5| NULL |29 |100.00 | Using index condition | ---- ------------- ------- ------------ ------- --------------- ------ --------- ------ ------ ---------- ----------------------- mysql> explain select * from t1 where a in(1,2,3); ---- ------------- ------- ------------ ------- --------------- ------ --------- ------ ------ ---------- ----------------------- | id | select_type | table | partitions | type| possible_keys | key| key_len | ref| rows | filtered | Extra| ---- ------------- ------- ------------ ------- --------------- ------ --------- ------ ------ ---------- ----------------------- |1 | SIMPLE| t1| NULL| range | a| a| 5| NULL |3 |100.00 | Using index condition | ---- ------------- ------- ------------ ------- --------------- ------ --------- ------ ------ ---------- -----------------------

推薦閱讀