需求是这样的:
找出a表(200万记录)的四个字段与b表(500万记录)的相应四个字段相同的,将它们的a和b表中的x字段更新为0(x初始为NULL)
剩下不同的全部的数据更新为-1
Cursor Cur is 
select A.1, A.2, A.3, A.4 from A for rec in Curloopupdate A  set A.x=0 --表B有相等的数据就更新A
where exists
(
select 1 from  B
where B.1=rec.1 and B.2=rec.2
and B.3=rec.3 and B.4=rec.4
)
and A.1=rec.1 and A.2=rec.2
and A.3=rec.3 and A.4=rec.4;update B   set B.x=0 --存不存在相等的数据都更新B表(没有肯定就更新不了:))
where
B.1=rec.1 and B.2=rec.2
and B.3=rec.3 and B.4=rec.4;
end loop;update A set A.x=-1 --剩下都是不相等的,一起更新
where A.x is null;update B set B.x=-1
where B.x is null ;
commit;我这么做感觉很慢很慢(更新B表的x字段很慢),是不是我的方法有问题,请高手指点一下.
建的索引:
A表 3个索引
B表 1个索引分不够说话另开帖送

解决方案 »

  1.   

    -----不建议使用游标,一条一条记录的处理,这样做会很慢
    -----先为A,B 表对四个字段建立组合索引
    update A  set A.x=0 
    where exists
    (
      select 1 from  B
      where B.1=A.1 and B.2=A.2
      and B.3=A.3 and B.4=A.4
    );update B set B.x=0 
    where exists (
      select 1 from A
      where B.1=A.1 and B.2=A.2
      and B.3=A.3 and B.4=A.4
    );update A set A.x=-1 
    where A.x is null;update B set B.x=-1
    where B.x is null ;
    commit;