Which clause performs first in a SELECT
statement?
I have a doubt in select
query on this basis.
consider the below example
SELECT *
FROM #temp A
INNER JOIN #temp B ON A.id = B.id
INNER JOIN #temp C ON B.id = C.id
WHERE A.Name = 'Acb' AND B.Name = C.Name
Whether, First it checks
WHERE
clause and then performsINNER JOIN
First
JOIN
and then checks condition?
If it first performs JOIN
and then WHERE
condition; how can it perform more where conditions for different JOIN
s?
The conceptual order of query processing is:
But this is just a conceptual order. In fact the engine may decide to rearrange clauses. Here is proof. Let's make 2 tables with 1000000 rows each:
Now run 2 queries:
Notice that the first query will filter most rows out in the
join
condition, but the second query filters in thewhere
condition. Look at the produced plans:This means that in the first query optimized, the engine decided first to evaluate the
join
condition to filter out rows. In the second query, it evaluated thewhere
clause first.