为什么Crossjoin效率低于联盟

时间:2015-08-17 16:04:36

标签: sql oracle

我有以下快速运行的查询:

SELECT CUSTOMER FROM (
SELECT CUSTOMER FROM
EMAILS INNER JOIN CUSTOMERS 
ON EMAILS.email=CUSTOMERS.email1
UNION
SELECT CUSTOMER FROM
EMAILS INNER JOIN CUSTOMERS 
ON EMAILS.email=CUSTOMERS.email2)

但是交叉加入正在取决于:

SELECT DISTINCT CUSTOMER FROM
EMAILS INNER JOIN CUSTOMERS 
ON EMAILS.email=CUSTOMERS.email1
OR EMAILS.email=CUSTOMERS.email2
-- alternative: EMAILS.email in (CUSTOMERS.email1,CUSTOMERS.email2)

1 个答案:

答案 0 :(得分:0)

关于ORUnion的讨论已经有很多,你可以在这里查看

由于您要将一列与两个不同的列进行比较,您也可以尝试使用

SELECT DISTINCT CUSTOMER 
FROM EMAILS 
INNER JOIN CUSTOMERS 
ON EMAILS.email in (CUSTOMERS.email1,CUSTOMERS.email2)

此外,您在第一次查询时不需要DISTINCT Union会删除重复项