How can I improve the efficiency of sorting this simple mysql? [

Source: Internet
Author: User
How can I improve the efficiency of sorting this simple mysql? [Select distinct A. name from table A order by (select sum (num) as num from table where name = A. name) desc

In a table, the name field is repeated, and the num field is a number .. Show non-repeated names, and then sort the num with the same name plus the total number ..
The preceding SQL result is correct, which is very slow. More than 10 thousand records are slow because they are sorted ..
How can I quickly achieve this sort function after modification?


Reply to discussion (solution)

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

Statistics are not fast. adding a redundant statistical table is the right path.

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



Thank you. it is easy to use .. It is much faster than the original one ..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.