oracle删除数据很慢的问题

欢声小语 2017-07-15 03:59:08
sql1:
delete from table1 t1 left join table2 t2 on t1.field1=t2.field1 and t1.field2=t2.field2 and t1.field3=t2.field3
where t2.field3 is not null

sql2:
delete from table1 t1
where exists (select 1 from table2 t2 where t1.field1=t2.field1 and t1.field2=t2.field2 and t1.field3=t2.field3
where t2.field3 is not null)

其中可以保证table1 中 field3是一定存在的,那这两个sql删除的数据是一样的 吗?求指导
在执行大数据量(千万级)删除时sql2会删除的超级慢。能不能换成sql1
...全文
1781 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
AHUA1001 2019-05-21
  • 打赏
  • 举报
回复
两边的结果不一样,如果field3一定存在,那么第一个语句,应该是会把table1的数据全部删掉。第二个之删掉和table2有关联的数据。
sql2慢,可以加索引。
Digitwomen 2019-05-20
  • 打赏
  • 举报
回复
数据量大的表做分区表,删除的时候就会比较方便。
miqi_oracle 2019-05-17
  • 打赏
  • 举报
回复
delete就这样
riisse 2019-05-03
  • 打赏
  • 举报
回复
不要直接删除海量数据,建议:先备份table1数据导table1_bak,再truncate table1,最后再将table1_bak中不删除的插入table1。在此操作以前,必须将主键、索引删除,数据处理完成后再恢复主键、索引。
peilianhai 2019-05-01
  • 打赏
  • 举报
回复
delete from table1 t1
where exists (select 1 from table2 t2 where t1.field1=t2.field1 and t1.field2=t2.field2 and t1.field3=t2.field3
where t2.field3 is not null) 你看看field3条件 允许为空? 还作为关联字段?
如果是
where length(t2.field3)>0 试试
  • 打赏
  • 举报
回复
Oracle 不支持join连接做update和delete 吧
金木犀 2018-02-01
  • 打赏
  • 举报
回复
一般来说删除速度: drop> truncate > delete。如果想删除部分数据行用delete,注意带上where子句, 回滚段要足够大 。想删除表,当然用drop 想保留表而将所有数据删除。 如果和事务无关,用truncate即可。如果和事务有关,或者想触发trigger,还是用delete 就看你自己啦。但是还是要小心使用drop 和truncate,尤其没有备份的时候.否则哭都找不到太平洋呀!!!
卜塔 2017-07-16
  • 打赏
  • 举报
回复
可以替换,执行结果是一样的。

17,089

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧