0

One of our websites has a table with about 60'000 records in it. Recently we noticed the page was timing out and could only be resolved by setting the memory limit to -1. This allowed the page to load but it was very slow. Furthermore I did not believe this was the correct way to resolve the problem, as obviously it indicates something is not quite right.

I managed to output the query that the page was running:

SELECT u.*,
(SELECT COUNT(*) FROM enquiry e WHERE e.user_id = u.id AND e.deleted = 0 AND e.time_started != 0) AS opened_count,
(SELECT COUNT(*) FROM enquiry e WHERE e.user_id = u.id AND e.deleted = 0 AND e.confirmed = 1 AND e.time_started != 0) AS confirmed_count,
(SELECT COUNT(*) FROM enquiry e WHERE e.user_id = u.id AND e.deleted = 0 AND e.call_back = 1 AND e.time_started != 0) AS call_back_count
FROM user u
WHERE u.active = 1 AND u.deleted = 0
ORDER BY u.username

I ran this query in phpMyAdmin and it takes over 30 seconds to return the results.

I feel the query needs optimising in some way but I'm struggling to work out how. I'm guessing I need to use a JOIN of some sort?

1
  • 3
    Please provide the EXPLAIN as well as DESCRIBE results.
    – Kermit
    Sep 30, 2013 at 19:33

1 Answer 1

7

You're really running >180,000 queries, since each of those 3 subqueries will be run once for every row in the user table.

You could try simplifying into a standard join with some groups, e.g.

SELECT user.*,
    COUNT(enq.id) AS opened_count
    SUM(e.confirmed = 1) AS confirmed_count
    SUM(e.call_back = 1) AS call_back_count
FROM user
LEFT JOIN  enquiry ON enquiry.user_id = user.id
WHERE user.active = 1 and user.deleted AND enquiry.deleted = 0
GROUP BY user.id
5
  • Doesn't this need a group by?
    – Laurence
    Sep 30, 2013 at 19:38
  • Thanks Marc. That has already made a massive difference. One small thing I need help with - how do I integrate my WHERE condition in to the COUNT query, i.e. WHERE enquiry.time_started != 0
    – MAX POWER
    Sep 30, 2013 at 20:03
  • Where ... and enquiry.time_started != 0.
    – Marc B
    Sep 30, 2013 at 20:06
  • Hmmm.. but that condition is only needed for one particular count result and not for the whole query..
    – MAX POWER
    Sep 30, 2013 at 20:08
  • then just add it inside the count/sum calls. You can have arbitrary logic in there, and mysql will take the resulting true/false and cast it to int 0/1 for the sum/count purposes.
    – Marc B
    Sep 30, 2013 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.