We use a monitoring solution (zabbix). It stores trends data (MySQL 5.X) using a table called trends_uint with the following structure (simplified):
itemid clock value_avg
1 1238774400 100
1 1250773900 70
1 1250773200 50
The clock field stores datetime values using Unix time.
I’m trying to extract a report to see the consumption of a monitored item. I thought doing this by selecting the oldest record and the newest record. I achieved it doing the following:
SELECT
(SELECT t.clock FROM trends_uint t WHERE t.itemid = 1 AND t.clock IN (SELECT MIN(t.clock) FROM trends_uint t WHERE t.itemid = 1)) AS minClock,
(SELECT t.value_avg FROM trends_uint t WHERE t.itemid = 1 AND t.clock IN (SELECT MIN(t.clock) FROM trends_uint t WHERE t.itemid = 1)) AS minValueAvg,
(SELECT t.clock FROM trends_uint t WHERE t.itemid = 1 AND t.clock IN (SELECT MAX(t.clock) FROM trends_uint t WHERE t.itemid = 1)) AS maxClock,
(SELECT t.value_avg FROM trends_uint t WHERE t.itemid = 1 AND t.clock IN (SELECT MAX(t.clock) FROM trends_uint t WHERE t.itemid = 1)) AS maxValueAvg,
(SELECT maxClock - minClock) AS timeGap,
(SELECT minValueAvg - maxValueAvg) AS valueGap,
(SELECT timeGap/86400) AS daysGap,
(SELECT valueGap/daysGap/1024/1024) AS consumeMB
My problem is that I have lot’s servers from which I want to calculate the consumption. I don’t want to change the query for which one (changing the itemid).
The system has another table (items) from which I could extract the items that I need to put into the report. Something like SELECT itemid FROM items WHERE ...
So this is my question: instead of using a hardcoded itemid, is it possible to make it “dynamic”, for instance using the returned codes from another query (ex.: SELECT ... FROM ... WHERE itemid IN (SELECT itemid FROM items WHERE ...)?
TIA,
Bob
It looks like what you want is to use either your programming language’s or MySQL user-defined variables to store the ID you selected once and reuse it.
I’m assuming you’re using some sort of a language, say PHP, to wrap around these calls.
You can then:
use MySQL user-defined variables and save the itemid, with something like
SET @itemid=SELECT itemid FROM trends_uint WHERE ….
SELECT …. WHERE t.itemid = @itemid ….