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 9004833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:59:21+00:00 2026-06-16T00:59:21+00:00

So I am in the process of optimising a SQL query. I have cleaned

  • 0

So I am in the process of optimising a SQL query. I have cleaned it up considerably but still have some work to do. See query below:

Competitions tbl:
ID, fb_share_points, tw_share_points

comp_mapper tbl:
id, referrer_user_id, user_subscription_id, has_fb_shared, has_tw_shared, competition_id

SELECT 
    IF (cm.has_fb_shared = 1, IFNULL(c.fb_share_points,0), 0) 
    + IF (cm.has_tw_shared = 1, IFNULL(c.tw_share_points,0), 0) 
    + (SELECT count(*) FROM comp_mapper as cm2
        WHERE cm2.comp_id = cm.comp_id 
        AND cm2.referrer_user_id = us.user_id)
    as shares
FROM competitions AS c
JOIN comp_mapper as cm ON cm.competition_id = c.id
JOIN user_subscription as us on cm.user_subscription_id = us.id
WHERE c.id = :id
ORDER BY shares DESC
LIMIT :limit

When a user is referred, the ID of the referring user is placed into the referrer_user_id column of the competition entrant. The subquery calculates how many competition entrants have referred other users. I am currently trying to find another way of writing this without the subquery because the query itself takes quite a while to run. If anyone has any suggestions or recommendations then please do share!

  • 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-16T00:59:23+00:00Added an answer on June 16, 2026 at 12:59 am

    This should return an equivalent result. The difference here is that rather than a correlated subquery, we’ve got an inline view (MySQL calls it a derived table) which is aliased as rf.

    SELECT IF(cm.has_fb_shared = 1, IFNULL(c.fb_share_points,0), 0) 
         + IF(cm.has_tw_shared = 1, IFNULL(c.tw_share_points,0), 0) 
         + IFNULL(rf.ref_count,0) AS shares
      FROM competitions c
      JOIN comp_mapper cm 
        ON cm.competition_id = c.id
      JOIN user_subscription us
        ON us.id = cm.user_subscription_id
      LEFT
      JOIN (  
             SELECT cm2.referrer_user_id 
                  , COUNT(1) AS ref_count
               FROM comp_mapper cm2
              WHERE cm2.comp_id = :id
              GROUP BY cm2.referrer_user_id
           ) rf
        ON rf.referrer_user_id = us.user_id
     WHERE c.id = :id
     ORDER BY shares DESC
     LIMIT :limit
    

    It strikes me a bit odd that the query is returning a set of rows with only a single column (shares), and no other columns. If the intent is to return a single value, there would need to be an aggregate function (i.e. SUM() ) in the select list.

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

Sidebar

Related Questions

I have a query which takes a very long time to run but produces
I was given a SQL query, saying that I have to optimize this query.
Current Process: I have a tar.gz file. (Actually, I have about 2000 of them,
Process.Start(IExplore.exe); Does this always work, on every machine ? If not, how to do
I'm in the process of porting some of my Linux programs to Windows. I
I have some complex queries with LINQ which behind the scenes do around 7-9
After some work, I managed to get HipHop up and running on my server.
Process Explorer has an option which I have used recently called Replace Task Manager..
I have a SQL Server 2005 database which has about 30-40 connections to the
I have been optimising memory performance on a Windows Mobile application and have encountered

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.