MySQL large table repeated field query efficiency

Source: Internet
Author: User
Keywords Network programming Mysql tutorial
Tags find find it group how to it is mysql mysql tutorial name

MySQL large table repeated fields should be how to find it? This is a lot of people have encountered the problem, here is to teach you a MySQL table repeated fields of inquiry, for your reference.

The database has a large table, you need to find the name of the duplicate record id, in order to compare.

If you just find the name of the database does not repeat the field, it is easy

SELECT min (`id`),` name` FROM `table` GROUP BY` name`;

But this does not get the id value that says there are duplicate fields. (Only got the smallest id value)

It is also easy to find out which fields are duplicates

SELECT `name`, count (` name`) as count FROM `table` GROUP BY` name` HAVING count (`name`)> 1 ORDER BY count DESC;

However, to check the repeated field id value, you must use the subquery, so use the following statement to MySQL large table repeated field queries.

SELECT `id`,` name` FROM `table` WHERE` name` in (SELECT `name` FROM` table` GROUP BY `name` HAVING count (` name`)> 1);

However, this statement is too inefficient in mysql, feeling mysql did not generate a temporary table for the subquery.

So use the first to create a temporary table

create table `tmptable` as (SELECT` name` FROM `table` GROUP BY` name` HAVING count (`name`)> 1);

Then use multi-table join query

SELECT a.`id`, a.`name` FROM `table` a,` tmptable` t WHERE a.`name` = t.`name`;

The result came quickly.

Repeat with distinct

SELECT distinct a.`id`, a.`name` FROM `table` a,` tmptable` t WHERE a.`name` = t.`name`;
Related Article

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.