Since you use INNER JOINs, the discussion of WHERE or JOIN depends only on your taste and style. Personally, I like to maintain a relationship between two tables (for example, a foreign key constraint) in the ON clause and actual filters against data in the WHERE clause.
SQL Server will analyze the query in the same token tree and, therefore, will build identical query execution plans.
If you used [LEFT / RIGHT] OUTER JOINS instead, this makes the world a difference, as performance is not only different, but also very likely.
To answer other questions:
When is the best time to filter my data?
- In the where clause of SQL.
- Create a temporary table with specific data and only then attach it.
- Add the predicate to the first internal ON clause.
- Another idea.
In a WHERE or ON clause, both are treated as the same thing. For 3, the "first inner join" is irrelevant. In an INNER JOIN scenario with multiple tables, it really doesn't matter what comes first (in the query), as the query optimizer will shuffle the order as it sees fit.
Using the temp table is completely unnecessary and will not help, because you still need to extract the corresponding part - which is what the JOIN will do. Moreover, if you have a good index in the JOIN conditions / WHERE parameter, the index will be used to view only the relevant data without looking at the rest of the tables.
RichardTheKiwi
source share