oracle怎样高效批量更新:
数据库迁移遇到个问题
原先MSSQL
update Temp1
set Temp1.IsTrue=Temp2.IsTrue
from Temp1 inner join Temp2 on Temp1.ID=Temp2.ID
在ORACLE里如何改写,用in或instr>0来判断效率都不高,有高效写法么.
------解决方法--------------------------------------------------------
update Temp1
set Temp1.IsTrue=(select Temp2.IsTrue from Temp2
where ID=Temp2.ID and rownum=1)
------解决方法--------------------------------------------------------
楼上写的有问题:
如果 select Temp2.IsTrue from Temp2
where ID=Temp2.ID and rownum=1
没有返回值,则
set Temp1.IsTrue=NULL,会把原来的值更新成NULL;
declare
cursor cur_temp is select * from temp2;
c_temp cue_temp%rowtype;
begin
open cur_tmep;
loop
fetch cur_temp into c_temp;
exit when cur_tmep%notfound;
update temp1 set istrue=c_temp.istrue where
id = c_tmep.id;
end loop;
close cur_temp;
commit;
exception
when others then
close cur_tmep;
rollback;
end;
------解决方法--------------------------------------------------------
如果记录数不多,可以这么写:
UPDATE t1
SET value1=rec1.value1
WHERE d2_pk_id = rec1. d2_pk_id;
上述方法处理十万条以下的记录还是可以的,如果记录数不到1000万条,可以用循环,分批更新:
UPDATE (SELECT t1.value1 a1, d2.value1 b1
FROM t1,d2
WHERE d2.d2_pk_id >= 10000000 and d2. d2_pk_id<10300000
And t1.d2_pk_id >= 10000000 and t1. d2_pk_id<10300000
And t1.d2_pk_id = d2.d2_pk_id )
SET a1 = b1;