Time range dependent SQL queries for graphs

Hi there,

I’ve got a SQL table which holds values for each minute. So that is fine for a detailed view within a small time range. But if I choose a big time range (for a year, by example), the amount of data leads into long processing times and sometimes to SQL errors like “Sort aborted” which leads into Grafana is displaying “No data”.

So I pimped my sql queries to retrieve only the n-th rows to reduce the amount of data.
This works pretty cool and pushed up the speediness alot, especially if I select big time ranges. But for small time ranges, the amount of retrieved data by the SQL query would be too poor.

How can I deal with different SQL queries for different time ranges?

You need to use time aggregation based on the time range. Check https://grafana.com/docs/grafana/latest/datasources/mysql/ there are examples and macros which you can use.

1 Like

Thank you for the hint!

As the manual mentions, a filter for the current selected time range is automatically added for new queries.

I shouldn’t have completely overwritten the out-of-the-box-queries with my garbage.
Now it works (but I guess this wasn’t my last post to the community, hehe).