One thing you can do to speed things up is to eliminate the `=~` in your
query. Using regexp matching means it has to do a string search over every
instance in your Prometheus for each metric. Using exact matching (`=`)
will speed things up a lot. Although you won't be able to do multiple
matching if you want that in your dashboard variables.

On Mon, Sep 18, 2023 at 7:58 AM David Leibovic <david.leibo...@gmail.com>
wrote:

> Hi there, I'm trying to optimize a slow query of this form:
>
> (1 * avg_over_time(foo{instance=~"$i"}[$interval]) <= 10) or
> (2 * avg_over_time(foo{instance=~"$i"}[$interval]) <= 20) or
> (3 * avg_over_time(foo{instance=~"$i"}[$interval]) <= 30) or
> (10 * avg_over_time(foo{instance=~"$i"}[$interval]))
>
> I suspect it's slow because of the many duplicate calls to
> avg_over_time(foo{instance=~"$i"}[$interval])
>
> Is there some way to only call the avg_over_time function once and re-use
> the results subsequently? I'm using Prometheus in conjunction with Grafana,
> in case it's relevant.
>
> The full query I'm trying to optimize is much more complicated, but I
> figured the above would be enough to understand the problem. But in case
> it's helpful, here is the full query I am trying to optimize (it's an Air
> Quality Index computation):
>
> ((50 - 0) / (12 - 0) *
> ((avg_over_time(ambient_pm25_env{instance=~"$room.*"}[$aqi_interval]) <=
> 12) - 0) + 0) or
> ((100 - 51) / (35.4 - 12.1) *
> ((avg_over_time(ambient_pm25_env{instance=~"$room.*"}[$aqi_interval]) > 12
> and avg_over_time(ambient_pm25_env{instance=~"$room.*"}[$aqi_interval]) <=
> 35.4) - 12.1) + 51) or
> ((150 - 101) / (55.4 - 35.5) *
> ((avg_over_time(ambient_pm25_env{instance=~"$room.*"}[$aqi_interval]) >
> 35.4 and
> avg_over_time(ambient_pm25_env{instance=~"$room.*"}[$aqi_interval]) <=
> 55.4) - 35.5) + 101) or
> ((200 - 151) / (150.4 - 55.5) *
> ((avg_over_time(ambient_pm25_env{instance=~"$room.*"}[$aqi_interval]) >
> 55.4 and
> avg_over_time(ambient_pm25_env{instance=~"$room.*"}[$aqi_interval]) <=
> 150.4) - 55.5) + 151) or
> ((300 - 201) / (250.4 - 150.5) *
> ((avg_over_time(ambient_pm25_env{instance=~"$room.*"}[$aqi_interval]) >
> 150.4 and
> avg_over_time(ambient_pm25_env{instance=~"$room.*"}[$aqi_interval]) <=
> 250.4) - 150.5) + 201) or
> ((400 - 301) / (350.4 - 250.5) *
> ((avg_over_time(ambient_pm25_env{instance=~"$room.*"}[$aqi_interval]) >
> 250.4 and
> avg_over_time(ambient_pm25_env{instance=~"$room.*"}[$aqi_interval]) <=
> 350.4) - 250.5) + 301) or
> ((500 - 401) / (500.4 - 350.5) *
> ((avg_over_time(ambient_pm25_env{instance=~"$room.*"}[$aqi_interval]) >
> 350.4 and
> avg_over_time(ambient_pm25_env{instance=~"$room.*"}[$aqi_interval]) <=
> 500.4) - 350.5) + 401) or
> clamp_max(avg_over_time(ambient_pm25_env{instance=~"$room.*"}[$aqi_interval]),
> 600)
>
> Thanks for any help you can provide!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Prometheus Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to prometheus-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/prometheus-users/664689b3-9f45-4b05-9438-4225e2dce773n%40googlegroups.com
> <https://groups.google.com/d/msgid/prometheus-users/664689b3-9f45-4b05-9438-4225e2dce773n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to prometheus-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/prometheus-users/CABbyFmrPbE_fjixveYZ4%3D0s2GdKEEADout9odJsu%2Byq7kymyqw%40mail.gmail.com.

Reply via email to