對(duì)于mysql優(yōu)化之IN換INNER JOIN的案例分享
發(fā)表時(shí)間:2023-07-26 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]今天擼代碼時(shí),遇到SQL問(wèn)題:要將A表查詢的ID,匹配B表的ID,并將B表全部?jī)?nèi)容查詢出來(lái):未優(yōu)化前:MySQL [xxuer]> SELECT -> COUNT(*)->...
今天擼代碼時(shí),遇到SQL問(wèn)題:
要將A表查詢的ID,匹配B表的ID,并將B表全部?jī)?nèi)容查詢出來(lái):
未優(yōu)化前:
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)
優(yōu)化后:
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)
查看執(zhí)行計(jì)劃對(duì)比:
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)
以上就是關(guān)于mysql優(yōu)化之IN換INNER JOIN的實(shí)例分享的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
學(xué)習(xí)教程快速掌握從入門(mén)到精通的SQL知識(shí)。