Discuz教程网

oracle 两表交叉update效率问题

[复制链接]
authicon 千度 发表于 2010-10-26 06:32:27 | 显示全部楼层 |阅读模式

因为数据量比较大,并且是每天重复操作,因此对效率要求较高,请指教,谢谢。
具体情况如下:
表A 2000w行左右,字段如下:
date,A,B,C,D,E
表B 5000w行左右,字段如下
date,A,B,C,D,E
目的:
将表A的D,E字段更新到B表的D,E,条件是A.A=B.A AND A.B=B.B AND A.C=B.C AND A.DATE与B.DATE最接近的那一项
eg:
表A:
20100818000000,1,2,3,a,b
20100818010000,1,2,3,c,d
20100818020000,1,3,4,e,d
表B:
20100818001200,1,2,3,,
20100818003300,1,2,3,,
20100818021200,1,3,4,,
最终希望结果如下:
表B:
20100818001200,1,2,3,a,b //由于时间最接近表A第一列,相减绝对值最小
20100818003300,1,2,3,c,d
20100818021200,1,3,4,e,d
自己写的一个,效率很第,跑了一天,同时还没将date列考虑进去。
-----------------------------------------------------------------------------------
create or replace procedure add_ci_to_sacdr
as
cursor c_sacdr is select sgsnip ,chargeid ,msisdn,rowid from nsn_sacdr_tmp2;
ncount number(5);
nci number;
nlac number;
begin
ncount:=0;
for r in c_sacdr loop
   begin
   select ci,lac into nci,nlac from nsn_scdr_tmp2 where sgsnip=r.sgsnip and chargeid=r.chargeid and msisdn=r.msisdn and rownum=1;
   update nsn_sacdr_tmp2 set ci=nci,lac=nlac where rowid=r.rowid;
   ncount:=ncount+1;
   if ncount > 4999 then
      commit;
      ncount:=0;
   end if;
   exception
      when others then
      null;
   end;
end loop;
commit;
end;
/
------------------------------------------------------------
请问有什么更高效率的方法,请指教,谢谢,最好能给个粗略的脚本哈,本人是初学者。



上一篇:读写分离----构建报表查询统计系统
下一篇:oracle报ora-07445
authicon doni 发表于 2010-10-26 06:33:23 | 显示全部楼层

A表在a,b,c上建index
定义B表的cursor
根据B表的a,b,c来读A表,order by abs(a.date-b.date)
取第一笔的d,e去update A
authicon duolanshizhe 发表于 2010-10-26 07:55:17 | 显示全部楼层

关注   明天早上思考一下  呵呵呵
authicon renxiao2003 发表于 2010-10-26 09:27:25 | 显示全部楼层

交叉更新容易发生死锁。考虑这点啊。
authicon rico83 发表于 2010-10-26 11:47:17 | 显示全部楼层

谢谢各位,我测试一下。
authicon lizhuo 发表于 2010-10-26 11:55:45 | 显示全部楼层

考虑一下merge。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-7-12 03:56

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表