-3

I have the following query that works fast:

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)

But cross join is taking forewer:

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)
Andrew
  • 7,619
  • 13
  • 63
  • 117

1 Answers1

0

Already so many discussion are present about OR vs Union you can check here

Since you are comparing one column with two different column you can also try this

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

Also you don't need DISTINCT in first query Union will remove the duplicates

Community
  • 1
  • 1
Pரதீப்
  • 91,748
  • 19
  • 131
  • 172