您好,登錄后才能下訂單哦!
不知道大家之前對類似通過IN換INNER JOIN實(shí)現(xiàn)對mysql的優(yōu)化的文章有無了解,今天我在這里給大家再簡單的講講。感興趣的話就一起來看看正文部分吧,相信看完通過IN換INNER JOIN實(shí)現(xiàn)對mysql的優(yōu)化你一定會有所收獲的。
SQL問題:
要將A表查詢的ID,匹配B表的ID,并將B表全部內(nè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í)行計劃對比:
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)
看完通過IN換INNER JOIN實(shí)現(xiàn)對mysql的優(yōu)化這篇文章,大家覺得怎么樣?如果想要了解更多相關(guān),可以繼續(xù)關(guān)注我們的行業(yè)資訊板塊。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。