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

  • Home
  • SEARCH
  • 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 8207715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:05:11+00:00 2026-06-07T09:05:11+00:00

This is by far the slowest query in my web application. SELECT prof.user_id AS

  • 0

This is by far the slowest query in my web application.

SELECT prof.user_id                      AS userId,
       prof.first_name                   AS first,
       prof.last_name                    AS last,
       prof.birthdate,
       prof.class_string                 AS classes,
       prof.city,
       prof.country,
       prof.state,
       prof.images,
       prof.videos,
       u.username,
       u.avatar,
       (SELECT Count(*)
        FROM   company_member_sponsorship
        WHERE  member_id = prof.user_id
               AND status = 'sponsored') AS sponsor_count,
       (SELECT Count(*)
        FROM   member_schedules
        WHERE  user_id = prof.user_id)   AS sched_count
FROM   member_profiles prof
       LEFT JOIN users u
              ON u.id = prof.user_id
ORDER  BY ( prof.images + prof.videos * 5 + (
            CASE
              WHEN prof.expire_date > :time THEN 50
              ELSE 0
            end ) + sponsor_count * 20 + sched_count * 4
          ) DESC,
          prof.last_name ASC
LIMIT  :start, :records  

Everything else on the site takes less than a second to load even with lots of queries happening on all levels. This one takes about 3-4 seconds.

It’s obviously the table scans that are causing the slowdown. I can understand why; the first table has 50,000+ rows, the second 160,000+ rows.

Is there any way I can optimize this query to make it go faster?

If worse comes to worst I can always go through my code and maintain a tally for sponsorships and events in the profile table like I do for images and videos though I’d like to avoid it.

EDIT: I added the results of an EXPLAIN on the query.

id  select_type         table                       type    possible_keys   key         key_len ref                         rows    Extra
1   PRIMARY             prof                        ALL     NULL            NULL        NULL    NULL                        44377   Using temporary; Using filesort
1   PRIMARY             u                           eq_ref  PRIMARY         PRIMARY     3       mxsponsor.prof.user_id      1   
3   DEPENDENT SUBQUERY  member_schedules            ref     user_id         user_id     3       mxsponsor.prof.user_id      6       Using index
2   DEPENDENT SUBQUERY  company_member_sponsorship  ref     member_id       member_id   3       mxsponsor.prof.user_id      2       Using where; Using index

EDIT2:

I ended up dealing with the problem by maintaining a count in the member profile. Wherever sponsorships/events are added/deleted I just invoke a function that scans the sponsorship/events table and updates the count for that member. There might still be a way to optimize a query like this, but we’re publishing this site rather soon so I’m going with the quick and dirty solution for now.

  • 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-07T09:05:13+00:00Added an answer on June 7, 2026 at 9:05 am

    Not guaranteed to work, but try using join and group by rather than inner selects:

    SELECT prof.user_id      AS userId,
           prof.first_name   AS first,
           prof.last_name    AS last,
           prof.birthdate,
           prof.class_string AS classes,
           prof.city,
           prof.country,
           prof.state,
           prof.images,
           prof.videos,
           u.username,
           u.avatar,
           Count(cms.id)     AS sponsor_count,
           Count(ms.id)      AS sched_count
    FROM   member_profiles prof
           LEFT JOIN users u
                  ON u.id = prof.user_id
           LEFT JOIN company_member_sponsorship cms
                  ON cms.member_id = prof.user_id
                     AND cms.status = 'sponsored'
           LEFT JOIN member_schedules ms
                  ON ms.user_id = prof.user_id
    GROUP  BY u.id
    ORDER  BY ( prof.images + prof.videos * 5 + (
                CASE
                  WHEN prof.expire_date > :time THEN 50
                  ELSE 0
                end ) + sponsor_count * 20 + sched_count * 4
              ) DESC,
              prof.last_name ASC
    LIMIT  :start, :records  
    

    If that’s not any better, a explain of that query would help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a beginner at setting up spring web-applications. I got this far but now
im learning wpf for the first time, i have made this far private void
I got this far... here is my code: - (void)applicationDidFinishLaunching:(UIApplication *)application { // create
I got this far, but it seems that buffer won't take arrays, because first
So I can get this far string selectedOption = ViewBag.SelectedOption; <select id=SelectedYear name=SelectedYear> <option
I got this far: :~ curl -u username:password -d status=new_status http://twitter.com/statuses/update.xml Now, how can
Been at this far too long; trying to populate a field in a record
After cobbling together a few questions I've managed to get this far to showing
I have this so far but the Column 'name' is ambiguous, so I want
I tried this so far: <?php $error = ; $conn = pg_connect(host=localhost dbname=brittains_db user=brittains

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.