I found a faster solution for "select max(ID) from t group by GroupID LIMIT 1);". create trigger t_bi_prune before insert on t begin delete from t where ID = ( select ID from t as o where (select count(ID) from t where GroupID == o.GroupID ) > 1 and Ack != 1 except select ID from t as o where ID = (select ID from t where GroupID = o.GroupID order by ID desc limit 1) order by ID limit 1; end 4000 rows - insert one row : needs around 0.002 sec