Very slow performance with UNION and UNION ALL
523125Dec 30 2008 — edited Dec 31 2008I am returning three datasets as one with a UNION ALL between each of three SQL statements. The first one returns either 3 or 4 rows, the second one returns about 10 rows and the third one returns one row. The SQL statements are a little complex but they're all working on very small datasets ( < 1000 rows ) so when I run each of the three SQL statements by themselves, they run to completion and return data in less than 1 second.
However.
When I join the three statements together with a UNION ALL statement between them, the statement runs until I manually abort it. I've run it for up to two hours and it shows no signs of either 1) erroring or 2) running to completion. And, I've tried replacing the UNION ALL with UNION and I get the identical results - nothing returned, no error message.
I've checked the obvious -- same column names, same number of columns, etc. I think that if there were something obvious like that, I would get an error message as soon as I attempted to execute the statements.
I will be happy to post the code if you'd like, but I'm wondering if anyone has experienced these or similar symptoms when joining together SQL statements with UNIONs and if so what you did to get around it.
Thanks in advance,
Carl