Stealth DBA, the following would seem to support what you are saying:
When and why are database joins expensive? - Stack Overflow
But in practice, on our system:
Code:
select a.f1,b.f2
from (select f1 from a where cond1) a
inner join (select f2 from b where cond2) b
on a.pk = b.fk
is much faster than:
Code:
select a.f1, b.f2
from a
inner join b on a.pk = b.fk
where a.f1 = cond1 and b.f2 = cond2
I imagine table structure and system setup greatly influence this, and it is not supposed to be true, but when I look at actual query execution times it seems that I can manually optimize better than whatever the standard optimization plan that occurs in our system. Perhaps the system is not setup properly. We all use what we have and it takes trying both to know. You should be right.
In truth I rarely bother with the efforts of manual tuning because the layered selects is ugly and unmanageable when changes are needed. Do they run faster? On my system yes.
Quote:
Eliminate any table space scans (on tables with significant number of rows).
Eliminate or reduce any sorting.
Encourage index usage
(This could include adding one or more indexes, adding the first key index value to the query that is only using the second value of a composite index, determine if index keys can be in a different order, etc.)
|
This doesn't conflict with what I have said. And I would not advocate denormalizing the tables into one table. I would rather write clear code, take the performance hit and run the long querys on a schedule after typical business hours, but the truth is that an intelligent programmer can sometimes make better optimization decisions based on his data and system than a standard predetermined optimization routine performed by the system.
The following site suggests precisely what I am mentioning reducing the set size on which work is done:
http://www.sql-server-performance.co..._joins_p1.aspx
http://www.devx.com/dbzone/Article/26995/1954 states that "[f]or complex queries involving joins between eight different tables, the optimizer could spend as much as 30 minutes to find an effective execution path before the server actually executes the query." This may mean that my tuned query is not really any quicker, but it executes 30 minutes faster because the effective execution path is immediately clear.