SQL outputs rows with no duplicate fields

Source: Internet
Author: User

Recently, I have been sorting out the database of my website. The database is mysql. Because of the increasing access function, I imported access and reviewed the SQL. There are a lot of data, 0.15 million records, about 1 GB. The two fields a and B of some data are repeated, so that duplicate rows can be considered as duplicated data, as long as any row is retained. Simply put, the SQL statement should be written like this, "select distinct A, B from Table1;". Then, haha, it seems wrong. All fields are output. The correct statement should be written like this. "select * From Table1 where ID in (select Min (ID) from Table1 group by a, B);", get it, run it, and the result is correct, however, this process is slow. Execute "select Min (ID) from Table1 group by a, B" separately, store the data in a table, and create an index for the ID field. This process is much faster.

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.