MYSQL选择count,sum,inner join和group by

时间:2014-12-14 00:04:09

标签: mysql sql group-by

SELECT COUNT(s.fid) AS tcount, SUM(t.size) AS tsize, s.uid 
FROM xbt_files_users AS s INNER JOIN torrents AS t 
ON s.fid = t.id 
WHERE s.active = 1 AND s.mtime > '.$time.' AND s.remaining = 0 AND t.size >= 52428800 
GROUP BY s.uid

这个查询有些问题,在忙碌时,有时需要超过20秒...... xbt_files_users的索引为:ARM(active,remaining,mtime),uid,(fid / uid - unique) torrents有大小索引,id为primary 它使用:

  

使用where;使用临时;使用filesort for xbt_files_users

  

在哪里使用种子

有什么方法可以改善此查询?

1 个答案:

答案 0 :(得分:0)

这是您的查询:

SELECT COUNT(s.fid) AS tcount, SUM(t.size) AS tsize, s.uid 
FROM xbt_files_users s INNER JOIN
     torrents t 
     ON s.fid = t.id 
WHERE s.active = 1 AND s.mtime > '.$time.' AND s.remaining = 0 AND t.size >= 52428800 
GROUP BY s.uid

这些查询的最佳索引是xbt_files_users(active, remaining, mtime, fid, uid)torrents(id, size)。我会从那些索引开始。