I have a time-series table like this:
Key | Value | Epoch
It could store many values for each Key, all marked with when then item was generated (epoch time). Let's say I have 100 values per key, I want to average the value of the last 50, but only if has a statistically significant amount of data, let's say 20. Currently, I have:
select key, avg(value)
from t
where
epoch > (select epoch from t order by epoch desc limit 50, 1)
group by key
having count(*) > 20
This performs pretty slowly and it uses a MySQL extension on the limit keyword. Is there a better way to do this?