Performance issue with Aggregation and Limit

Hi everyone,

I am struggling with a query performance issue with Influx 1. The idea is quite simple, I just want to get the last N aggregated data from a collection.

At this moment, I have a query like this:

SELECT MEAN(*) FROM xyz group by time(5m) fill(none) order by time desc limit 1

This collection have millions of records, and the query seems to handle all mean aggregations to provide the last one. So, the query takes several seconds to complete.

I tried to add a where to only take into account last aggregated period, and the query executes fine, but this is a different query with other meaning.

SELECT MEAN(*) FROM xyz where time>=now() -5m group by time(5m) fill(none) order by time desc limit 1

Any hint on how to improve the query?