SELECT
buttonID,
live,
title,
image,
(SELECT COUNT(*)
FROM otherdb.buttonclicks t2
WHERE t2.buttonid = t1.buttonID AND t2.`type`=0) as count
FROM buttons t1
GROUP BY buttonID
ORDER BY live DESC, buttonID ASC;
We have 78 ad buttons on our site and have separated the stats from the main db for size reasons. We record every time one of the buttons is clicked on and I am working on a stats screen that can pull the information quickly.
The above query works but takes 4000 seconds (literally) to run, how can I get the same results much more efficiently.
Running the query as:
SELECT
buttons.buttonID,
buttons.live,
buttons.title,
buttons.image,
count(buttonclicks.id) as count
FROM buttons INNER JOIN otherdb.buttonclicks ON buttons.buttonID = buttonclicks.buttonid
WHERE buttonclicks.type=0
GROUP BY buttons.buttonID
ORDER BY buttons.live DESC, buttons.buttonID ASC
is quicker but only picks up those buttons that have been clicked in the past three months (we archive the older clicks) because of the WHERE clause.
Solutions?
try this query.
I also observed that you have an image field. I think the query slow because of this since it’s a blob type that handles an image object.