【Oracle SQL】两百万数据的表中名称存在重复,直接删除方案和借助临时表方案比较(删除比例约七成,前者比后者慢)

【实验环境】

Oracle11g

【实验对象表及数据】

复制代码
create table test05(
    id number(10),
    name nvarchar2(5),
    primary key(id)
)

insert into test05
select
       rownum,
       dbms_random.string('*',dbms_random.value(1,5))
from dual
connect by level<2000001;
复制代码

约耗时15秒

【需求】

如果名称字段存在重复,则删除重复的记录

【备份数据以方便二次实验】

create table test06 as select * from test05;

【第一方案】

select count(*) from (select name from test05 group by name having count(id)>1);
发现有157551个name有重复值。

delete from test05 a where exists (select null from test05 b where b.name=a.name and b.id<a.id)
已删除1431272行。

已用时间: 00: 00: 50.04

SQL> select count(*) from test05;

COUNT(*)
----------
568728

已用时间: 00: 00: 00.03

【倒数据】
truncate table test05;
insert into test05 select * from test06;
用时约16秒

【第二方案】
select count(*) from (select name from test05 group by name having count(id)>1);
还是发现157551个name有重复值。这里确认了条件一致。

create table test07 as select * from test05 a where not exists (select null from test05 b where b.name=a.name and b.id<a.id)
用时约一秒半

SQL> select count(*) from test07;

COUNT(*)
----------
568728

已用时间: 00: 00: 00.04

truncate table test05;
insert into test05 select * from test07;
用时约12秒

 

由上可知,直接删除耗时约50秒,借助临时表耗时约1.5+12约14秒,这说明删除比例高时临时表方案胜出。

END

posted @   逆火狂飙  阅读(53)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2019-03-04 【Canvas与艺术】"社会主义核心价值观"表盘
2019-03-04 【Canvas与艺术】绘制地平线
2019-03-04 【Canvas与艺术】绘制磨砂黄铜材质Premium Quality徽章
2019-03-04 【Canvas与艺术】绘制蓝色森麒麟轮胎
2016-03-04 【Canvas技法】绘制绘制正五边形、正五角星
2016-03-04 VI使用技巧
2016-03-04 【Canvas与数学】绘制圆形中运动的包络线
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示