首页 > 数据库 > Oracle > 删除数据表中的重复记录
1.先创建一个表
CREATE TABLE APPTEST (
APP_ID INTEGER primary key NOT NULL,
MOBILE nvarchar2(50) NOT NULL
);
向其中插入1kw条测试数据
2.假设其中MOBILE字段大量重复,要删除重复记录,可以有如下三种方法:
(1)简单利用rowid删除
delete from APPTEST a where rowid not in (select max(rowid) from APPTEST b where a.mobile=b.mobile);
据说,这种方法在数据量很大时,效率并不高
有一些人说这种方法因为用到了rowid,所以效率较高。
经测试,花费时间2222s。回滚空间自动膨胀到2.5G。
(2)利用分析函数
delete APPTEST where rowid in (
select rid from
(select rowid rid,row_number() over(partition by mobile order by app_id desc) rn from APPTEST )
where rn > 1) ;
经测试,花费时间2844s。回滚空间自动膨胀到2.4G
(3)做temp表
create table temp_apptest as
select max(app_id) app_id,mobile from apptest
group by mobile;
drop table apptest;
rename temp_apptest to apptest;
经测试,花费时间10s。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/41451/viewspace-1007634/ ,如需转载,请注明出处,否则将追究法律责任。