mysql count group by order by optimization

≯℡__Kan透↙ 提交于 2019-11-30 23:02:14

Add an index to your tags column:

ALTER TABLE `tagname` 
ADD INDEX `tags_index` (`tags` ASC) ;

EDIT:

Try creating a second index

CREATE INDEX tags_pid_index ON tagname (tags, pid);

Then modify your query to:

SELECT tags, COUNT(pid) as Num 
FROM tagname
GROUP BY tags
ORDER BY Num DESC

Have you tried like this:

    SELECT tags, COUNT(*) as Num FROM tagname
    GROUP BY tags HAVING COUNT(*) > 1
    ORDER BY Num DESC LIMIT 10

The trick can be: If you know the minimum popularity number, you can change the number in COUNT(*) > x

Thanks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!