Remember the big pits in the business-mysql have the additional primary key under duplicate data

Source: Internet
Author: User

In the business process, two sheets are poor

At first a small table, thought that the amount of data is not very large, so there is no index.
When the actual operation of the process, the small table slowly grew up, found that two tables to find the difference between the time can reach a few hours!!! amazing!
What to do, to build index chant
Then the problem arises, the small table has the duplicate data, the whole, one deletes too SB
Write SQL
DELETE from Solr_indexed_max_incidentid WHERE incidentid in (SELECT Incidentid from Solr_indexed_max_incidentid GROUP by I Ncidentid having count (Incidentid) > 1);
Error:
ERROR 1093 (HY000): You can ' t specify target table ' Solr_indexed_max_incidentid ' for update on FROM clause
Oh, no.
Another want to switch

Build a temporary table and bake the data after the deduplication.
CREATE TABLE Solr_tmp as SELECT * from Solr_indexed_nax_incidentid GROUP by Incidentid have count (Incidentid) > 1;

and delete the original watch.
drop table Solr_indexed_max_incidentid; (later thought it was too unprofessional to change the table to a name equivalent to the backup there, in case the next mistake is not crazy?) )

and change the name.
RENAME TABLE solr_tmp to Solr_indexed_max_incidentid;

Then add the index
ALTER TABLE ' Solr_indexed_max_incidentid ' Add primary key (' Incidentid ');

Continue running business logic for a few seconds to finish!

Remember the big pits in the business-mysql have the additional primary key under duplicate data

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.