今天接到一个朋友电话, 原来两个schema 是在一个库下面, 为 Oracle 9.2.0.8版本,
所以两个schema 表之间的连接都是本地 , 后来业务及主机性能问题, 将一个
schema 切出去成为一个数据库, 使用了较新的 Oracle 10g 版本, 原来连接的一些
表就需要使用dblink 来操作了 (dblink 是在10g 库上) , 结果运行感觉慢很多, 看了
一下, 他们9i, 10g 库都是有分析的 , 只是9i 分析不是job定期做, 而是手工, 10g 是
自动分析 .
查询了一些资料 , 发现 dblink执行有两种方式,一种是在远端数据库执行完,
然后结果返回到本地,另一种是把远程的表数据传输到本地,然后在本地执行。
DRIVING_SITE
The DRIVING_SITE hint forces query execution to be done at a different site than that selected
by Oracle. This hint can be used with either rule-based or cost-based optimization.
For example:
SELECT /*+DRIVING_SITE(departments)*/ *
FROM employees,
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, returning
the result to the local site.
This hint is useful if you are using distributed query optimization.
DRIVING_SITE 提示强制在和Oracle选择不同的一端执行语句, 这个Hint可以用在
rule-based及cost-based 优化模式下。
如果上面的语句没有 /*+DRIVING_SITE(departments)*/ 提示, 远端的表 departments
的行要被传输到local site , 在local site 执行连接语句, 如果有这个提示, 那么本地的
employees 表的行会被传到远端site, 查询在远端site执行, 然后返回结果到本地, 这个
hint 在分布式查询优化中还是有用的 。
合理使用DRIVING_SITE,可以在分布式查询中大大减少网络流量。
从他们不慢到慢的变化上可以看出应该和两方面有关系, 一个是dblink , 还有
一个就是9i 升级到 10g 后执行计划的变化 , 查看DBLINK 其实没有参照物, 因为
原来使用的是同一个库 , 不存在dblink , 所以可以从执行计划入手 .
后来加入hint 之后运行正常, 不过还是不建议多用hint , 某个SQL, 选择将表放在local
端还是remote 端对于语句运行也是有决定作用的 .
-----------------------------------------------------------------------
备注:
insert into会导致driving_site提示 (hint) 无效,那么可以用一个视图的办法解决
问题:在目标端建立一个关于本查询中无参数的视图,然后在本地通过dblink
调用视图,这样先在远程执行出结果传输到本地。
-----------------------------------------------------------------------