I've got big (>Mil rows) MySQL database messed up by duplicates. I think it could be from 1/4 to 1/2 of the whole db filled with them. I need to get rid of them quick (i mean query execution time). Here's how it looks:
id (index) | text1 | text2 | text3
text1 & text2 combination should be unique, if there are any duplicates, only one combination with text3 NOT NULL should remain. Example:

1 | abc | def | NULL  
2 | abc | def | ghi  
3 | abc | def | jkl  
4 | aaa | bbb | NULL  
5 | aaa | bbb | NULL  

...变成:

1 | abc | def | ghi   #(doesn't realy matter id:2 or id:3 survives)   
2 | aaa | bbb | NULL  #(if there's no NOT NULL text3, NULL will do)

新ID可以是任何东西,它们不依赖于旧的表ID

CREATE TABLE tmp SELECT text1, text2, text3
FROM my_tbl;
GROUP BY text1, text2;
DROP TABLE my_tbl;
ALTER TABLE tmp RENAME TO my_tbl;

Or SELECT DISTINCT and other variations.
While they work on small databases, query execution time on mine is just huge (never got to the end, actually; > 20 min)

有没有更快的方法?请帮我解决这个问题.

推荐答案

我相信这可以做到,使用on replicate key+ifnull()

create table tmp like yourtable;

alter table tmp add unique (text1, text2);

insert into tmp select * from yourtable 
    on duplicate key update text3=ifnull(text3, values(text3));

rename table yourtable to deleteme, tmp to yourtable;

drop table deleteme;

应该比任何需要group by或distinct或子查询,甚至order by的查询都要快得多.这甚至不需要文件排序,这会降低大型临时表的性能.仍然需要对原始表格进行全面扫描,但这是不可避免的.

Mysql相关问答推荐

如何在逗号分隔字符串值中使用LEFT函数

MySQL问题难以将文本字符串转换为正确的日期格式

我如何才能从MySQL过程中获取LARAVEL端的数据?

根据上一行S列结果计算列的查询

插入时发生日期时间字段溢出错误

Ballerina SQL 中是否也应该考虑输入清理?

模拟Mysql Cursor类的fetchone()方法,并将其返回值设置为None

有人可以帮我用 R 编程解决这个问题吗?

查询mysql中无法识别数据值

有没有更好的方法在无限滚动的网页上呈现获取的提要数据?

如何在 Mysql 中创建复合外键

如何使用nodejs从mysql数据库中获取最新的10条记录?

MySQL 添加一个 NOT NULL 列

用序列号mysql更新列

你如何在 Node.js 中模拟 MySQL(没有 ORM)?

mysql 按日期 Select 总和组

如何使用 phpmyadmin 复制数据库?

在 MySQL 中检测 utf8 损坏的字符

将mysql查询输出存储到shell变量中

按 COUNT(*) 过滤?