分散式查詢優化SERIAL_FROM_REMOTE

zhouwf0726發表於2019-05-08

SQL> set autotrace on
SQL> select * from zhouwf.temp1@cslndc,zhouwf.temp2 where temp1.id=temp2.id;

ID MC
---------- ------------------------------------------------------------
ID MC
---------- ------------------------------------------------------------
1 1
1 1

2 2
2 2

3 3
3 3


ID MC
---------- ------------------------------------------------------------
ID MC
---------- ------------------------------------------------------------
4 4
4 4

5 5
5 5

6 6
6 6


ID MC
---------- ------------------------------------------------------------
ID MC
---------- ------------------------------------------------------------
7 7
7 7

8 8
8 8

9 9
9 9


ID MC
---------- ------------------------------------------------------------
ID MC
---------- ------------------------------------------------------------
10 10
10 10

11 11
11 11

12 12
12 12


ID MC
---------- ------------------------------------------------------------
ID MC
---------- ------------------------------------------------------------
13 13
13 13

14 14
14 14

15 15
15 15


ID MC
---------- ------------------------------------------------------------
ID MC
---------- ------------------------------------------------------------
16 16
16 16

17 17
17 17

18 18
18 18


ID MC
---------- ------------------------------------------------------------
ID MC
---------- ------------------------------------------------------------
19 19
19 19

20 20
20 20

100 100
100 100


已選擇21行。


執行計劃
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE
1 0 TABLE ACCESS (BY INDEX ROWID) OF 'TEMP2'
2 1 NESTED LOOPS
3 2 REMOTE* CSLNDC
4 2 INDEX (RANGE SCAN) OF 'IDX_TEMP2' (NON-UNIQUE)


3 SERIAL_FROM_REMOTE SELECT "ID","MC" FROM "ZHOUWF"."TEMP1" "TEMP
1"



統計資訊
----------------------------------------------------------
0 recursive calls
0 db block gets
10 consistent gets
0 physical reads
0 redo size
995 bytes sent via SQL*Net to client
522 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
21 rows processed

SQL>

對於執行計劃:SERIAL_FROM_REMOTE

http://yaanzy.itpub.net/post/1263/31064

在分散式查詢中,對於分佈在兩個節點上的兩張表進行巢狀迴圈連線將會非常慢,這是由於Oracle將
所有的遠端資料傳到本地來處理。所以對於這類查詢最好換成排序合併連線或者hash連線。

下面例子中的表aaa、bbb分別位於兩個庫,aaa所在庫通過資料庫鏈db244訪問bbb表:

create table aaa as select * from dba_objects;
create table bbb as select * from dba_objects;

1、巢狀迴圈連線

SQL> select count(*) from aaa a
where exists
(select * from bbb@db244 b where a.object_id=b.object_id);

COUNT(*)
----------
27165

Elapsed: 00:04:04.74

Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE
1 0 SORT (AGGREGATE)
2 1 FILTER
3 2 TABLE ACCESS (FULL) OF 'AAA'
4 2 REMOTE* DB244.US.ORACLE.COM
4 SERIAL_FROM_REMOTE SELECT "OBJECT_ID" FROM "BBB" "B" WHERE "OBJECT_ID"=:1

Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
437 consistent gets
0 physical reads
0 redo size
381 bytes sent via SQL*Net to client
503 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed

2、排序合併連線

SQL> select count(*) from aaa a
where a.object_id in
(select object_id from bbb@db244 b);

COUNT(*)
----------
27165

Elapsed: 00:00:00.59

Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE
1 0 SORT (AGGREGATE)
2 1 MERGE JOIN
3 2 SORT (JOIN)
4 3 TABLE ACCESS (FULL) OF 'AAA'
5 2 SORT (JOIN)
6 5 VIEW OF 'VW_NSO_1'
7 6 SORT (UNIQUE)
8 7 REMOTE* DB244.US.ORACLE.COM
8 SERIAL_FROM_REMOTE SELECT "OBJECT_ID" FROM "BBB" "B"


Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
437 consistent gets
0 physical reads
0 redo size
381 bytes sent via SQL*Net to client
503 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
3 sorts (memory)
0 sorts (disk)
1 rows processed

3、通過提示改用hash連線

SQL> select /*+ use_hash(a,b) */count(*) from aaa a
where exists
(select * from bbb@db244 b
where a.object_id=b.object_id);

COUNT(*)
----------
27165

Elapsed: 00:00:00.51

Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=91 Card=1 Bytes=26)
1 0 SORT (AGGREGATE)
2 1 HASH JOIN (SEMI) (Cost=91 Card=27172 Bytes=706472)
3 2 TABLE ACCESS (FULL) OF 'AAA' (Cost=47 Card=27172 Bytes=353236)

4 2 VIEW OF 'VW_SQ_1' (Cost=42 Card=32509 Bytes=422617)
5 4 REMOTE* (Cost=42 Card=32509 Bytes=422617)
DB244.US.ORACLE.COM
5 SERIAL_FROM_REMOTE SELECT "OBJECT_ID" FROM "BBB" "B"


Statistics
----------------------------------------------------------
3 recursive calls
0 db block gets
468 consistent gets
0 physical reads
0 redo size
381 bytes sent via SQL*Net to client
503 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
1 rows processed

http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:456820211101

Followup:
the query plan showed us that -- two remote operations followed by a filter.
gives us the remote query that was executed and all.

it is basically taking:

2 select tracknum, status, transdate, insseq
10:09:24 3 from workhistory@wkl
10:09:24 4 where tracknum in (select distinct tracknum from workhistory@wkl
where insseq = 142);

and executing it like this:


for x in ( select * from remote_table )
loop
query remote table again by tracknum to get tracknum and insseq

if one of the the returned insseq's is 142
then
output record
end if
end loop

Execution Plan
----------------------------------------------------------
0 INSERT STATEMENT Optimizer=CHOOSE (Cost=172346 Card=57009 By
tes=2052324)

1 0 FILTER
2 1 REMOTE* (Cost=1319 Card=57009 Bytes=2052324) WKL
3 1 REMOTE* (Cost=3 Card=1 Bytes=24) WKL


2 SERIAL_FROM_REMOTE SELECT "TRACKNUM","STATUS","TRANSDATE","INSS
EQ" FROM "WORKHISTORY" "SYS_ALIAS_1"

3 SERIAL_FROM_REMOTE SELECT "TRACKNUM","INSSEQ" FROM "WORKHISTORY
" "WORKHISTORY" WHERE "TRACKNUM"=:1


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/756652/viewspace-242245/,如需轉載,請註明出處,否則將追究法律責任。

相關文章