如果重复数据很多,被删掉的可能是大部分记录,业务又允许的情况下,可以考虑重建表
create table newtable as select distinct * from table;
rename table to oldtable;
rename newtable to table;
create index and constraint...
如果重复数据较少,利用rowid来删除:
delete table t1 where rowid < (select max(rowid) from table t2 where t1.col1 = t2.col1 and t1.col2 = t2.col2 and t1.col3 = t2.col3 ....)
这里要包含所有的字段相等的条件,还要考虑有的字段可能为NULL的情况,因为NULL是不等于NULL的,因此,对可能为NULL的字段有可能要写成NVL(T1.COL4,'XXX') = NVL(T2.COL4,'XXX')
上面的语句,效率未必好得到哪去,如果是9i 或以上版本,还可以考虑使用分析函数
delete table where rowid in (
select rid from (
select t.rowid rid, row_number() over(partition by col1, col2, col3 .... order by rowid) rn from table
) where rn > 1
)
如果你的系统业务繁忙,需要删除数据的表较大,事务也非常繁忙,那最好是别用一条delete语句来做删除操作,最好写PL/SQL,通过游标取出重复的那部分记录的rowid,然后批量绑定,批量删除,批量提交。
create table newtable as select distinct * from table;
rename table to oldtable;
rename newtable to table;
create index and constraint...
如果重复数据较少,利用rowid来删除:
delete table t1 where rowid < (select max(rowid) from table t2 where t1.col1 = t2.col1 and t1.col2 = t2.col2 and t1.col3 = t2.col3 ....)
这里要包含所有的字段相等的条件,还要考虑有的字段可能为NULL的情况,因为NULL是不等于NULL的,因此,对可能为NULL的字段有可能要写成NVL(T1.COL4,'XXX') = NVL(T2.COL4,'XXX')
上面的语句,效率未必好得到哪去,如果是9i 或以上版本,还可以考虑使用分析函数
delete table where rowid in (
select rid from (
select t.rowid rid, row_number() over(partition by col1, col2, col3 .... order by rowid) rn from table
) where rn > 1
)
如果你的系统业务繁忙,需要删除数据的表较大,事务也非常繁忙,那最好是别用一条delete语句来做删除操作,最好写PL/SQL,通过游标取出重复的那部分记录的rowid,然后批量绑定,批量删除,批量提交。