views:

33

answers:

1

In Microsoft SQL, is there any difference in performance between this:

SELECT columns FROM table1 WHERE cond
UNION
SELECT columns FROM table2 WHERE cond

and this:

SELECT columns FROM
(
SELECT columns FROM table1
UNION
SELECT columns FROM table2
) WHERE cond

?

+4  A: 

The former is preferrable because if index(es) exist on any column referenced in the WHERE clause (barring being wrapped in function calls), the index(es) can be used.

The latter is a derived table, so index use is out of the question.

OMG Ponies
In addition, use "UNION ALL" if there is no chance of duplicate rows from between the two tables.
BradC