mysql大量重复值建立索引_对于有大量重复数据的表添加唯一索引
遇到如題的這么一個場景:需要在MySQL的一張innodb引擎的表(tableA)上添加一個唯一索引(idx_col1_u)。但是表中已經有大量重復數據,對于每個key(col1),有的重復2行,有的重復N行。
此時,做數據的手工清理,或者SQL處理無疑是非常耗時的。
1. Alter ignore table come to help
印象中MySQL有一個獨有的 alter ignore add unique index的語法。
語法如下:
ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name
行為類似于insert ignore,即遇到沖突的unique數據則直接拋棄而不報錯。對于加唯一索引的情況來說就是建一張空表,然后加上唯一索引,將老數據用insert ignore語法插入到新表中,遇到沖突則拋棄數據。
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
總結
以上是生活随笔為你收集整理的mysql大量重复值建立索引_对于有大量重复数据的表添加唯一索引的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python分箱统计个数_【数据处理】p
- 下一篇: linux cmake编译源码,linu