MySQL Optimization example: in swap INNER JOIN

Source: Internet
Author: User

I'm having a SQL problem with the code today:


To query the ID of a table, match 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)


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)



To view execution plan comparisons:

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    ->          &nBsp;   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&NBSP;SEC)


This article is from the "next door Lao Zhang" blog, please be sure to keep this source http://xxuer.blog.51cto.com/11947593/1940438

MySQL Optimization example: in swap INNER JOIN

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.