在多表联合查询中,当使用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 the ORDERED hint, then it overrides all LEADING hints.


通过Leading 和 use_hash 提示连用,我们可以巧妙的影响SQL中表和结果集的Join顺序.


我们通过如下示例看一下这个提示是如何影响SQL执行的:


094218976.png



我们看到,通过这两个Hints的联合使用,该查询首先对T_MAX和T_MIDDLE表进行HASH JOIN,再以这个结果集同T_SMALL进行HASH JION.


单纯通过Ordered和USE_HASH提示通常是达不到这个效果的:


094231496.png


这是Leading  Hints在Oracle9i中的一个特殊用法.