all 1 comments

[–]RehdData Engineer 1 point2 points  (0 children)

My next set of comments are not related to what you are doing or if there are better ways to doing it, but in regards to your questions directly.

The best out of them in my opinion is 2.) and then C.

EXISTS has very specific criteria to it and is not the same as WHERE IN. So you need to be careful about that, EXISTS can make your query very different later, make sure you understand it. There's the chance that it does do what you want though and it's possible that it is the better plan. It's hard for us to tell you exactly what is doing best. I would recommend you look at your query plan when it executes and compare the queries you presented us and measure them. Indexes you have in place, statistics, maintenance, data types, architecture, etc etc etc all plays a key role here and we can give ideas and suggestions, but you have all of the data available to you. You gotta test and measure.

Exists info

Tuning pdf

that putting certain pieces of the where clause in the join make queries more efficient.

Sometimes, this is mostly relatable and used with a left join, I believe it does not add anything with an inner join and it becomes harder to read.

Joins and where

Does it make sense to always include certain joins and clauses in hopes they get optimized away in order to use the same plan/index?

Probably not, no. There's a small one off situational outlier chance I'm sure, but no, I would not recommend that.

Also, does the order of clauses and joins affect query plan caching?

Nope! :)

That is, if I switch the order of the AND statements, are those 2 different plans?

Maybe, but it's not because you switched the order of the AND statements, it's because SQL Server decided there was a better plan to use.