分布式查询中的driving_sit提示

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 driving_site_hint.gif follows
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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章