现在有A、B两个表,字段基本一样,A表有200万行数据,B表有80万行数据,现在想通过B表的HPZL、HPHM两个字段和A表的HAOPZL、HAOPHM字段相同来更新A表的几个字段数据:
update A tt
set (YEHMC,CHEPYS,CHANGPXH)=
(select t.YHMC,t.CPYS,t.CPXH from B t where
t.HPZL=tt.HAOPZL and t.HPHM=tt.HAOPHM)
where exists(select 1 from B t where t.HPZL=tt.HAOPZL and t.HPHM=tt.HAOPHM )
用这个语句执行很长时间也执行不完,请问有没有更好、更快的update 语句?
谢谢!
update A tt
set (YEHMC,CHEPYS,CHANGPXH)=
(select t.YHMC,t.CPYS,t.CPXH from B t where
t.HPZL=tt.HAOPZL and t.HPHM=tt.HAOPHM)
where exists(select 1 from B t where t.HPZL=tt.HAOPZL and t.HPHM=tt.HAOPHM )
用这个语句执行很长时间也执行不完,请问有没有更好、更快的update 语句?
谢谢!
解决方案 »
- 关于数据库(数据仓库)设计的问题
- oracle横着数据竖起来显示 Case When语句
- OracleOraDb11g_home1TNSListener启动后又立刻停止
- 在建RAC数据库时遇到的一个难题
- 查询符合这样条件的数据,日期大于某天,并且每天的字段f1都大于0
- oracle数据库连接问题,急~~~
- oracle各种数据类型的效率问题
- oracle7.3升级问题,特急!!!
- ORACLE中如何导出和导入数据库表
- oracle中的死锁和状态是inactive的进程
- 批处理运行load data infile导致oracle服务器死机
- 请问,编写一个PL/SQL程序时,是不是要将Exception语句块放到程序的最后?
A表的HAOPZL、HAOPHM上复合索引index2
update /*+ ordered use_nl(t, tt) */ t A tt
set (YEHMC,CHEPYS,CHANGPXH)=
(select t.YHMC,t.CPYS,t.CPXH from B t where
t.HPZL=tt.HAOPZL and t.HPHM=tt.HAOPHM)
where exists(select 1 from B t where t.HPZL=tt.HAOPZL and t.HPHM=tt.HAOPHM )
update (select /*+ ordered use_nl(t,tt)*/
tt.YHMC f1, tt.CPYS f2,tt.CPXH f3,t.YHMC ff1, t.CPYS ff2,t.CPXH ff3
FROM B t,
A tt
WHERE t.HPZL=tt.HAOPZL and t.HPHM=tt.HAOPHM )
SET f1=ff1, f2=ff2 , f3=ff3;
就是让B表做驱动表和A表做nest loop连接.
试试用游标更新数据
declare
type tb_1 is table of b.HPZL%type index by binary_integer;
type tb_2 is table of b.HPHM%type index by binary_integer;
type tb_3 is table of b.YHMC%type index by binary_integer;
type tb_4 is table of b.CPYS%type index by binary_integer;
type tb_5 is table of b.CPXH%type index by binary_integer;
tab_HPZL tb_1;
tab_HPHM tb_2;
tab_YHMC tb_3;
tab_CPYS tb_4;
tab_CPXH tb_5;
cursor cs is select HPZL, HPHM, YHMC, CPYS, CPXH from b;
begin
open cs;
loop
fetch cs bulk collect into tab_HPZL,tab_HPHM,tab_YHMC,tab_CPYS,tab_CPXH limit 5000;
exit when cs%notfound;
forall i in 1..tab_HPZL.count
update a set YEHMC=tab_YHMC(i),
CHEPYS=tab_CPYS(i),
CHANGPXH=tab_CPXH(i)
where HAOPZL=tab_HPZL(i) and HAOPHM=tab_HPHM(i);
end loop;
close cs;
end;
update (select /*+ ordered use_nl(t,tt)*/
tt.YHMC f1, tt.CPYS f2,tt.CPXH f3,t.YHMC ff1, t.CPYS ff2,t.CPXH ff3
FROM B t,
A tt
WHERE t.HPZL=tt.HAOPZL and t.HPHM=tt.HAOPHM )
SET f1=ff1, f2=ff2 , f3=ff3;