使用GROUP BY时,mySQL查询确实很慢

时间:2015-02-09 19:19:33

标签: mysql group-by query-optimization

我在尝试优化使用GROUP BY函数的查询时遇到了一些问题。

SELECT COUNT(*) AS count, snapshot_id, date_sent, subject, name
FROM snapshots
INNER JOIN brands
ON snapshots.brand_id = brands.brand_id
WHERE snapshots.status = 1 AND brands.status = 1
GROUP BY subject
ORDER BY date_sent DESC
LIMIT 10;

两个表

[Table: snapshots]
snapshot_id   subject   date_sent   brand_id   status
1             abc       2015-2-1    1          1
2             abc       2015-2-2    1          1
3             xyz       2015-2-3    2          1
4             xyz       2015-2-4    2          1
5             xyz       2015-2-5    3          1

[Table: brands]
brand_id      name      status
1             nike      1
2             adidas    1
3             puma      1

结果:

count   snapshot_id   date_sent   subject   name
2       1             2015-2-1    abc       nike
2       3             2015-2-3    xyz       adidas
1       5             2015-2-5    xyz       puma

没有限制的查询返回70K +记录。 在没有Group的情况下执行时,查询将返回没有任何问题。 当按主题执行Group时,查询非常缓慢。

感谢任何帮助。

0 个答案:

没有答案
相关问题