我正在尝试获取100个帖子并按照他们在上周“重新混音”的次数进行排序。这是我到目前为止的查询:
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
这会产生正确的结果;但是,在运行DESCRIBE
之后我得到了这个:
以下是posts
上的索引:
我的索引remixes
:
以下是我的问题:
提前致谢!
Per 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
这是最新的DESCRIBE
我仍然担心filesort
部分。有什么想法吗?
答案 0 :(得分:1)
尽量不要将JOIN
包装在子选择中,因为这将创建一个未编制索引的临时表来存储子选择的结果,然后它将连接到该未索引的表上。
相反,在加入重新混音表时,将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
答案 1 :(得分:0)
在我看来
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
可以改写为
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
应该为您提供更好的EXPLAIN
计划并加快运行速度。