Flux Querys with variables are very slow when running inside grafana

Hi everyone :slight_smile:

i am using grafana cloud in the latest version. Right now i am running into a trivial problem.

  • My goal is it to query my influxdb-cloud data inside my grafana-cloud instance.

  • Compared to the normal query syntax inside influxdb:

from(bucket: "TestBucket")
  |> range(start: -14d)
  |> filter(fn: (r) => r["_measurement"] == "Measurement_1")
  |> filter(fn: (r) => r["Bridge"] == "Bridge_1" or r["Bridge"] == "Bridge_2")
  |> filter(fn: (r) => r["Network"] == "Network_1")
  |> filter(fn: (r) => r["SensorID"] == "1" or r["SensorID"] == "2" or r["SensorID"] == "3" or r["SensorID"] == "4" or r["SensorID"] == "5")
  |> filter(fn: (r) => r["_field"] == "SensorValue")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "mean")

my Grafana query looks like the following:


from(bucket: "TestBucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => contains(value: r["_measurement"], set: ${Measurements:json}))
  |> filter(fn: (r) => 
  contains(value: r["Bridge"], set: ${Bridge:json}) and
  contains(value: r["Network"], set: ${Network:json}))
  |> filter(fn: (r) => contains(value: r["SensorID"], set: ${SensorID:json}))
  |> filter(fn: (r) => r["_field"] == "SensorValue")
  |> aggregateWindow(every: v.windowPeriod, fn: max, createEmpty: false)
  |> yield(name: "max")

This is because i use 4 different Variables which let me visualize a specific measurement unit and its selected gateway, network, and sensorid via a dropdown.

  • The problem with my grafana instance querys is that compared to my query inside my influxdb-cloud it takes like 10 times longer.

  • The question now would be what is causing those delays.

  • Underneath i also have my variables which are inside my dashboard settings:

  1. Measurement:
import "influxdata/influxdb/schema"
schema.measurements(bucket: "TestBucket")
  1. Bridge:
from(bucket: "TestBucket")
|> range(start: -6h, stop: now())
|> filter(fn: (r) => contains(value: r["_measurement"], set: ${Measurement:json}))
|> keyValues(keyColumns: ["Bridge"])
|> group()
|> keep(columns: ["Bridge"])
|> distinct(column: "Bridge")
  1. Network:
from(bucket: "TestBucket")
|> range(start: -6h, stop: now())
|> filter(fn: (r) => contains(value: r["_measurement"], set: ${Measurements:json}))
|> filter(fn: (r) => contains(value: r["Bridge"], set: ${Bridge:json}))
|> keyValues(keyColumns: ["Network"])
|> group()
|> keep(columns: ["Network"])
|> distinct(column: "Network")
  1. SensorID:
from(bucket: "TestBucket")
|> range(start: -6h, stop: now())
|> filter(fn: (r) => contains(value: r["_measurement"], set: ${Measurements:json}))
|> filter(fn: (r) => contains(value: r["Bridge"], set: ${Bridge:json}))
|> filter(fn: (r) => contains(value: r["Network"], set: ${Network:json}))
|> keyValues(keyColumns: ["SensorID"])
|> group()
|> keep(columns: ["SensorID"])
|> distinct(column: "SensorID")

I would really appreciate some input or help regarding this topic.
Greetings
Grafu :slight_smile:

Hello

Here is one of my go to on speeding flux

Hi there Yosiasz, thank you very much i will take a closer look into this. If i find something useful i will reply in this thread :slight_smile:

1 Like

another good one also

another key thing to understand is the following

image