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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:08:23+00:00 2026-05-29T05:08:23+00:00

Trying to optimize this query, with a corollated subquery SELECT g.*, g.instaLikes + (

  • 0

Trying to optimize this query, with a corollated subquery

SELECT g.*, g.instaLikes + (  SELECT COUNT( * )  FROM gallery_likes AS l WHERE l.gallery_id = g.id ) AS likes 
FROM gallery AS g 
WHERE (  STATUS =  'approved' ) 
ORDER BY g.moderated_at DESC , g.id ASC

The SELECT without the subquery is very fast, and when I try a LEFT JOIN its even slower.

SELECT g.*, (g.instaLikes + COUNT( l.id ) ) AS likes 
FROM gallery AS g
LEFT JOIN gallery_likes AS l ON ( g.id = l.gallery_id ) 
WHERE ( STATUS =  'approved)
GROUP BY g.id
ORDER BY g.moderated_at DESC , g.id ASC 

The option that seems to be fastest is to do the queries separately and then add the fields together in a PHP loop, since I can run the subquery once, rather than n times in gallery.

Currently there are 2000 rows in gallery and 15000 rows in gallery_likes. I figured that I wouldn’t need to optimize yet, because those numbers seem low, but the first query takes 20-30 seconds and the LEFT JOIN takes 100+ seconds!

  • 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-05-29T05:08:24+00:00Added an answer on May 29, 2026 at 5:08 am

    Doing separate queries and joining the data in PHP isn’t a bad idea. But if you wanted to do essentially the same thing in SQL, you can. You can treat a query just like a table (i.e. join on it), they are called derived tables. This is often a quicker and easier way to optimize these types of queries.

    SELECT g.*, g.instaLikes + l.c
    FROM gallery AS g
    LEFT JOIN (
        SELECT gallery_id, COUNT( * ) c  FROM gallery_likes GROUP BY gallery_id
    ) AS l ON g.id=l.gallery_id
    WHERE (  STATUS =  'approved' )
    ORDER BY g.moderated_at DESC , g.id ASC
    

    That is grouping all of the records from gallery_likes, which is probably very inefficient. So you should add some sort of filter. Event if the sub-select (derived table) joins on the gallery and use the same filter as the outer query. This almost like your second query, but it’s grouping the data before joining. Rather than joining and then grouping.

    Often times this is a great solution when you need to join on summary data (i.e. group by), but sometimes it can be inefficient. You can usually tell by the EXPLAIN.

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

Sidebar

Related Questions

I'm trying to optimize this slow query (>2s) SELECT COUNT(*) FROM crmentity c, mdcalls_trans_activity_update
I am trying to optimize a query that does something like this: SELECT ...
Hey guys, trying to optimize this query to solve a duplicate user issue: SELECT
I`m having trouble trying to optimize this query with OVER (PARTITION BY ...) because
I am trying to optimize this query. It is not slow for now but
I have a query which is crossing two tables select count(*) from ingenium.empevt, ingenium.evt
I'm trying to optimize this mysql query using EXPLAIN. Can somebody please help me
I am trying to optimize a bigger query and ran into this wall when
I'm trying to optimize this query because the DB it runs against is huge
I'm trying to optimize this query, and as simple as it sounds, I'm having

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.