Can it make any difference to query optimisation to have WHERE clauses in a different order for SQL Server?
For example, would the query plan for this:
select * from table where col1 = @var1 and col2 = @var2
be different from this?:
select * from table where col2 = @var2 and col1 = @var1
Of course this is a contrived example, and I have tried more complex ones out. The query plan was the same for both, but I’ve always wondered whether it was worth ordering WHERE clauses such that the most specific clauses come first, in case the optimiser somehow “prunes” results and could end up being faster.
This is really just a thought experiment, I’m not looking to solve a specific performance problem.
What about other RDBMS’s, too?
Every modern RDBMS has a query optimizer which is responsible, among other things, of reordering the conditions. Some optimizers use pretty sophisticated statistics to do this, and they often beat human intuition about what will be a good ordering and what will not. So my guess would be: If you can definitely say “This ordering is better than the other one”, so can the optimizer, and it will figure it out by itself.
Conclusion: Don’t worry about such things. It is rarely worth the time.