Hi,
I’m still very new to Grafana. This is my second question in as many weeks.
I use SQL Server as my primary data source.
I’m creating a dashboard with many visualizations, and I’m finding a lot of the visualizations are re-using the same SQL query or two (or three), and then I apply further tweaks via Transform operations.
Maybe I’m doing this wrong, but what I’ve found works okay for me is to define visualizations (tables) in a row that I keep collapsed; call them “QueryA”, “QueryB”, “QueryC”, etc. Each one of those could, in theory, be relatively expensive to perform, so I don’t want to re-invoke them all the time. In other rows, I define visualizations that use Dashboard as their Data source (not SQL), and specify, for example, QueryB as its data source. Then I apply transformations to tweak the output of the SQL table returned and turn it into, say, a Stat panel or a piechart. Another visualization might be defined in the same way (referring to QueryB as its data source) and with its own transformation, which is different from the other visualization.
My understanding is that this should be a good way to do this, as QueryB is hitting my SQL machine only once, and the same results are re-used for both visualizations (differing mainly in the Transform steps).
The problem is that I’m encountering a lot of instances where I just cannot quite get the output I want through transformations. I’m much more familiar with SQL, so what I find myself doing is to repeat QueryB and but tweak the SQL statement “just so”, so I obtain the results I want as a SQL table, and then I can display it straight in a visualization without having to introduce Transform steps. It works, but I can’t help but think this is inefficient as it needs to hit my server with very similar (but not identical) queries.
What I’m hoping for is a way to define my large and complex query (“QueryB”) only once (shared by the visualizations that need it), and then, for any visualization that needs it, apply a separate SQL query against the output of QueryB, rather than fussing about with Transform operations.
I suppose I could even have my expensive queries (QueryA, QueryB, QueryC) each write their output to a SQL temp table (#tempA, #tempB, #tempC) and then have my visualizations use a SQL data source querying those already-populated temp tables and adding the SQL tweaks to produce the final table desired (again avoiding transform steps). But I’m not certain how I could ensure that #tempA, #tempB and #tempC are all created before I get any visualization rendered. Typically I’d want to ensure the queries that create those temp tables are run as soon as a dashboard is loaded, but before any visualization tries to run the SQL queries that will use the temp tables.
Am I making sense? Is there something such as a blog article that someone’s already written that accomplishes something similar to what I’m trying to do, and goes over the steps one at a time?
I’m almost certain this (or something similar) is being done all the time; part of my problem when searching and googling at large might be my lack of familiarity with the terminology used. Is a variable what I’m after? A template? Something else?