mysql inner join in_关于mysql优化之IN换INNER JOIN的实例分享

本文探讨了在MySQL中,使用子查询与JOIN操作在查询性能上的差异。通过一个具体的例子,展示了如何将一个包含子查询的SQL语句优化为使用JOIN操作,从而显著提高查询速度。实验结果显示,优化后的JOIN查询比原始子查询在执行时间上快了约21倍。
摘要由CSDN通过智能技术生成

今天撸代码时,遇到SQL问题:

(相关mysql视频教程推荐:《mysql教程》)

要将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 | | 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 | | 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 | | ALL | NULL | NULL | NULL | NULL | NULL | Using temporary |

+----+--------------+---------------------+--------+---------------+---------+---------+-------+------+--------------------------+

5 rows in set (0.00 sec)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值