Calculating average (AVG) and grouping by week on large data set takes too long

Posted by caioiglesias on Stack Overflow See other posts from Stack Overflow or by caioiglesias
Published on 2010-04-09T21:08:31Z Indexed on 2010/04/09 21:33 UTC
Read the original article Hit count: 166

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about average