[英]MySQL JOIN + Subquery Query Optimization
I'm trying to fetch 100 posts and order them by the number of times they've been "remixed" in the last week. 我正在尝试获取100个帖子,并按上周“混音”的次数对其进行排序。 Here is my query thus far:
到目前为止,这是我的查询:
SELECT COUNT(remixes.post_id) AS count, posts.title
FROM posts
LEFT JOIN (
SELECT * FROM remixes WHERE created_at >= 1343053513
) AS remixes ON posts.id = remixes.post_id
GROUP BY posts.id
ORDER BY count DESC, posts.created_at DESC
LIMIT 100
This produces the correct result; 这会产生正确的结果; however, after running
DESCRIBE
I get this: 但是,运行
DESCRIBE
我得到以下信息:
And here are my indexes on posts
: 这是我的
posts
索引:
And my indexes on remixes
: 我的
remixes
索引:
And here are my questions: 这是我的问题:
Thanks in advance! 提前致谢!
Per Zane's solution, I've updated my query to: 根据Zane的解决方案,我已将查询更新为:
SELECT COUNT(remixes.post_id) AS count, posts.title
FROM posts
LEFT JOIN remixes ON posts.id = remixes.post_id AND remixes.created_at >= 1343053513
GROUP BY posts.id
ORDER BY count DESC, posts.created_at DESC
LIMIT 100
And here's the latest DESCRIBE
这是最新的
DESCRIBE
I'm still worried about the filesort
part. 我仍然担心文件
filesort
部分。 Any ideas? 有任何想法吗?
Try not to wrap your JOIN
in a sub-select as this will create an unindexed temporary table to store the result of the subselect in, where it then joins on that unindexed table. 尽量不要将
JOIN
包装在子选择中,因为这将创建一个未索引的临时表来存储子选择的结果,然后将子选择的结果连接到该未索引的表中。
Instead, put created_at
as an additional join condition when joining the remixes table: 相反,在加入混音表时,将
created_at
作为附加的加入条件:
SELECT
a.title, COUNT(b.post_id) AS remixcnt
FROM
posts a
LEFT JOIN
remixes b ON a.id = b.post_id AND b.created_at >= 1343053513
GROUP BY
a.id, a.title
ORDER BY
remixcnt DESC, a.created_at DESC
LIMIT 100
It seems to me that 在我看来,这
SELECT COUNT(remixes.post_id) AS count, posts.title
FROM posts
LEFT JOIN (
SELECT * FROM remixes WHERE created_at >= 1343053513
) AS remixes ON posts.id = remixes.post_id
GROUP BY posts.id
ORDER BY count DESC, posts.created_at DESC
LIMIT 100
could be rewritten as 可以改写成
SELECT COUNT(r.post_id) AS count, posts.title
FROM posts
LEFT JOIN remixes r ON posts.id = r.post_id
WHERE r.created_at >= 1343053513
GROUP BY posts.id
ORDER BY count DESC, posts.created_at DESC
LIMIT 100
which should give you a better EXPLAIN
plan and run faster. 这样可以为您提供更好的
EXPLAIN
计划,并且运行速度更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.