Help with performance: SUBQUERY vs JOIN (性能帮助:SUBQUERY vs JOIN)
本文介绍了性能帮助:SUBQUERY vs JOIN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的问题:
SUBQUERY 2.7 秒
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 秒:
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 秒内运行:
Finally the JOIN running in 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 秒 内执行子查询或联接.有可能吗?
I'm trying to do either the subquery or the join in 0.2 secs. Is it possible?
推荐答案
试试下面的查询:
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
这篇关于性能帮助:SUBQUERY vs JOIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:性能帮助:SUBQUERY vs JOIN
基础教程推荐
猜你喜欢
- 是否可以执行按位分组功能? 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 无法解决整理冲突 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
