大数据量高效率去重复数据

来源:互联网 发布:mysql实用教程 pdf 编辑:程序博客网 时间:2024/06/10 17:27

前提:

表a 字段 id、a1、a2(可以有多个字段,在此为方便只列出3个字段,其中id是主键,因此在一个表中是唯一的)

我的表数据大约是600w左右,最开始我采用的方法特别笨

delete from a where a.id not in(select max(id) from a group by a1,a2);

这种方式效率特别低,结果执行了1.5小时也没有执行完,果断换方法


解决办法:

1、建立一个临时表,将不重复的数据插入里边

creat table temp as select max(id) as id,a1,a2 from a group by a1,a2

2、将临时表中的数据导入到原表中

执行truncate table命令将原来表的数据删除,再用insert into select语句,在此不在赘述

0 0