更新时间:2025-07-08 GMT+08:00
分享

Drive方式的Hint

功能描述

优化器在进行查询计划生成时,会使用动态规划或遗传基因算法枚举可能的Join路径,并在其中选择最优的路径,当连接表的数目增大时,会导致搜索空间的过度膨胀。对于此类情况,可以通过Drive Hint指定查询中的事实表,使用启发式搜索的方式减少路径搜索空间的范围。该hint只有当GUC参数join_search_mode设置为heuristic时生效。

语法格式

1
[no] drive (table)

参数说明

  • no表示hint对应的表为非Drive表。
  • table表示hint指定的表,只能指定一个表,如果表存在别名应优先使用别名进行hint。

示例

创建表t1、t2、t3:

1
2
3
create table t1(a1 int,b1 int,c1 int,d1 int);
create table t2(a2 int,b2 int,c2 int,d2 int);
create table t3(a3 int,b3 int,c3 int,d3 int);

原语句为:

1
explain select * from t1,t2,t3 where t1.b1=t2.b2 and t1.c1=t3.c3 and t2.d2=t3.d3;

上述查询中,可以通过指定t3表为drive表,使得t3表优先和其它表join,减少计划生成路径搜索,同时更改join顺序。:
explain select /*+ drive(t3) */ * from t1, t2, t3 where t1.b1=t2.b2 and t1.c1=t3.c3 and t2.d2=t3.d3;

使用hint后的效果:

当GUC参数join_search_mode取值为heuristic且连接表个数超过from_collapse_limit时,优化器会自动识别Drive表,并在计划中显示。如果识别优化器最终选择的Drive不对,可以使用no drive hint进行纠偏。

相关文档