Question

I would like to know if there are any rules about the order of INNER JOIN's in T-SQL 2000.

I had 62x the performance on my query when my INNER JOIN that act like a filter is placed at first instead of the end.

In reference of http://sql-4-life.blogspot.com/2009/03/order-of-inner-joins.html

Was it helpful?

Solution

The optimal sequence (the query plan) is determined by the query executor, based on the SQL code (operators you use), data statistics (statistical distribution of values in columns, data volume, etc.) and database structure (availability of indexes, datatypes, etc.). So, there are rules, but first not all of them are under your total control and second the result is detemined by a combination of many factors.

To evaluate performance, you should always take a look at the estimated query plan and compare the changes to the plan caused by the changes in your SQL code, to undertand why a query run faster or slower than another.

OTHER TIPS

The order you write the JOINs is not relevant. Most, if not all, optimizers (and certainly SQL-Server's optimizer) know that JOIN is associative and commutative.

Finding the best execution plan (which means join order, join algorithm for each join, choice of indices to use, etc), is a very hard problem which is exactly what query optimizers were built to solve and they use various and complex heuristics and techniques. But the order we write the joins in our queries is not taken into consideration, unless (maybe) as a starting point.

Choosing between the (possibly millions or trillions) ways of ordering the joins is far from trivial. It's like being thrown in a planet with a terrain full of mountain peaks and bottomless pits with the aim to find the lowest point. The starting position has negligible effect on this quest except maybe in very complex queries where the optimizer obviously cannot spend eternity considering all possible plans.

If I am not wrong, Simulated annealing is one of the heuristics/techniques used by The SQL Server Query Optimizer.
(I was wrong)

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top