[英]Zenoss MySQL Query - Events per hour
I am working on some trending/analysis tools, and would like to query the events MySQL database for an events per hour metric so that I can send the metric out as a datapoint in graphite. 我正在使用一些趋势/分析工具,并且想在事件MySQL数据库中查询每小时事件数指标,以便可以将指标作为石墨中的数据点发送出去。 Because some events will be in the status table, while others in the history table, I came up with the basic query I am looking for.
由于某些事件将在状态表中,而其他事件将在历史表中,因此我想到了要查找的基本查询。 Keeping in mind, that I am by no means a DBA, I would like to learn how to get a total count from both tables.
请记住,我绝不是DBA,我想学习如何从两个表中获取总计数。 Here is what I have so far:
这是我到目前为止的内容:
mysql> SELECT COUNT(*) FROM events.history WHERE firstTime > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 HOUR)) AND (severity='5' OR severity='3')
-> UNION
-> SELECT COUNT(*) FROM events.status WHERE firstTime > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 HOUR)) AND (severity='5' OR severity='3')
-> ;
+----------+
| COUNT(*) |
+----------+
| 27 |
| 267 |
+----------+
2 rows in set (0.00 sec)
mysql>
I am hoping to modify the SQL to return a single row, with the count from both tables added, so in the example it would be 294. Somebody told me that I can accomplish this with a join, but I seem to be having some trouble getting the syntax dialed in. 我希望修改SQL以返回一行,并添加两个表的计数,因此在示例中为294。有人告诉我可以通过联接来完成此操作,但似乎遇到了一些麻烦获取语法。
Put the union in a subquery and then use SUM()
to add them together. 将联合放在一个子查询中,然后使用
SUM()
将它们加在一起。
SELECT SUM(c) total
FROM (SELECT COUNT(*) c FROM events.history WHERE firstTime > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 HOUR)) AND (severity='5' OR severity='3')
UNION
SELECT COUNT(*) c FROM events.status WHERE firstTime > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 HOUR)) AND (severity='5' OR severity='3')
) u
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.