阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

128次阅读
没有评论

共计 1098 个字符,预计需要花费 3 分钟才能阅读完成。

对于 Oracle 的两表联合更新的场景(有 A、B 两表,以 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 相关信息见 Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2016-07/133612.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计1098字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中