Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this alteration, as I dont have those tables, you might need to patch it up. After verifying, check the execution plans and look for table scans. Then add indexes. If the number indexes are large, consider included columns. I personally would modify this to a view and not stored proc but thats my preference. </p> <p>Someone once told me that counting * is actually slightly better but that was in SQL Server 2000 days and not sure if still relevant.</p> <p>If possibly, modify left joins to inner joins where you can. IE. I assume that users arent hard deleted so would modify user id joins to inner and filter disabled accounts where neccesary.</p> <pre><code> SELECT TOP 50 Flows_Users.UserName, Flows_Users.UserID, Flows_Users.ImageName, Flows_Users.DisplayName, Flows_UserBios.bio, a.FlowsCount, b.BeatsCount, c.FollowersCount, d.FollowingCount, ISNULL(SUM(Flows_Flows_Likes_Dislikes.[Like]), 0) AS Likes, ISNULL(SUM(Flows_Flows_Likes_Dislikes.Dislike), 0) AS DisLikes FROM Flows_Users LEFT JOIN ( SELECT Count(*) FlowCount FROM Flows_Flows WHERE UserID = Flows_Users.UserID AND Flows_Flows.Active = '1') a LEFT JOIN (SELECT Count(*) as FROM Flows_Beats WHERE UserName_ID = Flows_Users.UserID AND Flows_Beats.Active = '1') b LEFT JOIN (SELECT Count(*) as FollowersCount FROM Flows_Follow WHERE FOLLOWING = Flows_Users.UserID) c LEFT JOIN ( SELECT Count(*) as FollowingCount FROM Flows_Follow WHERE FOLLOWER = Flows_Users.UserID) d LEFT JOIN Flows_Flows ON Flows_Users.UserID = Flows_Flows.UserID LEFT JOIN Flows_UserBios ON Flows_Users.UserID = Flows_UserBios.userid LEFT JOIN Flows_Flows_Likes_Dislikes ON Flows_Flows.FlowID = Flows_Flows_Likes_Dislikes.FlowID WHERE Flows_Users.UserID = Flows_Users.UserID GROUP BY Flows_Users.UserID, Flows_Users.UserName, Flows_Users.ImagePath, Flows_Users.ImageName, Flows_Users.DisplayName, Flows_UserBios.bio ORDER BY [Likes] DESC, [Dislikes] ASC, FlowsCount DESC </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload