gpt4 book ai didi

MySQL 查询优化 - 内部查询

转载 作者:可可西里 更新时间:2023-11-01 07:05:36 25 4
gpt4 key购买 nike

这是整个查询...

SELECT s.*, (SELECT url FROM show_medias WHERE show_id = s.id AND is_primary = 1) AS media_url
FROM (shows As s)
WHERE `s`.`id` IN (
SELECT DISTINCT st.show_id
FROM show_time_schedules AS sts
LEFT JOIN show_times AS st ON st.id = sts.show_time_id
WHERE sts.schedule_date BETWEEN CAST('2012-01-10' AS date) AND CAST('2012-01-14' AS date)
)
AND `s`.`is_active` = 1
ORDER BY s.name asc

如果...

SELECT url FROM show_medias WHERE show_id = s.id AND is_primary = 1
(0.0004 sec)

还有……

 SELECT DISTINCT st.show_id
FROM show_time_schedules AS sts
LEFT JOIN show_times AS st ON st.id = sts.show_time_id
WHERE sts.schedule_date BETWEEN CAST('2012-01-10' AS date) AND CAST('2012-01-14' AS date)
(0.0061 sec)

是否有明显的原因......

SELECT s.*, (inner query 1) AS media_url
FROM (shows As s)
WHERE `s`.`id` IN ( inner query 2 )
AND `s`.`is_active` = 1
ORDER BY s.name asc

需要 5.7245 秒吗?

解释扩展

id  select_type         table       type    possible_keys   key     key_len ref                     rows    filtered    Extra
1 PRIMARY s ALL NULL NULL NULL NULL 151 100.00 Using where; Using filesort
3 DEPENDENT SUBQUERY sts ALL NULL NULL NULL NULL 26290 100.00 Using where; Using temporary
3 DEPENDENT SUBQUERY st eq_ref PRIMARY PRIMARY 4 bvcdb.sts.show_time_id 1 100.00 Using where
2 DEPENDENT SUBQUERY show_medias ALL NULL NULL NULL NULL 159 100.00 Using where

最佳答案

您可以随时使用 EXPLAIN or EXPLAIN EXTENDED查看 MySql 对查询做了什么

您也可以用稍微不同的方式编写查询,您是否尝试过以下方法?

SELECT        s.*, 
sm.url AS media_url
FROM shows AS s
INNER JOIN show_medias AS sm ON s.id = SM.show_id
WHERE `s`.`id` IN (
SELECT DISTINCT st.show_id
FROM show_time_schedules AS sts
LEFT JOIN show_times AS st ON st.id = sts.show_time_id
WHERE sts.schedule_date BETWEEN CAST('2012-01-10' AS date) AND CAST('2012-01-14' AS date)
)
AND `s`.`is_active` = 1
AND sm.is_primary = 1
ORDER BY s.name asc

看看它的效果会很有趣。我希望它会更快,因为目前,我认为 MySql 将为您的每个节目运行内部查询 1(这样一个查询将运行多次。连接应该更有效。)

如果您想要在 show_medias 中没有一行的所有节目,请将 INNER JOIN 替换为 LEFT JOIN。

编辑:

我很快就会看一下您的 EXPLAIN EXTENDED,我也想知道您是否想尝试以下内容;它删除所有子查询:

SELECT        DISTINCT s.*,  
sm.url AS media_url
FROM shows AS s
INNER JOIN show_medias AS sm ON s.id = SM.show_id
INNER JOIN show_times AS st ON (s.id = st.show_id)
RIGHT JOIN show_time_schedules AS sts ON (st.id = sts.show_time_id)

WHERE `s`.`is_active` = 1
AND sm.is_primary = 1
AND sts.schedule_date BETWEEN CAST('2012-01-10' AS date) AND CAST('2012-01-14' AS date)
ORDER BY s.name asc

(如果能看到关于这些的 EXPLAIN EXTENDED 也很好 - 您可以将其添加到对此的评论中)。

进一步编辑:

关于您的 EXPLAIN EXTENDED ( a good start on how to read these is here )

USING FILESORT 和 USING TEMPORARY 都是关键指标。希望我推荐的第二个查询应该删除任何临时表(在子查询中)。然后尝试关闭 ORDER BY 以查看这是否有所不同(我们可以将其添加到目前的调查结果中:-)

我还可以看到查询可能遗漏了很多索引查找;您所有的 id 列都是索引匹配的主要候选者(通常是 index caveats )。我也会尝试添加这些索引,然后再次运行 EXPLAIN EXTENDED 以查看现在有什么区别(我们已经从您上面的评论中知道了编辑!)

关于MySQL 查询优化 - 内部查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8778748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com