In order to exchange the driving
table, I tried the query:
EXPLAIN ANALYZE
SELECT D.Department_Name,
E.Last_Name, E.First_Name
FROM Departments D, Employees E
WHERE
D.Department_Id=E.Department_Id
AND E.Exempt_Flag='Y'
AND D.US_Based_Flag='Y'
;
However, the result was same.
I think this is because the query
planner can optimizer the 2nd query based on
table statistics of E and D.
E being the larger number of
records and has higher filtering rate so it continues to
be driving table.
Now, I tried another test case to
confirm my assumption.
The proportion of another test
case is different from above one.
The proportion of each table,
filtered table and joined table is below:
(E means employees table and D
means departments table.)
#rows in E #rows
in D #filtered rows in E
#filtered rows in D #rows in E and D
10000
490 5000
49
9800
The important point is the
difference in the filtering rate.
In this case, departments table
is higher filtering rate,
so taking departments table as
driving table will be able to cut the computational cost,
I think.
I tried same query:
EXPLAIN ANALYZE
SELECT D.Department_Name,
E.Last_Name, E.First_Name
FROM Employees E, Departments D
WHERE
E.Department_Id=D.Department_Id
AND E.Exempt_Flag='Y'
AND D.US_Based_Flag='Y'
And
EXPLAIN ANALYZE
SELECT D.Department_Name,
E.Last_Name, E.First_Name
FROM Employees E, Departments D
WHERE
E.Department_Id=D.Department_Id
AND E.Exempt_Flag='Y'
AND D.US_Based_Flag='Y'