[英]MYSQL Select with count, sum, inner join and 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
I have some problems with this query, in busy, some times takes more than 20 seconds ... xbt_files_users is having index for: ARM(active, remaining, mtime), uid, (fid/uid - unique) torrents is having index for size, id primary It`s using: 我对此查询有一些问题,在繁忙中,有时需要20秒钟以上的时间... xbt_files_users的索引为:ARM(活动,剩余,mtime),uid,(fid / uid-唯一)torrents的索引为大小,主要ID使用以下方法:
Using where;
在哪里使用 Using temporary;
使用临时; Using filesort for xbt_files_users
对xbt_files_users使用文件排序
and 和
Using where for torrents
在哪里使用种子
It's any way to improve this query? 有什么方法可以改善这个查询?
This is your query: 这是您的查询:
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
The best indexes for these queries are xbt_files_users(active, remaining, mtime, fid, uid)
and torrents(id, size)
. 这些查询的最佳索引是
xbt_files_users(active, remaining, mtime, fid, uid)
和torrents(id, size)
。 I would start with those indexes. 我将从这些索引开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.