删除数据库重复记录

来源:互联网 发布:网络十大禁书网盘下载 编辑:程序博客网 时间:2024/05/10 05:58

表t,三列:col,col1,col2

 

1,不适用于一个千万级记录的表,否则在生产系统中会给系统带来很大的开销。通过临时表,删除原来的数据,在把数据导回来。

create table t_tem (select distinct* from t);

truncate table t;

insert into t select * from t_tem;

 

2,使用于不知道重复了多少,且每条记录只有一条重复的。通过rowid来实现。

delete from t where rowid in(select a.rowid from t a,t b where a.woid>b.rowid and a.col1=b.col2 and a.col1=b.col2);

 

3,利用max或是min函数处理

 

delete from t a where rowid not in(select max(b.rowid) from t b where a.col1=b.col1 and a.col2=b.col2);

如果是min的话,前面where子句的<换成>

4,使用group by 减少了显性条件,提高效率。

delete from t where rowid not in (select max(rowid) from t a group by a.col2);

delete from t where (col1,col2) in (select col1,col2 from t group by col1,having count(*)>1) and rowid not in (select rowid from t group by col1,col2 having count(*)>1);

 

参照http://oracle.chinaitlab.com/attestation/832874.html验证实施。