mysql大量重复值建立索引_对于有大量重复数据的表添加唯一索引
遇到如題的這么一個(gè)場(chǎng)景:需要在MySQL的一張innodb引擎的表(tableA)上添加一個(gè)唯一索引(idx_col1_u)。但是表中已經(jīng)有大量重復(fù)數(shù)據(jù),對(duì)于每個(gè)key(col1),有的重復(fù)2行,有的重復(fù)N行。
此時(shí),做數(shù)據(jù)的手工清理,或者SQL處理無(wú)疑是非常耗時(shí)的。
1. Alter ignore table come to help
印象中MySQL有一個(gè)獨(dú)有的 alter ignore add unique index的語(yǔ)法。
語(yǔ)法如下:
ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name
行為類似于insert ignore,即遇到?jīng)_突的unique數(shù)據(jù)則直接拋棄而不報(bào)錯(cuò)。對(duì)于加唯一索引的情況來(lái)說(shuō)就是建一張空表,然后加上唯一索引,將老數(shù)據(jù)用insert ignore語(yǔ)法插入到新表中,遇到?jīng)_突則拋棄數(shù)據(jù)。
IGNORE is a MySQL extension to standard SQL. It controls how ALTER TABLE works if there are duplicates on unique keys in the new table or if warnings occur when strict mode is enabled. If IGNORE is not specified, the copy is aborted and rolled back if duplicate-key errors occur. If IGNORE is specified, only the first row is used of rows with duplicates on a unique key. The other conflicting rows are deleted. Incorrect values are truncated to the
總結(jié)
以上是生活随笔為你收集整理的mysql大量重复值建立索引_对于有大量重复数据的表添加唯一索引的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python分箱统计个数_【数据处理】p
- 下一篇: spring下连接mysql_使用Spr