Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 UNION ALL slow if one select returns no data

Author  Topic 

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-04-19 : 08:32:38
Dear all,
SELECT ...
UNION ALL
SELECT ...
UNION ALL
SELECT ...

My colleague has this issue.
If all 3 selects do return at leat one record then the select needs 4 secs.
If one select doesn't return data then the select needs 1 minute.

Has someone else of you ever had this problem?
Is there a known reason?
Is there a trick to make it better?

Thanks
Fred



No, you're never too old to Yak'n'Roll if you're too young to die.

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2010-04-19 : 08:39:14
What time does it take when you individually run those 3 statements? Does it take time when no data is returned for that particular statement?

Harsh Athalye
http://www.letsgeek.net/
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-04-19 : 08:53:31
With UNION ALL if one select doesn't return data then the select needs 1 minute.
Same statement and the UNION ALL lines commented out --> 4 secs.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-04-19 : 09:18:55
Could it be that when the "problem" UNION returns rows the query plan uses an Index, but when it doesn't then the query plan does a Table Scan instead??
Go to Top of Page
   

- Advertisement -