Okay, my dilemma is this. I have an admin page that I use to get a summary of the day's submissions to one of my sites. This sumamry page pulls data from several tables, some of which store the actual media data, others store the website urls, ids and owners, and another table stores the day-to-day stats to calculate in/out ratios for each domain.
This admin page has a main query, which pulls the actual media data, which looks like this (simplified):
SELECT
content.con_catid,
content.con_posttime,
content.con_url,
content.con_id,
content.con_title,
categories.name,
users.username,
users.user_id,
FROM content
LEFT JOIN categories
ON (content.con_catid = categories.id)
LEFT JOIN users
ON (content.con_poster = users.user_id)
WHERE con_status = 0 ORDER BY con_posttime
Fairly straight forward. Then, this is where it gets messy. For each time it loops the mysql_fetch_array statement (to output each content item), I also run these 2 queries:
SELECT count(con_id) as pending FROM content WHERE con_poster = '$this_userid' AND con_status = 0
SELECT count(con_id) as approved FROM content WHERE con_poster = '$this_userid' AND con_status = 2
These get the number of pending, and approved items for each user who owns the submission (which is listed next to each submission.
And then there is the killer, which adds a lot of time to page execution time, since it also has to be repeated for each row of the initial query.
SELECT website_url,
website_id,
website_shady,
COALESCE(sum(dstats_hits),0)+website_in_ballast as total_in,
COALESCE(sum(dstats_hits_out),0)+website_out_ballast as total_out,
sum(dstats_hits_out_paid) as total_paid,
website_in_ballast,
website_out_ballast
FROM websites
LEFT JOIN domain_stats ON (websites.website_id = domain_stats.dstats_domid)
WHERE website_id IN (SELECT website_id FROM websites WHERE website_userid = $this_userid)
GROUP BY website_url
Is there any way I can merge the latter 3 queries int he first one? Because now.... if I have 100 items in the queue, thats 301 queries (401 if you include the sub-query), which take a while to generate the page.