Calculating average (AVG) and grouping by week on large data set takes too long
- by caioiglesias
I'm getting average prices by week on 7 million rows, it's taking around 30 seconds to get the job done.
This is the simple query:
SELECT AVG(price) as price, yearWEEK(FROM_UNIXTIME(timelog)) as week from pricehistory where timelog > $range and product_id = $id GROUP BY week
The only week that actually gets data changed and is worth averaging every time is always the last one, so this calculation for the whole period is a waste of resources. I just wanted to know if mysql has a tool to help out on this.