mysql 优化例子:IN 换 INNER JOIN
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 优化例子:IN 换 INNER JOIN相关的知识,希望对你有一定的参考价值。
今天撸代码时,遇到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)
本文出自 “隔壁老张” 博客,请务必保留此出处http://xxuer.blog.51cto.com/11947593/1940438
以上是关于mysql 优化例子:IN 换 INNER JOIN的主要内容,如果未能解决你的问题,请参考以下文章
优化:WHERE x IN (1, 2 .., 100.000) vs INNER JOIN tmp_table USING(x)?
INEXISTS的相关子查询用INNER JOIN 代替--性能优化
即使使用 INNER JOIN 而不是 IN,MySQL 查询也非常慢
python和R对dataframe进行连接行过滤更新列内容:dplyrmergeinnerleftrightinner_joinleft_joinsort_valuesloc