Home > Database > Mysql Tutorial > body text

Example sharing on mysql optimization of IN to INNER JOIN

黄舟
Release: 2019-02-23 15:31:38
Original
4031 people have browsed it

When I was coding today, I encountered a SQL problem:

(recommended related mysql video tutorial: "mysql tutorial")

To match the ID of table A to the ID of table B, and query all the contents of table B:

Before optimization:

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)
Copy after login

After optimization:

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)
Copy after login

View execution plan comparison:

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)
Copy after login
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)
Copy after login

The above is the detailed content of Example sharing on mysql optimization of IN to INNER JOIN. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!