mysql慢COUNT()

时间:2010-06-09 20:23:07

标签: mysql count

我在查询方面遇到了一些困难:

SELECT d.*, 
       (SELECT COUNT(id) FROM downloads WHERE category = d.category) AS count 
FROM downloads d 
GROUP BY d.category 
ORDER BY count DESC

所以,我正在尝试获取每个类别的总下载量,但每次运行时此查询都会挂起。

下载表格有+ - 20000行

我做错了什么?

2 个答案:

答案 0 :(得分:4)

SELECT category, count(id) as count
FROM downloads d 
GROUP BY d.category 
ORDER BY count DESC

答案 1 :(得分:0)

我认为你最好这样做:

  SELECT category, count(*) as cnt
    FROM downloads
GROUP BY category
ORDER BY cnt desc;