首頁 > 資料庫 > mysql教程 > 關於mysql優化之IN換INNER JOIN的實例分享

關於mysql優化之IN換INNER JOIN的實例分享

黄舟
發布: 2019-02-23 15:31:38
原創
4060 人瀏覽過

今天擼程式碼時,遇到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       | <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)
登入後複製

以上是關於mysql優化之IN換INNER JOIN的實例分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板