通过IN换INNER JOIN实现对mysql的优化
短信预约 -IT技能 免费直播动态提醒
不知道大家之前对类似通过IN换INNER JOIN实现对mysql的优化的文章有无了解,今天我在这里给大家再简单的讲讲。感兴趣的话就一起来看看正文部分吧,相信看完通过IN换INNER JOIN实现对mysql的优化你一定会有所收获的。
SQL问题:
要将A表查询的ID,匹配B表的ID,并将B表全部内容查询出来:
未优化前:
MySQL [xxuer]> SELECT
-> COUNT(*)
-> FROM
-> t_cmdb_app_version
-> WHERE
-> id IN (SELECT
-> pid
-> FROM
-> t_cmdb_app_relation UNION SELECT
-> rp_id
-> FROM
-> t_cmdb_app_relation);
+----------+
| COUNT(*) |
+----------+
| 266 |
+----------+
1 row in set (0.21 sec)
优化后:
MySQL [xxuer]> SELECT
-> count(*)
-> FROM
-> t_cmdb_app_version a
-> INNER JOIN
-> (SELECT
-> pid
-> FROM
-> t_cmdb_app_relation UNION SELECT
-> rp_id
-> FROM
-> t_cmdb_app_relation) b ON a.id = b.pid;
+----------+
| count(*) |
+----------+
| 266 |
+----------+
1 row in set (0.00 sec)
查看执行计划对比:
MySQL [xxuer]> explain SELECT
-> COUNT(*)
-> FROM
-> t_cmdb_app_version
-> WHERE
-> id IN (SELECT
-> pid
-> FROM
-> t_cmdb_app_relation UNION SELECT
-> rp_id
-> FROM
-> t_cmdb_app_relation);
+----+--------------------+---------------------+-------+---------------+---------+---------+------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+---------------------+-------+---------------+---------+---------+------+------+--------------------------+
| 1 | PRIMARY | t_cmdb_app_version | index | NULL | PRIMARY | 4 | NULL | 659 | Using where; Using index |
| 2 | DEPENDENT SUBQUERY | t_cmdb_app_relation | ALL | NULL | NULL | NULL | NULL | 383 | Using where |
| 3 | DEPENDENT UNION | t_cmdb_app_relation | ALL | NULL | NULL | NULL | NULL | 383 | Using where |
| NULL | UNION RESULT | <union2,3> | ALL | NULL | NULL | NULL | NULL | NULL | Using temporary |
+----+--------------------+---------------------+-------+---------------+---------+---------+------+------+--------------------------+
4 rows in set (0.00 sec)
MySQL [xxuer]> explain SELECT
-> count(*)
-> FROM
-> t_cmdb_app_version a
-> INNER JOIN
-> (SELECT
-> pid
-> FROM
-> t_cmdb_app_relation UNION SELECT
-> rp_id
-> FROM
-> t_cmdb_app_relation) b ON a.id = b.pid;
+----+--------------+---------------------+--------+---------------+---------+---------+-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------+---------------------+--------+---------------+---------+---------+-------+------+--------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 766 | Using where |
| 1 | PRIMARY | a | eq_ref | PRIMARY | PRIMARY | 4 | b.pid | 1 | Using where; Using index |
| 2 | DERIVED | t_cmdb_app_relation | ALL | NULL | NULL | NULL | NULL | 383 | NULL |
| 3 | UNION | t_cmdb_app_relation | ALL | NULL | NULL | NULL | NULL | 383 | NULL |
| NULL | UNION RESULT | <union2,3> | ALL | NULL | NULL | NULL | NULL | NULL | Using temporary |
+----+--------------+---------------------+--------+---------------+---------+---------+-------+------+--------------------------+
5 rows in set (0.00 sec)
看完通过IN换INNER JOIN实现对mysql的优化这篇文章,大家觉得怎么样?如果想要了解更多相关,可以继续关注我们的行业资讯板块。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341