Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9173171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:31:37+00:00 2026-06-17T16:31:37+00:00

All, I have a users table with around 250000 records. I have the following

  • 0

All,

I have a users table with around 250000 records. I have the following query (mind you, I am a novice in SQL) but it is taking a very LONG time to execute. I checked out the execution plan and %60 of the execution time is being taken up by 2 sorts.

SELECT TOP 50
    Flows_Users.UserName,
    Flows_Users.UserID,
    Flows_Users.ImageName,
    Flows_Users.DisplayName,
    Flows_UserBios.bio,
    FlowsCount = (
        SELECT Count(1)
        FROM Flows_Flows
        WHERE UserID = Flows_Users.UserID
            AND Flows_Flows.Active = '1'
    ),
    BeatsCount = (
        SELECT Count(1)
        FROM Flows_Beats
        WHERE UserName_ID = Flows_Users.UserID
            AND Flows_Beats.Active = '1'
    ),
    FollowersCount = (
        SELECT Count(1)
        FROM Flows_Follow
        WHERE FOLLOWING = Flows_Users.UserID
    ),
    FollowingCount = (
        SELECT Count(1)
        FROM Flows_Follow
        WHERE FOLLOWER = Flows_Users.UserID
    ),
    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 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

I know this is a mess but it got the job done when tables where only in the tens of thousands. Is there any way to make this quicker? Right now it takes up to 5-10 minutes to execute. This is in a stored procedure also and I feel i have the columns that need to be indexed, indexed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:31:39+00:00Added an answer on June 17, 2026 at 4:31 pm

    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.

    Someone once told me that counting * is actually slightly better but that was in SQL Server 2000 days and not sure if still relevant.

    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.

        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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have users table. There are three other tables: developers, managers, testers. All of
I have a users table and a blocked_users table. users has all my authentication
I have a table that contains all the data about users . Users can
I have a table and I need to count all unique users on it.
I have a table of around 100 Users and I also have an array
In a VB.net application I have a SQL interface, I have a users table
I have the following SQL Query: SELECT upd.*, usr.username AS `username`, usr.profile_picture AS `profile_picture`
I have a usermessages table in which all messages sent between two users on
I have a form where users can search all the entries in a table
I have a table in my SQL in the following structure called actions: +----+--------+------+---------+

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.