Deduplicate data using the joint unique index keyword INGORE

For example, there are two fields user_id and user_name in the user table. If we do not want to have two identical user_id and user_name, we can add a joint unique index of the two fields to the user table:
alter table user add unique index(user_id ,user_name);


In this way, when adding the same record to the table, it will return 1062 adding failure information.
But there is a situation where there are already n duplicate records in the table, and then we remembered to add a unique index. When we perform the above operation, the database will tell you that there are already duplicate records, and the indexing fails. At this time , we can use the following operations:

alter ignore table user add unique index(user_id,user_name);


It will delete duplicate records (don't be afraid, it will keep one), and then build a unique index, which is efficient and user-friendly.

However, after executing alter ignore table tableA add unique index idx_col1_u (col1), the following error is still reported:

 #1062 - Duplicate entry '111' for key 'col1'.

Isn't it automatically discarding duplicate data? The worldview has been turned upside down. After checking the information, it turns out that the syntax of alter ignore does not support innodb.

It is learned that the implementation of alter ignore depends entirely on the internal implementation of the storage engine, rather than mandatory on the server side. The specific description is as follows:

For ALTER TABLE with the IGNORE keyword, IGNORE is now part of the
information provided to the storage engine. It is up to the storage
engine whether to use this when choosing between the in-place or copy
algorithm for altering the table. For InnoDB index operations, IGNORE
is not used if the index is unique, so the copy algorithm is used

 See: http://bugs.mysql.com/bug.php?id=40344

Of course, there are tricky ways to solve this problem. details as follows:

1、

ALTER TABLE tableA ENGINE MyISAM;
ALTER IGNORE TABLE tableA ADD UNIQUE INDEX idx_col1_u (col1)
ALTER TABLE table ENGINE InnoDB;

2、

You can use the method of set old_alter_table = 1; directly without changing to MyISAM. The specific methods are as follows:

set old_alter_table = 1;

ALTER IGNORE TABLE tableA ADD UNIQUE INDEX idx_col1_u (col1) 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324685251&siteId=291194637