I assume this is a fairly common SP that is present on social networks and community sites. I have this joint venture, which returns all the friends of the user in the order of their "friends" to those who are online now, and then in alphabetical order. It will take quite a while to download, and I want to speed it up.
I remember reading SO somewhere that breaking multiple compounds into smaller result sets can speed it up. I have not tried this yet, but I am curious to see what other recommendations may have on this procedure.
DECLARE @userID INT
DECLARE @lastActivityMinutes INT
SET @lastActivitytMinutes = '15'
SELECT
Active = CASE WHEN DATEDIFF("n", b.LastActivityDate ,GETDATE()) < @lastActivityMinutes THEN 1 ELSE 0 END,
a.DisplayName, a.ImageFile, a.UserId, b.LastActivityDate
FROM
Profile AS a
INNER JOIN aspnet_Users as b on b.userId = a.UserId
LEFT JOIN Friend AS x ON x.UserID = a.UserID
LEFT JOIN Friend AS z ON z.FriendID = a.UserID
WHERE ((x.FriendId = @userID AND x.status = 1)
OR (z.UserID = @userID AND z.Status = 1))
GROUP BY a.userID, a.DisplayName, a.ImageFile, a.UserId, b.LastActivityDate
ORDER BY Active DESC, DisplayName ASC
, , , , MERGE JOIN (Right Outer Join), 29%. Parallelism 9%, 6%, 5% 9% 29%.
, JOINED aspnet CTE, LEFT JOINS .