您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“MySQL5.7中select半連接優化的示例分析”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“MySQL5.7中select半連接優化的示例分析”這篇文章吧。
MySQL的子查詢一直以來都是性能差的著稱,解決方法是以join代替。
MySQL5.5版本中該查詢先把accessLog表中版本為2.2的數據線過濾出來,然后每個符合條件的數據都與內表進行一次select id from accessLog_01,因此性能低下。MySQL5.5采取的解決方法是將in重寫為exists。
在MySQL5.6/5.7版本中,子查詢執行計劃是將in/exists重寫為join,如下看執行計劃:
點擊(此處)折疊或打開
mysql> select version();
+------------+
| version() |
+------------+
| 5.7.18-log |
+------------+
點擊(此處)折疊或打開
mysql> explain select * from accessLog ac where ac.id in (select id from accessLog_01);
+----+--------------+--------------+------------+--------+---------------+---------+---------+----------------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------+--------------+------------+--------+---------------+---------+---------+----------------+------+----------+-------+
| 1 | SIMPLE | <subquery2> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | 100.00 | NULL |
| 1 | SIMPLE | ac | NULL | eq_ref | PRIMARY | PRIMARY | 8 | <subquery2>.id | 1 | 100.00 | NULL |
| 2 | MATERIALIZED | accessLog_01 | NULL | ALL | NULL | NULL | NULL | NULL | 1305 | 100.00 | NULL |
+----+--------------+--------------+------------+--------+---------------+---------+---------+----------------+------+----------+-------+
點擊(此處)折疊或打開
mysql> explain select * from accessLog ac where exists (select * from accessLog_01);
+----+-------------+--------------+------------+------+---------------+------+---------+------+--------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------------+------------+------+---------------+------+---------+------+--------+----------+-------+
| 1 | PRIMARY | ac | NULL | ALL | NULL | NULL | NULL | NULL | 586090 | 100.00 | NULL |
| 2 | SUBQUERY | accessLog_01 | NULL | ALL | NULL | NULL | NULL | NULL | 1305 | 100.00 | NULL |
+----+-------------+--------------+------------+------+---------------+------+---------+------+--------+----------+-------+
點擊(此處)折疊或打開
mysql> explain select ac.* from accessLog ac join accessLog_01 b on ac.id=b.id;
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-------+
| 1 | SIMPLE | b | NULL | ALL | NULL | NULL | NULL | NULL | 1305 | 100.00 | NULL |
| 1 | SIMPLE | ac | NULL | eq_ref | PRIMARY | PRIMARY | 8 | xinhost.b.id | 1 | 100.00 | NULL |
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-------+
辦連接查詢優化默認開啟,通過show variables like 'optimizer_switch' \G查詢:
點擊(此處)折疊或打開
mysql> show variables like 'optimizer_switch' \G
*************************** 1. row ***************************
Variable_name: optimizer_switch
Value: index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on,index_condition_pushdown=on,mrr=on,mrr_cost_based=on,block_nested_loop=on,batched_key_access=off,materialization=on,semijoin=on,loosescan=on,firstmatch=on,duplicateweedout=on,subquery_materialization_cost_based=on,use_index_extensions=on,condition_fanout_filter=on,derived_merge=on
1 row in set (0.01 sec)
但是半連接優化只是針對查詢,對于DML操作,性能依舊很差。
以上是“MySQL5.7中select半連接優化的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。