使用一个条件

时间:2017-07-25 06:44:08

标签: sql oracle sql-delete dml

我的脚本是正确的,但完成的执行时间约为5分钟,删除了11320860条记录。是否有其他方式编写此查询以减少执行时间?

场景是相同的记录组合可以有E和A记录。如果同一组合中至少存在一条E记录,则代码会尝试删除A和E记录。

Delete from tableA u 
WHERE EXISTS
    (Select 1 from tableA w 
    WHERE w.a = u.a
    AND w.b = u.b
    AND w.c = u.c
    AND w.d = u.d
    AND w.flag ='E' ); - Del about 11320860 records in 4 Mins

1 个答案:

答案 0 :(得分:0)

所以你需要这个,我想:

Delete from tableA u 
WHERE u.flag in ('A', 'E')
  and EXISTS
    (Select 1 from tableA w 
      WHERE w.a = u.a
        AND w.b = u.b
        AND w.c = u.c
        AND w.d = u.d
        AND w.flag ='E')

这种方式也应该有效:

delete from tableA 
  where flag in ('A', 'E')
    and (a, b, c, d) in 
    (select a, b, c, d 
       from tableA 
      where flag = 'E')