I’m implementing a web frontend with pagination for InfluxDB data, but I’m facing a performance challenge when dealing with buckets that have unknown data time ranges.
I’m using anchor-based pagination with the following approach:
string flux = $"from(bucket: \"{bucket}\") " +
$"|> range(start: 2020-01-01T00:00:00Z, stop: {anchorTime:yyyy-MM-ddTHH:mm:ssZ}) " +
$"{filterExpression} " +
"|> pivot(rowKey: [\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\") " +
"|> group() " +
"|> sort(columns: [\"_time\"], desc: true) " +
$"|> limit(n: {limit})";
For the first page, I use now() as the anchor time, and for subsequent pages, I use the timestamp of the last record from the previous page as the new anchor time.
The Problem
1,Performance Issue: Using a fixed start time like 2020-01-01T00:00:00Z causes the query to scan potentially years of data, making it very slow for large datasets.
2,Empty Results Problem: If I try to optimize by setting the start time closer to the anchor time (e.g., start: -30d), I risk getting empty results when the latest data in the bucket is older than 30 days.
3,Unknown Data Range: I’m dealing with various buckets where I don’t know when the latest data was written. Some buckets might have data from yesterday, others from months ago.
What I’ve Tried
1,Using relative time ranges like -30d, -365d etc., but this fails when data is older than the range.
2,Progressive range expansion (try -1d, then -7d, then -30d, etc.), but this requires multiple queries.
3,Using last() function, but it still requires a reasonable time range to be effective.
Questions
1,Is there a fast way to get the timestamp of the latest record in a bucket without specifying a time range? Something like a metadata query that doesn’t scan the actual data?
2,Am I approaching pagination wrong for InfluxDB? Should I be using a different strategy altogether?
3,Are there any InfluxDB-specific best practices for implementing pagination when the data time range is unknown?
Use Case
Frontend web application displaying time-series data Multiple buckets with varying data freshness (some recent, some very old) Need to show the latest data first, then allow users to paginate backwards in time Performance is critical - first page should load in under 500ms.
Environment
InfluxDB version: 2.x
Using Flux queries via the InfluxDB Client API
C# backend, JavaScript frontend
