Get latest N records from InfluxDB table

Is it possible to query latest N records from a table with a single query?

Now I am using two queries for getting the latest N records as given below. Is this the right way?

1. SELECT LAST(*) FROM "h2o_feet"
2. SELECT LAST(*) FROM "h2o_feet" where time < t1

where t1 is the timestamp of record obtained from Step1. Step2 will be executed N-1 times with the timestamp obtained from previous step

Is it possible to query latest N records from a table with a single query?

SELECT * FROM “h20_feet” ORDER BY time DESC LIMIT N

Now I am using two queries for getting the latest N records as given below.
Is this the right way?

1. SELECT LAST(*) FROM "h2o_feet"
2. SELECT LAST(*) FROM "h2o_feet" where time < t1

where t1 is the timestamp of record obtained from Step1. Step2 will be
executed N-1 times with the timestamp obtained from previous step

Sounds inefficient, but then again several back-end data stores are also
inefficient when you ask for “ORDER BY time DESC”, so try both and see which
works best for you.

Antony.

1 Like