MySQL5.6子查詢優化總結

一、子查詢合併不支持
mysql> explain select * from t1 where exists (select * from t2 where t2.a2>5) and exists (select * from t2 where t2.a2<10);
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 9867 | NULL |
| 3 | SUBQUERY | t2 | ALL | NULL | NULL | NULL | NULL | 100 | Using where |
| 2 | SUBQUERY | t2 | ALL | NULL | NULL | NULL | NULL | 100 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
3 rows in set (0.00 sec)mysql

依然是兩條子查詢 沒有合併成一條。
對比子查詢邏輯優化後的語句:
mysql> explain select * from t1 where exists (select * from t2 where t2.a2>5 and t2.a2<10);
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 9867 | NULL |
| 2 | SUBQUERY | t2 | ALL | NULL | NULL | NULL | NULL | 100 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
2 rows in set (0.01 sec)
兩個子查詢合併成了一條。sql

因此在作多個子查詢時需注意對子查詢的合併。oop

二、子查詢展開
mysql> explain select * from t1, (select * from t2 where t2.a2>10) v_t2 where t1.a1<10 and v_t2.a2<20;優化

| 1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | 100 | Using where |
| 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 9867 | Using where; Using join buffer (Block Nested Loop) |
| 2 | DERIVED | t2 | ALL | NULL | NULL | NULL | NULL | 100 | Using where |
table

3 rows in set (0.00 sec)select

對比邏優化後,將子查詢展開的語句:
mysql> explain select * from t1, t2 where t1.a1<10 and t2.a2<20;查詢

| 1 | SIMPLE | t2 | ALL | NULL | NULL | NULL | NULL | 100 | Using where |
| 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 9867 | Using where; Using join buffer (Block Nested Loop) |tab

2 rows in set (0.00 sec)
因此對嵌套的子查詢,注意在邏輯上進行展開,將其和外表鏈接查詢。query

三、不支持對聚類子查詢的消除join

四、不支持對exists、not exists類型的子查詢優化

五、支持對IN類型的子查詢優化,但有些狀況下優化不徹底。
mysql> explain select b1 from t1 where t1.a1 in (select t2.a2 from t2 where t2.b2=10);
+----+--------------+-------------+------+---------------+------+---------+------+------+----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------+-------------+------+---------------+------+---------+------+------+----------------------------------------------------+
| 1 | SIMPLE | | ALL | NULL | NULL | NULL | NULL | NULL | NULL |
| 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 9867 | Using where; Using join buffer (Block Nested Loop) |
| 2 | MATERIALIZED | t2 | ALL | NULL | NULL | NULL | NULL | 100 | Using where |
+----+--------------+-------------+------+---------------+------+---------+------+------+----------------------------------------------------+
3 rows in set (0.00 sec)

上表說明對IN類型子查詢作了必定優化,可是不徹底,沒有消除子查詢。
對比優化後的語句:
mysql> explain select b1 from t1, t2 where t1.a1=t2.a2 and t2.b2=10;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------+
| 1 | SIMPLE | t2 | ALL | NULL | NULL | NULL | NULL | 100 | Using where |
| 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 9867 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------+
2 rows in set (0.00 sec)

六、對於NOT IN類型子查詢,一樣支持物化的優化,但不支持子查詢的消除。

相關文章
相關標籤/搜索