开发者

Performance difference if I UNION first, then put WHERE on unioned result set?

开发者 https://www.devze.com 2022-12-30 10:59 出处:网络
In Microsoft SQL, is there any difference in performance between this: SELECT columns FROM table1 WHERE cond

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
S开发者_运维知识库ELECT columns FROM table2
) WHERE cond

?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消