Comparison between distinct and group by in mysql

Source: Internet
Author: User


The data table records the bibliography used for user verification. Now I want to retrieve all the bibliography and use DISTINCT and group by to get the expected results, but I found that the returned results are arranged differently, distinct is displayed in the order of data storage, while group by is sorted (generally ASC ). DISTINCT is actually very similar to the implementation of the group by operation, except that only one record is retrieved in each GROUP after group. Therefore, the implementation of DISTINCT is similar to that of group by, and there is no big difference. It can also be achieved through loose index scanning or compact index scanning. Which of the following is more efficient for DISTINCT and group? The DISTINCT operation only needs to find all the different values. The group by operation also requires preparation for other Aggregate functions. At this point, the group by operation should do more work than DISTINCT. But in fact, group by is more efficient. Why? For the DISTINCT operation, it reads all records, and the number of records to be read by group by is as large as the number of groups in the GROUP, that is, it is much less than the actual number of records.
Author lcffufly

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.