温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL5.7中select半连接优化的示例分析

发布时间:2021-11-01 14:32:49 来源:亿速云 阅读:240 作者:小新 栏目:MySQL数据库

这篇文章主要为大家展示了“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,如下看执行计划:

点击(此处)折叠或打开

  1. mysql> select version();

  2. +------------+

  3. | version() |

  4. +------------+

  5. | 5.7.18-log |

  6. +------------+

点击(此处)折叠或打开

  1. mysql> explain select * from accessLog ac where ac.id in (select id from accessLog_01);

  2. +----+--------------+--------------+------------+--------+---------------+---------+---------+----------------+------+----------+-------+

  3. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

  4. +----+--------------+--------------+------------+--------+---------------+---------+---------+----------------+------+----------+-------+

  5. | 1 | SIMPLE | <subquery2> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | 100.00 | NULL |

  6. | 1 | SIMPLE | ac | NULL | eq_ref | PRIMARY | PRIMARY | 8 | <subquery2>.id | 1 | 100.00 | NULL |

  7. | 2 | MATERIALIZED | accessLog_01 | NULL | ALL | NULL | NULL | NULL | NULL | 1305 | 100.00 | NULL |

  8. +----+--------------+--------------+------------+--------+---------------+---------+---------+----------------+------+----------+-------+

点击(此处)折叠或打开

  1. mysql> explain select * from accessLog ac where exists (select * from accessLog_01);

  2. +----+-------------+--------------+------------+------+---------------+------+---------+------+--------+----------+-------+

  3. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

  4. +----+-------------+--------------+------------+------+---------------+------+---------+------+--------+----------+-------+

  5. | 1 | PRIMARY | ac | NULL | ALL | NULL | NULL | NULL | NULL | 586090 | 100.00 | NULL |

  6. | 2 | SUBQUERY | accessLog_01 | NULL | ALL | NULL | NULL | NULL | NULL | 1305 | 100.00 | NULL |

  7. +----+-------------+--------------+------------+------+---------------+------+---------+------+--------+----------+-------+

点击(此处)折叠或打开

  1. mysql> explain select ac.* from accessLog ac join accessLog_01 b on ac.id=b.id;

  2. +----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-------+

  3. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

  4. +----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-------+

  5. | 1 | SIMPLE | b | NULL | ALL | NULL | NULL | NULL | NULL | 1305 | 100.00 | NULL |

  6. | 1 | SIMPLE | ac | NULL | eq_ref | PRIMARY | PRIMARY | 8 | xinhost.b.id | 1 | 100.00 | NULL |

  7. +----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-------+

  办连接查询优化默认开启,通过show variables like 'optimizer_switch' \G查询:

点击(此处)折叠或打开

  1. mysql> show variables like 'optimizer_switch' \G

  2. *************************** 1. row ***************************

  3. Variable_name: optimizer_switch

  4.         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

  5. 1 row in set (0.01 sec)

  但是半连接优化只是针对查询,对于DML操作,性能依旧很差。

以上是“MySQL5.7中select半连接优化的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI