Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Performance - union
Message
 
General information
Forum:
Microsoft SQL Server
Category:
Other
Environment versions
SQL Server:
SQL Server 2008
Application:
Desktop
Miscellaneous
Thread ID:
01534182
Message ID:
01534211
Views:
40
>>>>Hi
>>>>I have a view (MyView) which is
>>>>
>>>>
SELECT * FROM DB1.dbo.tableA
>>>>UNION 
>>>>SELECT * FROM DB2.dbo.TableB
>>>>
>>>>
>>>>I have SQL statement
>>>>
>>>>
>>>>SELECT myView.* FROM MyView
>>>>INNER JOIN DB2.dbo.TableZ ON myView.Key = TableZ.Key
>>>>
>>>>
>>>>It is working fine, returned result in 2 secs
>>>>
>>>>However, if I amend it as below, it will take more than a minute to execute.
>>>>
>>>>
>>>>SELECT myView.*, TableZ.FieldA FROM MyView
>>>>INNER JOIN DB2.dbo.TableZ ON myView.Key = TableZ.Key
>>>>
>>>>
>>>>After some trial and error, found that, if I remark the edit my view without UNION (select only from TableB), the performance is 2 secs as well. Why?
>>>>
>>>>Beside, I tried to add an index which contains Key + FieldA, but not helping at all.
>>>>
>>>>Please advice, why remove the UNION can be helpful? How to solve this matter with "right" way?
>>>>
>>>>Thank you
>>>
>>>If it's performance you're after you should definitely look at EXISTS.
>>
>>
>>They are not the same, Mike. JOIN and EXISTS are two different animals
>
>Two different means to the same end. My point stands.

Moreover, if we need to return a field from TableZ as in this case, you can not do it with EXISTS. As Gregory said, in this particular case they are different and can not be used interchangeably.
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform