开发者

mysql slow COUNT()

I'm having some difficulties with a query:

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

So, I'm trying to get the total downloadcount of each category but this query hangs each time I run it.

The downloads table has +- 20000 rows

What am I doing wrong开发者_如何转开发?


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


I think you'd be better off doing this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜