(ZT) 修改表连接顺序的hint

Full Hinting
Filed under: Execution plans, Hints — Jonathan Lewis @ 9:17 pm UTC Jan 16,2007

This question came up some time ago on the comp.database.oracle.server newsgroup.
It’s an interesting question, and I’m not sure I know the answer.

Is there a way to tell the optimizer to precisely follow the exact plan & access
paths you want ? You can’t specify a hint to join a result set to a table, then
the result set this join will yield with another table etc., so how can you deal
with this ?
If I have:

SELECT...
FROM    tab1, tab2, tab3, tab4
WHERE
        tab1.id = tab3.id
AND     tab3.id = tab2.id
AND     tab2.id = tab4.id
AND     {filter clauses};
;

How could I force the optimizer to first join TAB1 and TAB3 with a nested loop,
then join this result to TAB2 using a hash etc etc. ??


For the general case, I’m not sure if there is always a solution; but for this
particular example, Oracle 10g makes it particularly easy to express:

/*+
        leading (t1, t3, t2, t4)
        use_nl(t3)
        use_hash(t2) swap_join_inputs(t2)
        use_nl(t4)
*/

The leading() hint allows you to specify the join order, and the use_nl(t3)
ensures we get a nested loop from t1 to t3. We then have to join to t2 because of
the leading() hint, but the  swap_join_inputs() would make t2 the build (first) table
and the intermediate result the probe (second) table in the hash join. Finally we
specify a nested loop for the join to t4. (For 9i, just change the leading() hint
to an ordered() hint, in this case - (added 20th April: and swap the order of t2
and t3 in the from clause))

But even in 10g there are probably cases where you would have  to do things like
introduce partial order through in-line views with no_merge hints, or subquery
factoring with materialize hints to get the plan you want.

Unfortunately, one of the difficulties of exercising total control is that Oracle’s
optimizer works very hard to produce “left-deep” trees; and sometimes we know that
the best plan comes from a “bushy” tree - and it can be hard to produce a legal execution
plan which generates partial results and still joins those results efficients.

Ultimately there are a few dozen hints which exist but are not documented and a few
dozen that exist but are not documented properly, and the list keeps growing - so it
can be hard to figure out exactly how to force a particular execution plan to appear;
it’s hard to know if there are even enough hints (yet) to describe every path we might want.

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/386081/viewspace-566843/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/386081/viewspace-566843/

你可能感兴趣的:((ZT) 修改表连接顺序的hint)