Performant way for conditional filter

Hi there,
I’m new to Flux and try to visualize my data in an easy way to the user.
I’ve a lot of measurements and want to give the user the ability to filter for measurement (devicename in my case), customer and consumer. For each I got variables in Grafana, filled from queries e.g.

Variable: measurement

import “influxdata/influxdb/schema”
schema.measurements(bucket: “buckeeet”)

Custom all value = “All”

Now if I want to see some data in grafana my query took > 30 seconds

from(bucket: “buckeeet”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) =>
(if “${measurements}” != “All” then r._measurement == “${measurements}” else r._measurement != “”) and
(if “${customer}” != “All” then r.customer == “${customer}” else r.customer != “”) and
(if “${consumer}” != “All” then r.consumer == “${consumer}” else r.consumer != “”) and
(r._field == “voltage”)
)
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> yield(name: “mean”)

When the query is static and looks like the followed, I tooks 1 - 3 seconds (3 seconds for max series of 1000) to get a result.

from(bucket: “buckeeet”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) =>
r._measurement != “” and
r.customer == “secret” and
r.consumer != “” and
r._field == “voltage”
)
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> yield(name: “mean”)

Can someone help me to optimize it? I don’t found a solution online.

This post helped me out:

Next Problem to solve: Grafana is only storing 1k values in their variables.

This topic here could be closed

@norkler thank you for sharing your answer with the community! I appreciate it.