您好,欢迎访问一九零五行业门户网

有关DBLINK操作的语句执行机制及优化方式

?分布式查询语句对于远程对象的查询在远程库执行,在远程可以执行的语句会通过优化器的查询转换,执行的是转换后的语句,然后结果集返回到本地,再与本地表运算。当然, 本地还是远程是相对的,我们可以通过driving_hint改变主查询计划的执行位置,但是对dml
?分布式查询语句对于远程对象的查询在远程库执行,在远程可以执行的语句会通过优化器的查询转换,执行的是转换后的语句,然后结果集返回到本地,再与本地表运算。当然,
本地还是远程是相对的,我们可以通过driving_hint改变主查询计划的执行位置,但是对dml,driving_site是失效的。另外对远程表也可以使用其他hint。
?分布式查询可能一条sql语句中有不同远程库的表,优化分布式查询要达到3点效果:
1.访问同一个远程库的次数要尽量少,也就是同一远程库的对象应该尽量转为一条sql运算,一次运算,运算后将结果返回给本地库
2.从远程库上返回到本地库的结果要尽量少,只访问远程对象需要的字段
3.远程库上执行的语句的计划以及远程库返回的结果与本地可以联合查询的计划应该高效
?优化分布式查询需要从以上3个方面着手。
下面的local_tab 7万多条,remote_big_tab百万条,remote_small_tab 7万多条。
?1.使用collocated内联视图
? 也就是sql要对引用不同远程库的表,要组织好,将相同库的表放一起组织成内联视图,这样oracle就很容易知道这个内联视图里的表是在同一远程库作完查询
? 后再返回给本地库,这样减少了本地库与远程库的交互次数和传输结果集的数量和次数。比如上面的查询
select ?* from local_tab a
?where exists
?(select ?1 from remote_big_tab@remote b,remote_small_tab@remote c
?where b.object_id=c.object_id and a.object_type=b.object_type);执行计划
———————————————————-
plan hash value: 49311412
————————————————————————————————
| id ?| operation ? ? ? ? ?| name ? ? ?| rows ?| bytes | cost (%cpu)| time ? ? | inst ? |in-out|
————————————————————————————————
| ? 0 | select statement ? | ? ? ? ? ? | 42747 | ?4508k| ?2152 ? (4)| 00:00:26 | ? ? ? ?| ? ? ?|
|* ?1 | ?hash join ? ? ? ? | ? ? ? ? ? | 42747 | ?4508k| ?2152 ? (4)| 00:00:26 | ? ? ? ?| ? ? ?|
| ? 2 | ? view ? ? ? ? ? ? | vw_sq_1 ? | ? ?26 | ? 286 | ?1855 ? (4)| 00:00:23 | ? ? ? ?| ? ? ?|
| ??3 | ? ?remote?? ? ? ?| ? ? ? ? ? | ? ? ? | ? ? ? | ? ? ? ? ? ?| ? ? ? ? ?| remote | r->s |
| ? 4 | ? table access full| local_tab | 73985 | ?7008k| ? 296 ? (1)| 00:00:04 | ? ? ? ?| ? ? ?|
————————————————————————————————
predicate information (identified by operation id):
—————————————————
? ?1 – access(“a”.”object_type”=”item_0″)
remote sql information (identified by operation id):
—————————————————-
? ?3 – explain plan set statement_id=’plus5801659′ into plan_table@!?for select
? ? ? ?distinct “a2″.”object_type” from “remote_big_tab” “a2″,”remote_small_tab” “a1″ where
? ? ? ?”a2″.”object_id”=”a1″.”object_id”?(accessing ‘remote’ )
? 可以看出,在远程库remote上执行的语句是两个远程表关联后,并经过查询转换(全转为大写,自己取了别名a1,a2,oracle内部自己改造为止查询distinct remote_big_tab.object_type),之后远程查询结果返回给本地,可以去远程库里查询实际的计划,走的是hash join。
2.了解cbo优化器对分布式查询的处理
? ?cbo对分布式查询的处理,也是尽量转为collocated内联视图,cbo会做如下动作:
? ?1)所有可mergeable的视图会merge
? ?2 ) cbo会测试collocated内联视图的query block
? ?3 ) 如果可以使用,就使用合并
? ?当然,cbo对分布式查询的处理,可能是不高效的,这时候得用其他的方法,比如使用hint,改造sql,改造分布式查询的方法(远程库用视图)等。
?特别当分布式查询包含下列情况,cbo可能是不高效的:
? ?1)有分组运算
? ?2)有子查询
? ?3)sql很复杂
? ?
? ?比如下面语句含有子查询:
select ?* from local_tab a,remote_big_tab@remote b,remote_small_tab@remote c
?where b.object_id=c.object_id and a.object_type=b.object_type
?and a.object_id in (select object_id from sub);
执行计划
———————————————————-
plan hash value: 252158753
———————————————————————————————————-
| id ?| operation ? ? ? ? ? ? | name ? ? ? ? ? ? | rows ?| bytes | cost (%cpu)| time ? ? | inst ? |in-out|
———————————————————————————————————-
| ? 0 | select statement ? ? ?| ? ? ? ? ? ? ? ? ?| ? ?79m| ? ?20g| ?3843 ?(46)| 00:00:47 | ? ? ? ?| ? |
|* ?1 | ?hash join ? ? ? ? ? ?| ? ? ? ? ? ? ? ? ?| ? ?79m| ? ?20g| ?3843 ?(46)| 00:00:47 | ? ? ? ?| ? |
| ? 2 | remote ? ? ? ? ? ? ?| remote_small_tab?| ?5320 | ? 431k| ? ? 8 ? (0)| 00:00:01 | remote | r->s |
|* ?3 | ? hash join ? ? ? ? ? | ? ? ? ? ? ? ? ? ?| ? 172m| ? ?31g| ?2978 ?(31)| 00:00:36 | ? ? ? ?| ? |
|* ?4 | ? ?hash join ? ? ? ? ?| ? ? ? ? ? ? ? ? ?| ?5260 | ? 565k| ? 303 ? (2)| 00:00:04 | ? ? ? ?| ? |
| ? 5 | ? ? sort unique ? ? ? | ? ? ? ? ? ? ? ? ?| ?5320 | 69160 | ? ? 5 ? (0)| 00:00:01 | ? ? ? ?| ? |
| ? 6 | ? ? ?table access full| sub ? ? ? ? ? ? ?| ?5320 | 69160 | ? ? 5 ? (0)| 00:00:01 | ? ? ? ?| ? |
| ? 7 | ? ? table access full | local_tab ? ? ? ?| 73985 | ?7008k| ? 296 ? (1)| 00:00:04 | ? ? ? ?| ? |
| ? 8 | ?remote ? ? ? ? ? ? | remote_big_tab | ?1479k| ? 119m| ?1819 ? (2)| 00:00:22 | remote | r->s |
———————————————————————————————————-
predicate information (identified by operation id):
—————————————————
? ?1 – access(“b”.”object_id”=”c”.”object_id”)
? ?3 – access(“a”.”object_type”=”b”.”object_type”)
? ?4 – access(“a”.”object_id”=”object_id”)remote sql information (identified by operation id):
—————————————————-
?2 – select “object_name”,”subobject_name”,”object_id”,”data_object_id”,”object_type”,”created”,
? ? ? ?”last_ddl_time”,”timestamp”,”status”,”temporary”,”generated”,”secondary” from “remote_small_tab”
? ? ? ?”c” (accessing ‘remote’ )
? ?8 – select “owner”,”object_name”,”subobject_name”,”object_id”,”data_object_id”,”object_type”,”c
? ? ? ?reated”,”last_ddl_time”,”timestamp”,”status”,”temporary”,”generated”,”secondary” from
? ? ? ?”remote_big_tab” “b” (accessing ‘remote’ )
?
?通过计划可以看到remote有两个,两张远程表无法做collocated inline view运算。
?
? 再比如下面的语句,有分组运算:
select ?* from local_tab a,remote_big_tab@remote b,(select max(object_id) object_id from remote_small_tab@remote c group by c.object_type) c
where b.object_id=c.object_id and a.object_type=b.object_type;执行计划
———————————————————-
plan hash value: 2122363341
—————————————————————————————————–
| id ?| operation ? ? ? ? ?| name ? ? ? ? ? | rows ?| bytes | cost (%cpu)| time ? ? | inst ? |in-out|
—————————————————————————————————–
| ? 0 | select statement ? | ? ? ? ? ? ? ? ?| ?2321k| ? 431m| ?2144 ? (3)| 00:00:26 | ? ? ? ?| ? ? ?|
|* ?1 | ?hash join ? ? ? ? | ? ? ? ? ? ? ? ?| ?2321k| ? 431m| ?2144 ? (3)| 00:00:26 | ? ? ? ?| ? ? ?|
|* ?2 | ? hash join ? ? ? ?| ? ? ? ? ? ? ? ?| ?1412 | ? 135k| ?1836 ? (3)| 00:00:23 | ? ? ? ?| ? ? ?|
| ? 3 | ? ?view ? ? ? ? ? ?| ? ? ? ? ? ? ? ?| ? ?11 | ? 143 | ? ? 9 ?(12)| 00:00:01 | ? ? ? ?| ? ? ?|
| ? 4 | ? remote ? ? ? | ? ? ? ? ? ? ? ?| ? ? ? | ? ? ? | ? ? ? ? ? ?| ? ? ? ? ?| remote | r->s |
| ? 5 | ?remote ? ? ? ? ?| remote_big_tab?| ?1479k| ? 119m| ?1819 ? (2)| 00:00:22 | remote | r->s |
| ? 6 | ? table access full| local_tab ? ? ?| 73985 | ?7008k| ? 296 ? (1)| 00:00:04 | ? ? ? ?| ? ? ?|
—————————————————————————————————–
predicate information (identified by operation id):
—————————————————
? ?1 – access(“a”.”object_type”=”b”.”object_type”)
? ?2 – access(“b”.”object_id”=”c”.”object_id”)
remote sql information (identified by operation id):
—————————————————-
? ?4 – explain plan set statement_id=’plus5801659′ into plan_table@! for select
? ? ? ?max(“a1″.”object_id”) from “remote_small_tab” “a1″ group by “a1″.”object_type” (accessing
? ? ? ?’remote’ )
? ?5 – select “owner”,”object_name”,”subobject_name”,”object_id”,”data_object_id”,”object_typ
? ? ? ?e”,”created”,”last_ddl_time”,”timestamp”,”status”,”temporary”,”generated”,”secondary” from
? ? ? ?”remote_big_tab” “b” (accessing ‘remote’ )
通过计划看出,将远程表进行分组运算后,传输给本地库,然后大表传输给本地库,之后做hash join,这是不高效的。运行时间:已用时间: ?00: 02: 12.22
?可以改造分布式查询,手动组织collocated inline view,在远程库建立view:
create or replace view v_remote
as
select ?b.* from remote_big_tab b,(select max(object_id) object_id from remote_small_tab c group by c.object_type) c
where b.object_id=c.object_id;?查询改为:
?select ? * from local_tab a,v_remote@remote v where a.object_type=v.object_type;
sql> select ? * from local_tab a,v_remote@remote v where a.object_type=v.object_type;
已选择1727104行。
已用时间: ?00: 01: 02.81
执行计划
———————————————————-
plan hash value: 2216230941
————————————————————————————————
| id ?| operation ? ? ? ? ?| name ? ? ?| rows ?| bytes | cost (%cpu)| time ? ? | inst ? |in-out|
————————————————————————————————
| ? 0 | select statement ? | ? ? ? ? ? | ? ? 1 | ? 274 | ? 299 ? (2)| 00:00:04 | ? ? ? ?| ? ? ?|
|* ?1 | ?hash join ? ? ? ? | ? ? ? ? ? | ? ? 1 | ? 274 | ? 299 ? (2)| 00:00:04 | ? ? ? ?| ? ? ?|
| ? 2 | remote ? ? ? ? ? | v_remote| ? ? 1 | ? 177 | ? ? 2 ? (0)| 00:00:01 | remote | r->s |
| ? 3 | ? table access full| local_tab | 73985 | ?7008k| ? 296 ? (1)| 00:00:04 | ? ? ? ?| ? ? ?|
————————————————————————————————
predicate information (identified by operation id):
—————————————————
? ?1 – access(“a”.”object_type”=”v”.”object_type”)
?通过计划可以看出,现在是远程表做整体操作之后才返回到本地了。
?3.使用hint,特别是driving_site hint
? 对远程表可以使用hint,比如parallel,use_nl,use_hash,full等。
? driving_site hint能够指定执行计划在远程还是本地做,比如下面使用driving_site(b),那么原来的远程表就相当于本地表,本地表要传输给remote库,主计划在remote库上执行
select/*+driving_site(b)*/ ?* from local_tab a,remote_big_tab@remote b,(select max(object_id) object_id from remote_small_tab@remote c group by c.object_type) c
where b.object_id=c.object_id and a.object_type=b.object_type;
? ?当然,如果是driving_site(a)那么就是本地驱动的,默认的是本地驱动的。
? ?
? ?使用driving_site,特别是本地小结果集,远程大结果集的时候,总体结果集较小,希望计划在远程驱动,这样远程执行完毕,将结果集传输到本地,这样避免大结果集的传输。
? ?
? ?例1:
?? ? ? 小表9998条,大表3169376条记录,远程大表sub_id,acc_id上联合索引
sql> ?select ?count(*) ?from small_tab_local a, big_tab_remote@remote b
? 2 ? where a.sub_id=b.sub_id and a.acc_id=b.acc_id;
?
? ? ?859
已用时间: ?00: 00: 50.76执行计划
———————————————————-
plan hash value: 1507576754
——————————————————————————————————–
| id ?| operation ? ? ? ? ? ?| name ? ? ? ? ? ?| rows ?| bytes | cost (%cpu)| time ? ? | inst ? |in-out|
——————————————————————————————————–
| ? 0 | select statement ? ? | ? ? ? ? ? ? ? ? | ? ? 1 | ? ?41 | ? ?44 ? (3)| 00:00:01 | ? ? ? ?| ? ? ?|
| ? 1 | ?sort aggregate ? ? ?| ? ? ? ? ? ? ? ? | ? ? 1 | ? ?41 | ? ? ? ? ? ?| ? ? ? ? ?| ? ? ? ?| ? ? ?|
| ? 2 | merge join ? ? ? | ? ? ? ? ? ? ? ? | ?9998 | ? 400k| ? ?44 ? (3)| 00:00:01 | ? ? ? ?| ? ? ?|
| ? 3 | ?remote ? ? ? ? ? ?| big_tab_remote ?| ?6771k| ? 167m| ?26 ? (0)| 00:00:01 | remote | r->s |
|* ?4 | ? ?sort join ? ? ? ? | ? ? ? ? ? ? ? ? | ?9998 | ? 146k| ? ?18 ? (6)| 00:00:01 | ? ? ? ?| ? ? ?|
| ? 5 | ? ? table access full| small_tab_local | ?9998 | ? 146k| ? ?17 ? (0)| 00:00:01 | ? ? ? ?| ? ? ?|
——————————————————————————————————–
predicate information (identified by operation id):
—————————————————
? ?4 – access(“a”.”sub_id”=”b”.”sub_id” and “a”.”acc_id”=”b”.”acc_id”)
? ? ? ?filter(“a”.”acc_id”=”b”.”acc_id” and “a”.”sub_id”=”b”.”sub_id”)
remote sql information (identified by operation id):
—————————————————-
? ?3 – select “sub_id”,”acc_id” from “big_tab_remote” “b” order by “sub_id”,”acc_id”
? ? ? ?(accessing ‘remote’ )
? ? 查询876条数据,耗时50s,显然将大结果集拉到本地做运算是不好的,因为本地表很小,远程大表有索引,如果能在远端执行,并走nl,那么显然效率非常好。使用driving_site hint改造查询如下:
select/*+driving_site(b) ?ordered use_nl(b)*/ ?count(*) from small_tab_local a, big_tab_remote@remote b
?where a.sub_id=b.sub_id and a.acc_id=b.acc_id;计划如下:
———————————————————————————————
| id ?| operation ? ? ? ? ? ? ?| name ? ? ? ? ? ? ? | rows ?| bytes | cost ?| inst ? |in-out|
———————————————————————————————
| ? 0 | select statement remote| ? ? ? ? ? ? ? ? ? ?| ? ? 1 | ? ?52 | 10009 | ? ? ? ?| ? ? ?|
| ? 1 | sort aggregate ? ? ? ? | ? ? ? ? ? ? ? ? ? ?| ? ? 1 | ? ?52 | ? ? ? | ? ? ? ?| ? ? ?|
| ? 2 |?nested loops ? ? ? ? ? | ? ? ? ? ? ? ? ? ? ?| ? 681 | 35412 | 10009 | ? ? ? ?| ? ? ?|
| ? 3 |?remote ? ? ? ? ? ? ? ? | ? ? ? ? ? ? ? ? ? ?| ?9998?| ? 253k| ? ?11 | ? ? ?! | r->s |
|* ?4 | index range scan ? ? ? | idx_big_tab_remote | ? ? 1 | ? ?26 | ? ? 1 | mzt~ | ? ? ?|
———————————————————————————————
predicate information (identified by operation id):
—————————————————
? ?4 – access(“a2″.”sub_id”=”a1″.”sub_id” and “a2″.”acc_id”=”a1″.”acc_id”)
remote sql information (identified by operation id):
—————————————————-
?3 – select “sub_id”,”acc_id” from “small_tab_local” “a2″ (accessing ‘!’ )
? ? ?现在主计划是在远端remote上执行的,本地表small_tab_local变成了远程表,会讲small_tab_local结果集送到远端,只查询了sub_id,acc_id,然后作为驱动表,与远端表做nl运算,
计划里可以看到远端表走索引了,最后将远端结果返回到本地。(事实上这里的远端库与本地库换了)
? driving_site hint注意点:
driving_site对dml无效,dml以目标表所在库驱动sql计划。比如下面的driving_site失效,后面的hint还是有效的。
? ?
? create table test_cnt (cnt number);
? insert into test_cnt
? select/*+driving_site(b) ordered use_nl(b)*/ ?count(*) from small_tab_local a, big_tab_remote@remote b
? where a.sub_id=b.sub_id and a.acc_id=b.acc_id;
已用时间:00: 01: 31.48执行计划
———————————————————-
plan hash value: 259989953
————————————————————————————————————
| id ?| operation ? ? ? ? ? ? ? ?| name ? ? ? ? ? ?| rows ?| bytes | cost (%cpu)| time ? ? | inst ? |in-out|
————————————————————————————————————
| ? 0 | insert statement ? ? ? ? | ? ? ? ? ? ? ? ? | ? ? 1 | ? ?41 | 10035 ? (1)| 00:02:01 | ? ? ? ?| ? ? ?|
| ? 1 | ?load table conventional | test_cnt ? ? ? ?| ? ? ? | ? ? ? | ? ? ? ? ? ?| ? ? ? ? ?| ? ? ? ?| ? ? ?|
| ? 2 | ? sort aggregate ? ? ? ? | ? ? ? ? ? ? ? ? | ? ? 1 | ? ?41 | ? ? ? ? ? ?| ? ? ? ? ?| ? ? ? ?| ? ? ?|
| ? 3 | ?nested loops ? ? ?| ? ? ? ? ? ? ? ? | ?9998 | ? 400k| 10035 ? (1)| 00:02:01 | ? ? ? ?| ? ? ?|
| ? 4 | ? ? table access full ? ?| small_tab_local | ?9998 | ? 146k| ? ?17 ? (0)| 00:00:01 | ? ? ? ?| ? ? ?|
| ? 5 | ? ? remote ? ? ? ? ? ? ? | big_tab_remote ?| ? ? 1 | ? ?26 | ? ? 1 ? (0)| 00:00:01 | remote | r->s |
————————————————————————————————————
remote sql information (identified by operation id):
—————————————————-
? ?5 – select?/*+ opaque_transform use_nl (“b”) */?”sub_id”,”acc_id” from “big_tab_remote” “b”
? ? ? ?where?:1=”sub_id” and :2=”acc_id”?(accessing ‘remote’ )
? 语句执行1分31s,driving_site hint失效,但是后面的nl没有失效,可以从计划中看出类似绑定变量的东西,这实际对于每个small_tab_local的结果集的行,将sub_id,acc_id传给远端表big_tab_remote,也就是:1,:2,这样本地的表筛选出多少行,远程语句?select?/*+ opaque_transform use_nl (“b”) */?”sub_id”,”acc_id” from “big_tab_remote” “b”
?where?:1=”sub_id” and :2=”acc_id” 就执行多少次。
?这里本地表9998条,无过滤条件,因此远程表语句运行了9998次,虽然远程查询也是走索引的,但是sql被执行了9998次,是非常影响性能的。可以去远程库查询下:
?
sql> select sql_text,executions from v$sql where sql_text like ‘%select /*+ use_nl (“b”) */ “sub_id”,”acc_id” from “big_tab_remote”%’
? 2 ?/
?
sql_text ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? executions
——————————————————————————– ————————————————————–
select /*+ use_nl (“b”) */ “sub_id”,”acc_id” from “big_tab_remote” “b” where :1= ? ? 9998
? ?这里driving_site失效,但是后面的nl还有效,远程表执行的次数是small_tab_local表的数量(因为这里没有谓词过滤small_tab_local),可以使用其他hint,比如。
? insert into test_cnt
? select/*+ordered use_hash(b)*/ ? count(*) from small_tab_local a, big_tab_remote@remote b
? where a.sub_id=b.sub_id and a.acc_id=b.acc_id;
当然效率不一定很好,因为这里由远程驱动效率最好,为了不想driving_site失效,可以使用pl/sql(这里是只查询数量,如果查询结果集可以使用pl/sql批处理插入)。
begin
? ? for i in (select/*+driving_site(b) ordered use_nl(b)*/ ?count(*) cnt from small_tab_local a, big_tab_remote@remote b
? ? ? ? ? ? ? ? ? ?where a.sub_id=b.sub_id and a.acc_id=b.acc_id)
? ?loop
? ? ?insert into test_cnt values(i.cnt);
? end loop;
? commit;
end;
已用时间:00: 00: 00.89
? ? ?
? 例2:
? ? ? 查询语句:
select * from?v_remote?where object_id in (
select c.object_id from c where c.object_name
? ? ? ? in (select d.object_name from d where d.object_id=11)
);
?比较慢,返回32行,需要10来秒。其中v_remote是个视图,此视图连接到远程表,其中远程的两张表的object_id都有索引:
create or replace view v_remote
as
select object_name,object_id,object_type from a@remote ?
union all
select ?object_name,object_id,object_type from b@remote;两表记录数如下:
sql> select count(*) from a;
?
? count(*)
———-
? 369888
sql> select count(*) from b;
?
? count(*)
———-
? ? 5323
? c和d是本地表,d.object_id以及c.object_name有索引。单独查询很快,
–单独本地语句消耗时间00: 00: 00.01
sql> select c.object_id from c where c.object_name in (select d.object_name from d where d.object_id=11);
已用时间: ?00: 00: 00.01
执行计划
———————————————————-
plan hash value: 2528799293
—————————————————————————————-
| id ?| operation ? ? ? ? ? ? ? ? ? ? ?| name ?| rows ?| bytes | cost (%cpu)| time ? ? |
—————————————————————————————-
| ? 0 | select statement ? ? ? ? ? ? ? | ? ? ? | ? ? 2 | ? ?94 | ? ? 6 ?(17)| 00:00:01 |
| ? 1 | ?nested loops ? ? ? ? ? ? ? ? ?| ? ? ? | ? ? ? | ? ? ? | ? ? ? ? ? ?| ? ? ? ? ?|
| ? 2 | ? nested loops ? ? ? ? ? ? ? ? | ? ? ? | ? ? 2 | ? ?94 | ? ? 6 ?(17)| 00:00:01 |
| ? 3 | ? ?sort unique ? ? ? ? ? ? ? ? | ? ? ? | ? ? 1 | ? ?17 | ? ? 2 ? (0)| 00:00:01 |
| ? 4 | ? ? table access by index rowid| d ? ? | ? ? 1 | ? ?17 | ? ? 2 ? (0)| 00:00:01 |
|* ?5 | ? ? ?index range scan ? ? ? ? ?| idx_d | ? ? 1 | ? ? ? | ? ? 1 ? (0)| 00:00:01 |
|* ?6 | ? ?index range scan ? ? ? ? ? ?| idx_c | ? ? 2 | ? ? ? | ? ? 2 ? (0)| 00:00:01 |
| ? 7 | ? table access by index rowid ?| c ? ? | ? ? 2 | ? ?60 | ? ? 3 ? (0)| 00:00:01 |
—————————————————————————————-
predicate information (identified by operation id):
—————————————————
? ?5 – access(“d”.”object_id”=11)
? ?6 – access(“c”.”object_name”=”d”.”object_name”)–单独远程语句消耗时间?00: 00: 00.06
sql> select * from v_remote where object_id=11;
已选择32行。
已用时间: ?00: 00: 00.06
执行计划
———————————————————-
plan hash value: 1788691278
————————————————————–
| id ?| operation ? ? ? ?| name | cost (%cpu)| inst ? |in-out|
————————————————————–
| ? 0 | select statement | ? ? ?| ? ? 0 ? (0)| ? ? ? ?| ? ? ?|
| ? 1 |remote ? ? ? ? ?| ? ? ?| ? ? ? ? ? ?| remote | r->s?|
————————————————————–
remote sql information (identified by operation id):
—————————————————-
?1 – explain plan set statement_id=’plus5821518′ into plan_table@!
? ? ? ?for select “a1″.”object_name”,”a1″.”object_id”,”a1″.”object_type” from
? ? ? ?( (select “a4″.”object_name” “object_name”,”a4″.”object_id”
? ? ? ?”object_id”,”a4″.”object_type” “object_type” from “a” “a4″ where
? ? ? ?”a4″.”object_id”=11) union all ?(select “a3″.”object_name”
? ? ? ?”object_name”,”a3″.”object_id” “object_id”,”a3″.”object_type”
? ? ? ?”object_type” from “b” “a3″ where “a3″.”object_id”=11)) “a1″ (accessing
? ? ? ?’remote’ )
–联合查询消耗时间00: 00: 10.95
sql> select * from v_remote where object_id in (
? 2 ?select c.object_id from c where c.object_name in (select d.object_name from d where d.object_id=11)
? 3 ?);
已选择32行。
已用时间: ?00: 00: 10.95
执行计划
———————————————————-
plan hash value: 2118901120
————————————————————————————————————-
| id ?| operation ? ? ? ? ? ? ? ? ? ? ? ?| name ? ? | rows ?| bytes | cost (%cpu)| time ? ? | inst ? |in-out|
————————————————————————————————————-
| ? 0 | select statement ? ? ? ? ? ? ? ? | ? ? ? ? ?| ? ?65 | ?6695 | ? 471 ? (3)| 00:00:06 | ? ? ? ?| ? ? ?|
|* ?1 | ?hash join ? ? ? ? ? ? ? ? ? ? ? | ? ? ? ? ?| ? ?65 | ?6695 | ? 471 ? (3)| 00:00:06 | ? ? ? ?| ? ? ?|
| ? 2 | ? view ? ? ? ? ? ? ? ? ? ? ? ? ? | vw_nso_1 | ? ? 2 | ? ?26 | ? ? 6 ?(17)| 00:00:01 | ? ? ? ?| ? ? ?|
| ? 3 | ? ?hash unique ? ? ? ? ? ? ? ? ? | ? ? ? ? ?| ? ? 2 | ? ?94 | ? ? 6 ?(17)| 00:00:01 | ? ? ? ?| ? ? ?|
| ? 4 | ? ? nested loops ? ? ? ? ? ? ? ? | ? ? ? ? ?| ? ? ? | ? ? ? | ? ? ? ? ? ?| ? ? ? ? ?| ? ? ? ?| ? ? ?|
| ? 5 | ? ? ?nested loops ? ? ? ? ? ? ? ?| ? ? ? ? ?| ? ? 2 | ? ?94 | ? ? 5 ? (0)| 00:00:01 | ? ? ? ?| ? ? ?|
| ? 6 | ? ? ? table access by index rowid| d ? ? ? ?| ? ? 1 | ? ?17 | ? ? 2 ? (0)| 00:00:01 | ? ? ? ?| ? ? ?|
|* ?7 | ? ? ? ?index range scan ? ? ? ? ?| idx_d ? ?| ? ? 1 | ? ? ? | ? ? 1 ? (0)| 00:00:01 | ? ? ? ?| ? ? ?|
|* ?8 | ? ? ? index range scan ? ? ? ? ? | idx_c ? ?| ? ? 2 | ? ? ? | ? ? 2 ? (0)| 00:00:01 | ? ? ? ?| ? ? ?|
| ? 9 | ? ? ?table access by index rowid | c ? ? ? ?| ? ? 2 | ? ?60 | ? ? 3 ? (0)| 00:00:01 | ? ? ? ?| ? ? ?|
| ?10 | ? view ? ? ? ? ? ? ? ? ? ? ? ? ? | v_remote | ? 375k| ? ?32m| ? 462 ? (2)| 00:00:06 | ? ? ? ?| ? ? ?|
|11 | ? ?union-all ? ? ? ? ? ? ? ? ? ? | ? ? ? ? ?| ? ? ? | ? ? ? | ? ? ? ? ? ?| ? ? ? ? ?| ? ? ? ?| ? ? ?|
| ?12 | ? ? remote ? ? ? ? ? ? ? ? ? ? ? | a ? ? ? ?| ? 369k| ? ?29m| ? 454 ? (2)| 00:00:06 | remote | r->s |
| ?13 | ? ? remote ? ? ? ? ? ? ? ? ? ? ? | b ? ? ? ?| ?5323 | ? 431k| ? ? 8 ? (0)| 00:00:01 | remote | r->s?|
————————————————————————————————————-
predicate information (identified by operation id):
—————————————————
? ?1 – access(“object_id”=”object_id”)
? ?7 – access(“d”.”object_id”=11)
? ?8 – access(“c”.”object_name”=”d”.”object_name”)
remote sql information (identified by operation id):
—————————————————-
12 – select “object_name”,”object_id”,”object_type” from “a” “a” (accessing ‘remote’ )
? 13 – select “object_name”,”object_id”,”object_type” from “b” “b” (accessing ‘remote’ )
?
? ? ?单独查询很快,为什么联合查询就慢了呢?原因在于:
?
单独执行远程查询 本地与远程混合查询
直接执行视图,并将object_id=11谓词推入到视图中,走索引,最后只将32行结果返回给本地 从计划中可以看到,本地查询与远程查询做hash join,但是访问远程的sql是没有谓词的,这样必然全表从远程拉到本地,因为行数较多,所以慢
?
? ??因此,优化此混合查询的语句可以由多种办法(比如本地查询的数量较少,可以采用上面的方法,本地与远程查询拆分为2条语句),另外就是可以使用driving_site hint,将主计划推到远程库去执行,本地的结果集少,推到远程,远程视图走索引,效率高。如下:
–耗时已用时间: ?00: 00: 00.08
sql> select/*+driving_site(v_remote.a)*/ * from v_remote where object_id in (
? 2 ?select c.object_id from c where c.object_name in (select d.object_name from d where d.object_id=11
? 3 ?);
已选择32行。
已用时间: ?00: 00: 00.08
—————————————————————————————
| id ?| operation ? ? ? ? ? ? ? ? ?| name ? ? | rows ?| bytes | cost ?| inst ? |in-out|
—————————————————————————————
| ? 0 | select statement remote ? ?| ? ? ? ? ?| ? ?15 | ?1425 | ? 109 | ? ? ? ?| ? ? ?|
| ? 1 | nested loops ? ? ? ? ? ? ? | ? ? ? ? ?| ? ?15 | ?1425 | ? 109 | ? ? ? ?| ? ? ?|
| ? 2 | sort unique ? ? ? ? ? ? ? ?| ? ? ? ? ?| ? ? ? | ? ? ? | ? ? ? | ? ? ? ?| ? ? ?|
| ? 3 | view ? ? ? ? ? ? ? ? ? ? ? | vw_nso_1 | ? ? 3 | ? ?39 | ? ? 8 | mzt~ | ? ? ?|
| ? 4 |?remote ? ? ? ? ? ? ? ? ? ? | ? ? ? ? ?| ? ? ? | ? ? ? | ? ? ? | ? ? ?! | r->s?|
| ? 5 | view ? ? ? ? ? ? ? ? ? ? ? | ? ? ? ? ?| ? ? 5 | ? 410 | ? ?33 | ? ? ? ?| ? ? ?|
| ? 6 |?union-all partition ? ? ? ?| ? ? ? ? ?| ? ? ? | ? ? ? | ? ? ? | ? ? ? ?| ? ? ?|
| ? 7 | table access by index rowid| a ? ? ? ?| ? ?32 | ? 960 | ? ?35 |mzt~ | ? ? ?|
|* ?8 | index range scan ? ? ? ? ? | idx_a ? ?| ? ?32 | ? ? ? | ? ? 3 |mzt~ | ? ? ?|
| ? 9 | table access by index rowid| b ? ? ? ?| ? ? 1 | ? ?32 | ? ? 2 | mzt~ | ? ? ?|
|* 10 | index range scan ? ? ? ? ? | idx_b ? ?| ? ? 1 | ? ? ? | ? ? 1 | mzt~ | ? ? ?|
—————————————————————————————predicate information (identified by operation id):
—————————————————
? ?8 – access(“a6″.”object_id”=”vw_nso_1″.”object_id”)
? 10 – access(“a5″.”object_id”=”vw_nso_1″.”object_id”)
remote sql information (identified by operation id):
—————————————————-
? ?4 – select /*+ */ “a1″.”object_id” from “d” “a2″,”c” “a1″ where
? ? ? ?”a1″.”object_name&
其它类似信息

推荐信息