简体   繁体   English

MySQL JOIN +子查询查询优化

[英]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我得到以下信息:

DESCRIBE语法的结果

And here are my indexes on posts : 这是我的posts索引:

帖子索引

And my indexes on remixes : 我的remixes索引:

混音索引

And here are my questions: 这是我的问题:

  1. Can you explain what the terms used in the extra column are really trying to tell me? 你能解释一下什么额外的列中使用的术语是真的想告诉我?
  2. Could you provide tips on how I can optimize this query so that it'll scale better. 您能否提供有关如何优化此查询的提示,以便更好地进行扩展。

Thanks in advance! 提前致谢!

Update 更新资料

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM