在多表联合查询中,当使用Ordered提示改变SQL执行计划之后,通常我们很难再次控制结果集中进一步Join的顺序.
这时候我们可以使用Oracle提供的另外一个Hints: Leading 提示.
这个Hints在Oracle9i中的含义为:
The
LEADING
hint causes Oracle to use the specified table as the first table in the join order.If you specify two or more
LEADING
hints on different tables, then all of them are ignored. If you specify theORDERED
hint, then it overrides allLEADING
hints.
通过Leading 和 use_hash 提示连用,我们可以巧妙的影响SQL中表和结果集的Join顺序.
我们通过如下示例看一下这个提示是如何影响SQL执行的:
SQL> SELECT /*+ leading(t_max) use_hash(t_max t_middle) */ COUNT (*) Execution Plan |
我们看到,通过这两个Hints的联合使用,该查询首先对T_MAX和T_MIDDLE表进行HASH JOIN,再以这个结果集同T_SMALL进行HASH JION.