Din forespørgsel er en måde at lave et løbende gennemsnit på:
SELECT t.*,
(select avg(speed) from tbl tt where tt.timestamp <= t.timestamp) as avg
FROM tbl t;
Alternativet er at bruge variabler:
select t.*, (sum_speed / cnt) as running_avg_speed
from (select t.*, (@rn := @rn + 1) as cnt, (@s := @s + speed) as sum_speed
from tbl t cross join
(select @rn := 0, @s := 0) params
order by timestamp
) t;
Et indeks på tbl(timestamp)
bør forbedre ydeevnen yderligere.