Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Join in Where Clause

Author  Topic 

james_wells
Yak Posting Veteran

55 Posts

Posted - 2010-03-24 : 13:02:54
When no "join on" statement is issued but the join is issued in the where clause does sql

1. build up the record complex based on the join if one exists and
then filter by the where clause

2. if no join is specified then finds the best index if one
exists based on the where clause and then results to a
hash join

or generates a hash join regardless of what is in the where
cluase if no join is specified.

I am investigating some sql code which is being generated via our application in which no joins are specified but explicitly joins
the table within the where clause.

I was under the impression that if no join statement was specified
then SQL builds up a record complex selecting all records in all tables specified in the FROM clause before filering on the where clause and therefore was more effecient to place the joins in the
FROM cluase because the record complex built by the select clause was smaller becuase og=f the joins before filtering on the where clause ?

I am just interested in way that SQL builds and filters the record
complexes before the output is generated rather then a solution to
a problem.







Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-03-24 : 13:28:43
The optimiser is probably smart enough to derive the same plan anyway.

For instance I get the same query plan and INdex utilisation for the following (very basic) query

DECLARE @foo TABLE (
[ID] INT
, [val] VARCHAR(45)

PRIMARY KEY ([ID])
)

DECLARE @bar TABLE (
[ID] INT
, [fooID] INT
, [val] VARCHAR(25)

PRIMARY KEY ([ID])
)

INSERT @foo SELECT 1, 'a' UNION SELECT 2, 'b'
INSERT @bar SELECT 1, 1, 'aa' UNION SELECT 2, 1, 'ab'

SELECT
f.[val]
, b.[val]
FROM
@foo f
JOIN @bar b ON b.[fooID] = f.[ID]

SELECT
f.[val]
, b.[val]
FROM
@foo f
, @bar b
WHERE
b.[fooID] = f.[ID]


I think if the queries are far more complex then you can get filtered CROSS JOINS when you didn't want them but...

Anyway -- the JOINS are the standard and support for old school LEFT joins etc will be depreciated at some point.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -