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 2005 Forums
 Transact-SQL (2005)
 help optimzing query

Author  Topic 

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2007-07-16 : 09:53:42
Hi,

I have the following query below, and I have one concern with the way it was written. The

"WHERE UD.userID IN (SELECT userID FROM tblFriends WHERE friendID=1)"

part of the query appears 4 times. Would it be possible/more efficient to populate this into a temp table / variable and have it only execute once?

If so, any help is much appreciated as I have no idea how to approach this.


Many thanks once again! Much appreciated!
Mike123






SELECT TOP 100 *
FROM
(
SELECT Ud.UserID, UD.NameOnline, EventDate = F.dateAdded, TypeId = 1,
friendID as friend_counterID, null as photoID, UD2.nameOnline as friend_NameOnline
from tblFriends F
JOIN tblUserDetails UD ON F.userID = UD.UserID
JOIN tblUserDetails UD2 ON F.FriendID = UD2.UserID

WHERE UD.userID IN (SELECT userID FROM tblFriends WHERE friendID=1)

UNION ALL
SELECT Ud.UserID, UD.NameOnline, EventDate = P.photoDate , TypeId = 3, counterID, photoID, null
from tblExtraPhotos P
JOIN tblUserDetails UD ON P.userID = UD.UserID

WHERE UD.userID IN (SELECT userID FROM tblFriends WHERE friendID=1)
UNION ALL
SELECT userID, nameOnline, EventDate = lastUpdated, TypeId = 4, null, null, null
from tblUserDetails UD

WHERE UD.userID IN (SELECT userID FROM tblFriends WHERE friendID=1)

UNION ALL
SELECT userID, nameOnline, EventDate = date, TypeId = 5, null, null, null
from tblUserDetails UD

WHERE UD.userID IN (SELECT userID FROM tblFriends WHERE friendID=1)
) as data

ORDER BY eventDate DESC


Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-07-16 : 10:10:05
quote:
Originally posted by mike123

Hi,

I have the following query below, and I have one concern with the way it was written. The

"WHERE UD.userID IN (SELECT userID FROM tblFriends WHERE friendID=1)"

part of the query appears 4 times. Would it be possible/more efficient to populate this into a temp table / variable and have it only execute once?

...


Did it run faster when you tested it that way?



CODO ERGO SUM
Go to Top of Page

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2007-07-16 : 10:11:56

Hi Michael,

I'm trying to figure out how to write it and if its possible. I'm guessing it is, but I read in SQL2005 somebody say we shouldnt need temp tables anymore.

I'm not sure the best way to write this? Any help is much appreciated then I can test it and let everyone know! :)

Thanks again,
mike123
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2007-07-16 : 10:28:56
1. Exists subquerys are usually quicker than IN subquerys so try:
WHERE EXISTS (SELECT * FROM tblFriends F WHERE F.friendID = 1 AND F.userID = UD.userID)

2. The last two selects are essentially the same. Try replacing them with something like:

SELECT UD.userID
,UD.nameOnline
,EventDate =
CASE D.TypeId
WHEN 4 THEN UD.lastUpdated
ELSE UD.[date]
END
,D.TypeId
,NULL, NULL, NULL
FROM tblUserDetails UD
CROSS JOIN (
SELECT 4 UNION ALL
SELECT 5
) D (TypeId)
WHERE EXISTS (
SELECT *
FROM tblFriends F
WHERE F.friendID = 1
AND F.userID = UD.userID
)

Go to Top of Page
   

- Advertisement -