在子查询上加入字段vs join

时间:2014-03-11 13:48:15

标签: sql sql-server join subquery

早上好。

我想知道它更优化了什么:

SELECT PROD.ID
FROM PRODUCT PROD
INNER JOIN SUBPRODUCT SUB ON PROD.SUBTYPE = SUB.SUBTYPE AND SUB.SOMEFIELD = 1
WHERE PROD.FOO = 2

OR

SELECT PROD.ID
FROM PRODUCT PROD
INNER JOIN 
( 
 SELECT SUB.SUBTYPE FROM SUBPRODUCT SUB WHERE SUB.SOMEFIELD = 1
) PROD.SUBTYPE = SUB.SUBTYPE
WHERE PROD.FOO = 2

非常感谢!!

1 个答案:

答案 0 :(得分:1)

只是通过查看查询,我无法分辨哪一个是更优化的形式。但我会选择下面的一个。

SELECT PROD.ID
FROM PRODUCT PROD
INNER JOIN 
( 
 SELECT SUB.SUBTYPE FROM SUBPRODUCT SUB WHERE SUB.SOMEFIELD = 1
) T ON PROD.SUBTYPE = T.SUBTYPE
WHERE PROD.FOO = 2