I’ve tried looking around for a solution but can’t seem to find one.
I have a table of post ratings with a column “rating” that is 0 or 1 based on whether a person gave a thumbs up or a thumbs down to a post. There are currently around 2000 approved postings and 2.8 million ratings.
I would like to find the top 20 rated posts that have been approved through moderation. Here is the query I thought would work:
SELECT p.* FROM `posts` p
LEFT JOIN `ratings` r ON r.pid=p.id
WHERE p.approved=1
GROUP BY p.id
ORDER BY AVG(r.rating) DESC LIMIT 0,20
I’ve added an index on r.rating and the query still takes around 3 seconds. How to reduce this time?
Well, create one more aggregate table, which will contain calculated values for all ratings. And recalculate it by some trigger or by scheduler.