[英]Help with performance: SUBQUERY vs JOIN
這是我的問題:
子查詢2.7 secs
SELECT SQL_NO_CACHE
item_id
FROM
mtrt_items_searches
WHERE
search_id IN (
SELECT
SQL_NO_CACHE
search_id
FROM
mtrt_searches_groups
WHERE
client_id =1
GROUP BY
search_id
)
LIMIT 0,350000
+----+--------------------+----------------------+-------+---------------+-----------+---------+-------+--------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+----------------------+-------+---------------+-----------+---------+-------+--------+--------------------------+
| 1 | PRIMARY | mtrt_items_searches | index | NULL | search_id | 12 | NULL | 367362 | Using where; Using index |
| 2 | DEPENDENT SUBQUERY | mtrt_searches_groups | ref | client_id | client_id | 4 | const | 13 | Using where; Using index |
+----+--------------------+----------------------+-------+---------------+-----------+---------+-------+--------+--------------------------+
僅子查詢需要0.0009秒才能返回以下數據,然后用此數據替換子查詢,查詢將在0.2 secs
運行:
SELECT SQL_NO_CACHE
item_id
FROM
mtrt_items_searches
WHERE
search_id IN (
1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35
)
LIMIT 0,350000
+----+-------------+---------------------+-------+---------------+-----------+---------+------+--------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------------+-------+---------------+-----------+---------+------+--------+--------------------------+
| 1 | SIMPLE | mtrt_items_searches | index | search_id | search_id | 12 | NULL | 367362 | Using where; Using index |
+----+-------------+---------------------+-------+---------------+-----------+---------+------+--------+--------------------------+
最終JOIN在0.4 secs
運行:
SELECT SQL_NO_CACHE
r.item_id
FROM
mtrt_items_searches r
INNER JOIN
mtrt_searches_groups sg
ON r.search_id =sg.search_id
WHERE
sg.client_id =1
GROUP BY
r.item_id
LIMIT 0,350000
+----+-------------+-------+------+---------------------+-----------+---------+------------------------+-------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------------+-----------+---------+------------------------+-------+----------------------------------------------+
| 1 | SIMPLE | sg | ref | search_id,client_id | client_id | 4 | const | 13 | Using index; Using temporary; Using filesort |
| 1 | SIMPLE | r | ref | search_id | search_id | 4 | clubr_new.sg.search_id | 26240 | Using index |
+----+-------------+-------+------+---------------------+-----------+---------+------------------------+-------+----------------------------------------------+
我正在嘗試在0.2 secs
執行子查詢或0.2 secs
。 可能嗎?
請嘗試以下查詢:
SELECT STRAIGHT_JOIN item_id
FROM (
SELECT DISTINCT search_id
FROM mtrt_searches_groups
WHERE client_id = 1
) JOIN mtrt_items_searches USING(search_id)
LIMIT 0,350000
嘗試添加以下索引
mtrt_items_searches(search_id,item_id)
mtrt_searches_groups(client_id, search_id)
嗯,我不這么認為。 子查詢太慢,遺憾的是最快的方法是直接使用數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.