Query Latest Entries

Hi,

I have a simple query that I cannot make it to perform well. I just want to query the latests n elements from a measurement, so the user can see the most recent entries on its data (not bounded by time). I think it worked fine on Influx1, but I am struggling to get similar results on Influx2.

At this moment, I have a measurement called monitoring, with some millions of entries. The following query takes more than 3 seconds to complete, which I think it is not performing fine.

import "json"
from(bucket: "alvarolb")
|> range(start: 0, stop: now())
|> filter(fn: (r) => r._measurement == "monitoring")
|> top(n: 100, columns: ["_time"])

I have tried sort and then limit, but it is practically the same.

Any ideas?

Assuming that the results are ordered by time anyway (not 100%, but I believe so), could you not use tail without having to use a sort?

So:

from(bucket: "mybucket")
  |> range(start: 0)
  |> filter(fn: (r) => r._measurement == "my-measurement")
  |> tail(n:100)

Untested so not sure if it performs better.

It performs much better! Thanks!

However, I think it is still not feasible for measurements with tags. I have another measurement with 100 different tag values, and around 20-30 fields each, with data coming every 1 minute for each tag.

The query using the tail approach takes around 30 seconds to complete, which is quite a lot. Any other workaround?