Skip to content

TimeSeries 'latest' scans the whole series to return one row, though queryDescending(limit 1) exists #7322

Closed

Description

@robfrank

Summary

Reading the newest sample of a time-series type materializes the entire series and throws all of it
away but the last row. TimeSeriesGateway.latest (extracted from GetTimeSeriesLatestHandler in
#7305, same code as before) is:

final List<Object[]> rows = engine.query(Long.MIN_VALUE, Long.MAX_VALUE, null, tagFilter);
return rows.isEmpty() ? null : rows.get(rows.size() - 1);

TimeSeriesEngine.query merges every shard's full range into one ArrayList and sorts it, so a type
holding N samples costs O(N) heap and O(N log N) time to answer a question about one row.

The engine already has the bounded form, added for exactly this shape in #5414/#5416:

public List<Object[]> queryDescending(final long fromTs, final long toTs, final int[] columnIndices,
    final TagFilter tagFilter, final int limit, final AggregationMetrics metrics)

whose javadoc says "an unbounded last-point query costs O(shards x blocks touched) instead of
O(series)". FetchFromTimeSeriesStep uses it; the latest path does not.

Consequence

GET /api/v1/ts/{database}/latest and, since #7305, the gRPC TimeSeriesLatest RPC both load the
whole series to return one row. On a type with millions of samples that is a large allocation per
call, on an endpoint whose whole purpose is to be cheap enough to poll - a Grafana single-stat panel
or a health check hits it on every refresh.

Why it was not fixed in #7305

Swapping in queryDescending(..., 1, null) changes behaviour where several samples share the maximum
timestamp: the ascending merge takes the last of the ties in shard order, the descending scan takes
whichever trimToDescendingLimit keeps. Nothing specifies which row wins a tie today, so the change
is safe in substance but not a refactor, and #7305 needed the two protocols to answer identically
against a known baseline.

Suggested fix

Move TimeSeriesGateway.latest onto queryDescending(from, to, null, tagFilter, 1, null), decide and
document the tie-break, and add a test that pins it. Both protocols pick the change up at once, since
both already call that one method.

Activity

  1. added
    grpc
    server
    on Sep 9, 2026
  2. self-assigned this
    on Sep 9, 2026
  3. added this to the 26.10.1 milestone on Sep 9, 2026
  4. added
    in progress
    and removed
    in progress
    on Sep 9, 2026
  5. added 2 commits that reference this issue on Sep 10, 2026
  6. robfrank commented on Sep 10, 2026

    @robfrank
    CollaboratorAuthor

    Fixed in #7376.

    latest was asking the engine for the whole series (query(MIN, MAX)) and then taking the last row, which merged every shard's full range into one list and sorted it - O(series) heap for a one-row question. It now calls queryDescending(from, to, null, tagFilter, 1, null), the bounded form added for exactly this shape in #5414/#5416, so the engine stops after the first row. Both callers pick this up at once: GET /ts/{database}/latest and the gRPC TimeSeriesLatest RPC go through the same method.

    One behaviour change worth knowing: when several rows share the newest timestamp, the two scans sort stably in opposite directions, so the old ascending scan returned the last of the tied rows and the bounded one returns the first. The timestamp is the guarantee; narrow with tags if you need to disambiguate. This is now stated in the javadoc and pinned by Issue7322LatestBoundedTest.

    Still open


    ❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.

  7. removed
    in progress
    on Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

buggrpcperformanceservertimeseries

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions