这条简单的mysql的排序怎么提高效率? [

WBOY
Release: 2016-06-23 13:38:31
Original
1079 people have browsed it

select distinct A.name from table A order by (select sum(num) as num from table where name=A.name) desc

一个表table,name字段有重复,num字段是数字。。显示不重复的name,然后相同的name的num加总数量排序。。
上面这条SQL结果是正确的,就是非常慢。一万多条记录就很慢了,原因是在排序那里。。
请问怎么修改才能又快又达到这样的排序功能?


回复讨论(解决方案)

select name, sum(num) as xxx from table group by name order by xxx desc

统计是不会快的,增加一个冗余的统计表才是正道.

select name, sum(num) as xxx from table group by name order by xxx desc



谢谢,好用。。比原来快多了。。
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!