码迷,mamicode.com
首页 > 数据库 > 详细

Oracle并行更新的两种方式(merge/update内联视图)

时间:2014-11-30 22:47:41      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:style   ar   使用   sp   on   数据   ad   as   size   

对于Oracle的两表联合更新的场景(有AB两表,以A.id=B.id关联,根据B表中的记录更新A表中的相应字段),一般有update内联视图和merge两种方式,下面举例介绍:

 

创建用例表:

create table test1(id number(10),name varchar2(20));

create table test2(id number(10),name varchar2(20));

 

测试数据:

begin

insert into test1 values(1,‘A‘);

insert into test1 values(2,‘B‘);

 

insert into test2 values(1,‘C‘);

insert into test2 values(2,‘D‘);

end;

 

 

merge方式:

merge into test1 using test2

on (test1.id = test2.id)

when matched then update

set test1.name = nvl2(test1.name,test2.name,test1.name);

 

merge方法是最简洁,效率最高的方式,在大数据量更新时优先使用这种方式。

 

 

update内联视图方式:

使用这种方式必须在test2.id上有主键(这里很好理解,必须保证每一个test1.id对应在test2里只有一条记录,如果test2中有多条对应的记录,怎么更新test1?),一般而言这种方式代价比merge方式稍高。

alter table test2 add constraint pk_test2 primary key(id);  --/*+ BYPASS_UJVC */

 

update (select /*+ BYPASS_UJVC */a.id aid,a.name aname,b.id bid,b.name bname from test1 a,test2 b where a.id=b.id) t

set aname = nvl2(aname,bname,aname);

 

 

 

使用并行,加快大量数据更新:

merge /*+parallel(test1,4)*/ into test1 using test2

on (test1.id = test2.id)

when matched then update

set test1.name = nvl2(test1.name,test2.name,test1.name);

Oracle并行更新的两种方式(merge/update内联视图)

标签:style   ar   使用   sp   on   数据   ad   as   size   

原文地址:http://www.cnblogs.com/quanweiru/p/4133682.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!