|
To simplify the discussion, let us focus on Nested Loop and Sort Merge first. Hash Join is quit difficult to discuss here.
Select * from A, B
Where A1=B1
If A1 and B1 are B-Tree indexed on table A and B. Let consider an isolated case, if you have unlimited memory and everything can be performed in memory, now we count the number of operation for Nested Loop.
We assume that A has 1,000 records and B has 200,000 records. Let us calculate number of operations for driving path A-> B. Which means that we open table A, for each record from A to fetch (scan) table B.
Operations= 1000*LN(200,000)/2 ~ 6103
Where LN(200,000) is depth of the B-tree index, LN(200,000)/2 is the average operation to match the right node in tree.
Let’s calculated the driving path for B->A
Operations= 200,000*LN(1,000)/2 ~ 690775
So, it is almost 113 time slower then the A->B. So, normally, the Nested Loop problem is caused by wrong driving path.
Let’s consider the following SQL, you will know more why Oracle and other database cannot always make right decision.
Select * from A, B
Where A1=B1 and A1<:var
Actually Oracle will improve your SQL by adding a new condition.
Select * from A, B
Where A1=B1 and A1<:var and B1<:var
So, A->B or B-> A are also be consider, :var is an unknown factor, it also hard to predict how many records will be filtered by A1<:var or B1<:var . So, the decision is not always correct. Sometimes you change it Hash Join may find a big improvement, that may not be caused by Hash Join operation, but it may caused by Hash changing your wrong driving path only. As it is too long, I would leave it here and discuss Sort Merge next time.
Forgive me, if any mistakes I made here, no prove read before publish! |
|