首页 / 数据库 / MySQL / Oracle并行更新的两种方式(merge/update内联视图)
对于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));测试数据:begininsert 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 test2on (test1.id = test2.id)when matched then updateset 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) tset aname = nvl2(aname,bname,aname);使用并行,加快大量数据更新:merge /*+parallel(test1,4)*/ into test1 using test2on (test1.id = test2.id)when matched then updateset test1.name = nvl2(test1.name,test2.name,test1.name);更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12本文永久更新链接地址