rdbms 11.2.0.4
driving_sit提示的作用:作用是將本地表l推至遠程庫上和遠程表r相關聯,運算完畢後,再將結果回傳到本地。例如:
select /*+ driving_sit(r) */ * from local_tabel l,remote_tab@dblink r where l.id=r.id .
以下進行測試
--創建到遠端的dblink
create public database link to_54_102
connect to apps identified by XXX
using '192.168.54.102/abc';
-- 在本地上創建表 local_t1
create table local_t1 as select * from dba_objects where rownum<=10;
SQL> create table local_t1 as select * from dba_objects where rownum<=10;
Table created.
SQL> select count(*) from local_t1;
COUNT(*)
----------
10
SQL>
-- 在遠端創建表remote_t1,
create table remote_t1 as select * from dba_objects ;
SQL> create table remote_t1 as select * from dba_objects ;
Table created.
SQL>
SQL> select count(*) from remote_t1;
COUNT(*)
----------
88065
SQL>
-- 在本地上查詢,遠端數據庫上表remote_t1上數據88065行
SQL> select count(*) from remote_t1@to_54_102;
COUNT(*)
----------
88065
SQL>
-- 查詢,不加hint, 執行時間是0.24秒 。執行計劃中的remote對應的name是remote_t1(遠程),也就是將remote的表,取到本地,和本地表進行關聯查詢,如果遠程表的量很大的話,會對性能有一定的影響。
select * from local_t1 l ,remote_t1@to_54_102 r where l.object_id=r.object_id;
SQL> select * from local_t1 l ,remote_t1@to_54_102 r where l.object_id=r.object_id;
10 rows selected.
Elapsed: 00:00:00.24
Execution Plan
----------------------------------------------------------
Plan hash value: 1753300716
------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Inst |IN-OUT|
------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 105K| 41M| 230 (1)| 00:00:03 | | |
|* 1 | HASH JOIN | | 105K| 41M| 230 (1)| 00:00:03 | | |
| 2 | TABLE ACCESS FULL| LOCAL_T1 | 10 | 2070 | 3 (0)| 00:00:01 | | |
| 3 | REMOTE | REMOTE_T1 | 105K| 20M| 226 (1)| 00:00:03 | TO_54~ | R->S |
------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("L"."OBJECT_ID"="R"."OBJECT_ID")
Remote SQL Information (identified by operation id):
----------------------------------------------------
3 - SELECT "OWNER","OBJECT_NAME","SUBOBJECT_NAME","OBJECT_ID","DATA_OBJECT_ID","OBJEC
T_TYPE","CREATED","LAST_DDL_TIME","TIMESTAMP","STATUS","TEMPORARY","GENERATED","SECONDAR
Y","NAMESPACE","EDITION_NAME" FROM "REMOTE_T1" "R" (accessing 'TO_54_102' )
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
49 recursive calls
0 db block gets
67 consistent gets
9 physical reads
0 redo size
3386 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
5 sorts (memory)
0 sorts (disk)
10 rows processed
SQL>
-- 查詢 ,加上hint ,先刷掉shared pool和buffer pool ,查看執行計劃,執行時間是0.14秒(比上面的0.24秒少)。執行計劃中的remote對應的name是local_t1(本地),則表示本地的表被推送到遠程數據庫。
SQL> alter system flush shared_pool;
System altered.
Elapsed: 00:00:26.37
SQL> alter system flush buffer_cache;
System altered.
select /*+ driving_site(r) */ * from local_t1 l ,remote_t1@to_54_102 r where l.object_id=r.object_id;
SQL> select /*+ driving_site(r) */ * from local_t1 l ,remote_t1@to_54_102 r where l.object_id=r.object_id;
10 rows selected.
Elapsed: 00:00:00.14
Execution Plan
----------------------------------------------------------
Plan hash value: 3668660825
----------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Inst |IN-OUT|
----------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT REMOTE| | 327 | 132K| 354 (1)| 00:00:05 | | |
|* 1 | HASH JOIN | | 327 | 132K| 354 (1)| 00:00:05 | | |
| 2 | REMOTE | LOCAL_T1 | 327 | 67689 | 2 (0)| 00:00:01 | ! | R->S |
| 3 | TABLE ACCESS FULL | REMOTE_T1 | 88054 | 17M| 351 (1)| 00:00:05 | ABC | |
----------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("A2"."OBJECT_ID"="A1"."OBJECT_ID")
Remote SQL Information (identified by operation id):
----------------------------------------------------
2 - SELECT "OWNER","OBJECT_NAME","SUBOBJECT_NAME","OBJECT_ID","DATA_OBJECT_ID","OBJECT_TY
PE","CREATED","LAST_DDL_TIME","TIMESTAMP","STATUS","TEMPORARY","GENERATED","SECONDARY","NAME
SPACE","EDITION_NAME" FROM "LOCAL_T1" "A2" (accessing '!' )
Note
-----
- fully remote statement
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
64 recursive calls
0 db block gets
76 consistent gets
9 physical reads
0 redo size
3471 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
5 sorts (memory)
0 sorts (disk)
10 rows processed
SQL>
通過以上的測試,可以使用hint ,來確定數據在本地服務器上處理還是在遠程服務器上處理,從而達到最優。
參考文檔:
https://docs.oracle.com/cd/E11882_01/server.112/e41084/sql_elements006.htm#BABEGIJC
DRIVING_SITE Hint
Description of the illustration ''driving_site_hint.gif''
(See "Specifying a Query Block in a Hint", tablespec::=)
The DRIVING_SITE
hint instructs the optimizer to execute the query at a different site than that selected by the database. This hint is useful if you are using distributed query optimization.
For example:
SELECT /*+ DRIVING_SITE(departments) */ * FROM employees, departments@rsite WHERE employees.department_id = departments.department_id;
If this query is executed without the hint, then rows from departments
are sent to the local site, and the join is executed there. With the hint, the rows from employees
are sent to the remote site, and the query is executed there and the result set is returned to the local site.
https://docs.oracle.com/cd/E11882_01/server.112/e25494/ds_appdev.htm#ADMIN12206
Using the DRIVING_SITE Hint
The DRIVING_SITE
hint lets you specify the site where the query execution is performed. It is best to let cost-based optimization determine where the execution should be performed, but if you prefer to override the optimizer, you can specify the execution site manually.
Following is an example of a SELECT
statement with a DRIVING_SITE
hint:
SELECT /*+DRIVING_SITE(dept)*/ * FROM emp, [email protected] WHERE emp.deptno = dept.deptno;
https://docs.oracle.com/cd/E11882_01/server.112/e25494/ds_concepts.htm#ADMIN12139
Distributed Query Optimization
Distributed query optimization is an Oracle Database feature that reduces the amount of data transfer required between sites when a transaction retrieves data from remote tables referenced in a distributed SQL statement.
Distributed query optimization uses cost-based optimization to find or generate SQL expressions that extract only the necessary data from remote tables, process that data at a remote site or sometimes at the local site, and send the results to the local site for final processing. This operation reduces the amount of required data transfer when compared to the time it takes to transfer all the table data to the local site for processing.
Using various cost-based optimizer hints such as DRIVING_SITE
, NO_MERGE
, and INDEX
, you can control where Oracle Database processes the data and how it accesses the data.
END