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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:47:02+00:00 2026-06-08T07:47:02+00:00

I am currently working on speeding up a website, that is returning 300,000+ rows

  • 0

I am currently working on speeding up a website, that is returning 300,000+ rows from a query. While I don’t think this is too much of a load on the DB server, this query is happening in a while loop depending on the number of ‘galleries’ a user has.

For example Joe has 10 galleries in his account. Each of those galleries has x number of images, which have x number of comments on those images. So the query that is currently being run…

SELECT count(*) as total 
FROM galleryimage a 
INNER JOIN imagecomments b ON a.id=b.imgId 
WHERE a.galleryId='".$row['id']."' 
AND b.note <> ''

…is looking through all the galleryimage table 334,000 rows and the imagecomments table 76,000 rows and returning the result on each gallery. The query run on a single gallery returns a result in about 578ms, but with many galleries, say 30-40 you could be looking at a page load time of 17+ secs. Any suggestions on how to deal with this issue?

I cannot change the DB architecture….

Query for gallery id

SELECT a.id, 
       a.created, 
       a.name, 
       b.clientName, 
       a.isFeatured, 
       a.views, 
       a.clientId 
FROM gallery a 
INNER JOIN client b 
ON a.clientId = b.id 
WHERE a.isTemp = 0 
AND a.clientRef = '{$clientRef}' 
AND a.finish='1'  
AND a.isArchive='0' 
ORDER BY created 
DESC
  • 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-08T07:47:04+00:00Added an answer on June 8, 2026 at 7:47 am

    You can consolidate the queries and eliminate the need for looping:

    SELECT 
        a.id, 
        a.created, 
        a.name, 
        b.clientName, 
        a.isFeatured, 
        a.views, 
        a.clientId,
        COALESCE(c.img_cnt, 0) AS gallery_image_count,
        COALESCE(c.comment_cnt, 0) AS gallery_comment_count
    FROM 
        gallery a 
    INNER JOIN 
        client b ON a.clientId = b.id 
    LEFT JOIN
    (
        SELECT aa.galleryId, 
               COUNT(DISTINCT aa.id) AS img_cnt, 
               COUNT(1) AS comment_cnt
        FROM galleryimage aa
        INNER JOIN imagecomments bb ON aa.id = bb.imgId
        WHERE bb.note <> ''
        GROUP BY aa.galleryId
    ) c ON a.id = c.galleryId
    WHERE 
        a.isTemp = 0 AND
        a.clientRef = '{$clientRef}' AND
        a.finish = 1 AND
        a.isArchive = 0 
    ORDER BY 
        a.created DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im currently working on a c# project that uses another .net library. This library
Were currently working on a script that reads the current users information from out
Currently working with converting SQLException error messages into messages that are more useful for
Currently working with NSURLConnection. Found a great website showing important delegate methods coming with
Im currently working on a game that uses multi touch to apply zoom to
Am currently working on an application that requires users to submit posts and comments
Currently working through a Teach Yourself WPF tutorial. Usually I can mentally convert from
Am currently working on a site that uses a lot of cfwindow objects and
Am currently working on a web application which receives the encoded text from the
im currently working in a simple program that implements plugins whith dll libraries (using

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.