你好,游客 登录 注册 搜索
背景:
阅读新闻

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

[日期:2016-07-27] 来源:Linux社区  作者:quanweiru [字体:大 中 小]

对于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

linux
相关资讯       Oracle并行更新 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款