Efficient way to query only 1 record and 1 column (_time) of the last measurement for a filtered key

I want to fetch the “_time” of the last inserted measurement.

With the help of top Limit() with multiple optional fields (limit limits each field separately) I learned about funcion v1.fieldsAsCols(), which corresponds to schema.fieldsAsCols() in v2.

The issue is that I receive loads of tables as result without fieldsAsCols() and all fields when using this function, while I still only need the value of the _time column.

import "influxdata/influxdb/schema"
from(bucket: "dsm")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["equipment_id_electricity"] == "39")
  |> last()
  |> schema.fieldsAsCols()
  |> limit(n: 1)
  |> keep(columns: ["_time"])

This query looks processing intensive for the simple task.

Is there a way to optimize the query and get only one result (1 row and 1 column)?


PS Thumbs up for “Data Explorer” that its function [ Inject ] button also automagically creates the import line for influxdata/influxdb/schema

1 Like