In MS SQL Server, the two queries:
-----------------
SELECT au_fname, au_ord
FROM authors, titleauthor
WHERE authors.au_id=titleauthor.au_id
------------------
and
---------------------
SELECT au_fname, au_ord
FROM authors INNER JOIN titleauthor
ON authors.au_id=titleauthor.au_id
-----------------------
produce, both, the SAME query plan. Remember that SQL is not about HOW TO
get the result, but about to DESCRIBE the result you want. The HOW TO, the
imperative part, is ideally left to the database.
So, yes, if the query plan would use a cross join to solve the problem, that
would be inefficient, but as writing the description of what I want, there
is NO PROBLEM using a CROSS JOIN to do it, FOLLOWED by a WHERE clause.
When I said that SQL joins are probably easier to understand by STARTING
with cross joins, I meant as a mean to understand them, not HOW the database
should (would) do it. In fact, I am far from the first using that approach,
indeed, I 'borrow' the approach from Joe Celko. (The outer joins are then
explained by the re-injection of each row, from the preserved side, which
would have disappear.)
LINQ can thus allow easy non-equi-join if you start by describing a cross
join, and the use the WHERE clause to describe the condition that you would
normally put in the ON clause of the INNER join. The problem is for OUTER
NON EQUI join, only. In some cases, you can still make use of sub-queries.
Vanderghast, Access MVP
"Frans Bouma [C# MVP]" <pe******************@xs4all.nlwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Michel Walsh wrote:
>If used alone, yes, but with a WHERE clause, MS SQL Server optimizer
should recognize an (implicit) inner join and thus, limit the effective
amount of data transferred through the wire.
That's not what makes it inefficient. The cross-join execution path is
often less ideal as the cross-join action by itself is first handled, and
THEN the where clause is handled to filter out rows you don't want. As a
cross-join can spawn millions of tuples, which are often not needed, the
where clause will run slower than an ON clause in an inner-join set
operation.
It's not the data that's sent over the wire, it's the query execution
itself which makes it less ideal. That's also why I find it a big design
flaw in linq that the only operator in the linq's join clause is an
'equals' operator, which makes the production of left/right joins awkward,
yet these operations are often required. The alternative is a nested from
but that leads to lesser ideal queries than a left-join + accompanying on
clause.
FB
>>