Forum Moderators: open
SELECT count(my_item), my_item
FROM data WHERE item_category='example_category'
GROUP BY my_item
ORDER BY count(my_item) DESC LIMIT 10;
id 1
select_type SIMPLE
table data
type ALL
possible_keys NULL
key NULL
key_len NULL
ref NULL
rows 5794584
Extra Using where; Using temporary; Using filesort
MySQL database with several million rows, from which I want to select the top 10 occurrences of a particular item
ORDER BY clause works when you use a function, or what kind of optimizations or caching are done, but something worth trying might be to give the result of COUNT() a name and then ORDER BY that name. SELECT COUNT( `my_item` ) AS `item_count` [...] ORDER BY `item_count` [...]