My initial reaction was to use LIMIT to limit the average to 5 results, which led me to the conclusion:
select a.host, avg(a.execution_time) from (select id, execution_time, host from jobs order by id desc limit 5) a group by a.host;
But it’s clear that this limits the average for the last 5 jobs, not the most recent 5 jobs per host.
It seems difficult to use LIMIT to limit the average without using any stored procedure. This led me to consider assigning each job an order or host for each host using the mysql variable.
This is untested, but the theory he illustrates should be a good starting point:
-, :
select
host,
execution_time,
@current_pos := if (@current_host = host, @current_pos, 0) + 1 as position,
@current_host := host
from
(select @current_host := null, @current_pos := 0) set_pos,
jobs
order by
host,
id desc;
, 5 :
select
jt.host,
avg(jt.execution_time)
from
(
select
host,
execution_time,
@current_pos := if (@current_host = host, @current_pos, 0) + 1 as position,
@current_host := host
from
(select @current_host := null, @current_pos := 0) set_pos,
jobs
order by
host,
id desc
) jt
where
jt.position <= 5
group
by host;
, , , , . .