I am trying to get the difference between two almost identical tables in postgresql. The current request that I run is:
SELECT * FROM tableA EXCEPT SELECT * FROM tableB;
and
SELECT * FROM tableB EXCEPT SELECT * FROM tableA;
Each of the above requests takes about 2 minutes (large table)
I wanted to combine these two queries in the hope of saving time, so I tried:
SELECT * FROM tableA EXCEPT SELECT * FROM tableB UNION SELECT * FROM tableB EXCEPT SELECT * FROM tableA;
And while it works, it takes 20 minutes! I would suggest that it takes no more than 4 minutes, the amount of time for each request is individual.
Is there any kind of extra UNION work that makes it take so long? Or can I speed it up (with or without UNION)?
UPDATE: Running a query with UNION ALL takes 15 minutes, almost 4 times longer than running each of them. Am I right in saying that UNION (everyone) is not going to speed it up at all?
sql diff postgresql union
lanrat
source share