开发者

mysql slow COUNT()

开发者 https://www.devze.com 2023-01-03 02:38 出处:网络
I\'m having some difficulties with a query: SELECT d.*, (SELECT COUNT(id) FROM downloads WHERE category = d.category) AS 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

精彩评论

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