如何从每 2 分钟存储的状态日志中确定事件的开始/结束时间
Posted
技术标签:
【中文标题】如何从每 2 分钟存储的状态日志中确定事件的开始/结束时间【英文标题】:How to determine start/end time for events from status logs stored every 2 minutes 【发布时间】:2019-10-03 10:29:47 【问题描述】:MariaDB 版本:版本:10.0.38-MariaDB-0+deb8u1
我有一个表格,其中每 2 分钟报告一次设备的状态(开/关),其时间戳为 unix 时间。
select * from devices_stats
where device_id = 'LivingLight'
AND timestamp BETWEEN 1570080242 AND 1570084922;
+-------+-------------+--------+------------+-------------+
| id | device_id | status | timestamp | device_iddr |
+-------+-------------+--------+------------+-------------+
| 16416 | LivingLight | OFF | 1570080242 | 1 |
| 16427 | LivingLight | OFF | 1570080363 | 1 |
| 16438 | LivingLight | OFF | 1570080483 | 1 |
| 16449 | LivingLight | OFF | 1570080602 | 1 |
| 16460 | LivingLight | OFF | 1570080723 | 1 |
| 16471 | LivingLight | OFF | 1570080842 | 1 |
| 16482 | LivingLight | ON | 1570080963 | 1 |
| 16493 | LivingLight | ON | 1570081083 | 1 |
| 16504 | LivingLight | ON | 1570081203 | 1 |
| 16515 | LivingLight | ON | 1570081323 | 1 |
| 16526 | LivingLight | ON | 1570081443 | 1 |
| 16537 | LivingLight | ON | 1570081563 | 1 |
| 16548 | LivingLight | ON | 1570081682 | 1 |
| 16559 | LivingLight | ON | 1570081803 | 1 |
| 16570 | LivingLight | ON | 1570081922 | 1 |
| 16581 | LivingLight | ON | 1570082042 | 1 |
| 16592 | LivingLight | ON | 1570082163 | 1 |
| 16603 | LivingLight | ON | 1570082283 | 1 |
| 16614 | LivingLight | ON | 1570082402 | 1 |
| 16625 | LivingLight | ON | 1570082523 | 1 |
| 16636 | LivingLight | ON | 1570082643 | 1 |
| 16647 | LivingLight | ON | 1570082762 | 1 |
| 16658 | LivingLight | ON | 1570082882 | 1 |
| 16669 | LivingLight | OFF | 1570083003 | 1 |
| 16680 | LivingLight | OFF | 1570083123 | 1 |
| 16691 | LivingLight | OFF | 1570083242 | 1 |
| 16702 | LivingLight | OFF | 1570083363 | 1 |
| 16713 | LivingLight | OFF | 1570083483 | 1 |
| 16724 | LivingLight | OFF | 1570083603 | 1 |
| 16735 | LivingLight | OFF | 1570083722 | 1 |
| 16746 | LivingLight | OFF | 1570083843 | 1 |
| 16757 | LivingLight | OFF | 1570083963 | 1 |
| 16768 | LivingLight | OFF | 1570084083 | 1 |
| 16779 | LivingLight | OFF | 1570084202 | 1 |
| 16790 | LivingLight | OFF | 1570084323 | 1 |
| 16801 | LivingLight | OFF | 1570084442 | 1 |
| 16812 | LivingLight | ON | 1570084563 | 1 |
| 16823 | LivingLight | ON | 1570084683 | 1 |
| 16834 | LivingLight | OFF | 1570084803 | 1 |
| 16845 | LivingLight | OFF | 1570084922 | 1 |
+-------+-------------+--------+------------+-------------+
我想检索带有开始和结束时间的“ON”事件列表。
考虑到上面的例子,我想要这样的输出:
+-------------+------------+------------+
| device_id | start | stop |
+-------------+------------+------------+
| LivingLight | 1570080963 | 1570082882 |
| LivingLight | 1570084563 | 1570084683 |
您能帮我创建查询吗?
【问题讨论】:
你的 mysql 版本是多少?运行SELECT Version();
查询并编辑问题以添加版本的详细信息。
这是我的版本:10.0.38-MariaDB-0+deb8u1
糟糕,这意味着没有窗口,如果可能的话,考虑升级到更现代的版本,这样查询会更容易..
是否应该只为该样本数据返回这 2 行?
@RaymondNijland 即使有 Windowing,它也不是 LEAD()
的直接应用程序。
【参考方案1】:
这是一种使用用户定义变量的方法。值得注意的是,在这个问题中,即使是 Windowing 函数也不能直接使用。但是,您的版本很旧,也不支持它们。以下解决方案是通用的,如果您不在device_id
上使用WHERE
条件并且希望结果集中有多个device_id
,则处理场景。
这里的总体思路是,我们为具有相同device_id
和status
值(ON 或OFF)的连续行(基于时间戳)计算“岛号”(在查询中表示为chng
)。最终,我们可以只过滤掉那些状态为ON
的岛,然后聚合得到MIN()
(开始时间戳)和MAX()
(停止时间戳)。
架构 (MySQL v5.7)
CREATE TABLE device_stats
(`id` int, `device_id` varchar(11), `status` varchar(3), `timestamp` int, `device_iddr` int)
;
INSERT INTO device_stats
(`id`, `device_id`, `status`, `timestamp`, `device_iddr`)
VALUES
(16416, 'LivingLight', 'OFF', 1570080242, 1),
(16427, 'LivingLight', 'OFF', 1570080363, 1),
(16438, 'LivingLight', 'OFF', 1570080483, 1),
(16449, 'LivingLight', 'OFF', 1570080602, 1),
(16460, 'LivingLight', 'OFF', 1570080723, 1),
(16471, 'LivingLight', 'OFF', 1570080842, 1),
(16482, 'LivingLight', 'ON', 1570080963, 1),
(16493, 'LivingLight', 'ON', 1570081083, 1),
(16504, 'LivingLight', 'ON', 1570081203, 1),
(16515, 'LivingLight', 'ON', 1570081323, 1),
(16526, 'LivingLight', 'ON', 1570081443, 1),
(16537, 'LivingLight', 'ON', 1570081563, 1),
(16548, 'LivingLight', 'ON', 1570081682, 1),
(16559, 'LivingLight', 'ON', 1570081803, 1),
(16570, 'LivingLight', 'ON', 1570081922, 1),
(16581, 'LivingLight', 'ON', 1570082042, 1),
(16592, 'LivingLight', 'ON', 1570082163, 1),
(16603, 'LivingLight', 'ON', 1570082283, 1),
(16614, 'LivingLight', 'ON', 1570082402, 1),
(16625, 'LivingLight', 'ON', 1570082523, 1),
(16636, 'LivingLight', 'ON', 1570082643, 1),
(16647, 'LivingLight', 'ON', 1570082762, 1),
(16658, 'LivingLight', 'ON', 1570082882, 1),
(16669, 'LivingLight', 'OFF', 1570083003, 1),
(16680, 'LivingLight', 'OFF', 1570083123, 1),
(16691, 'LivingLight', 'OFF', 1570083242, 1),
(16702, 'LivingLight', 'OFF', 1570083363, 1),
(16713, 'LivingLight', 'OFF', 1570083483, 1),
(16724, 'LivingLight', 'OFF', 1570083603, 1),
(16735, 'LivingLight', 'OFF', 1570083722, 1),
(16746, 'LivingLight', 'OFF', 1570083843, 1),
(16757, 'LivingLight', 'OFF', 1570083963, 1),
(16768, 'LivingLight', 'OFF', 1570084083, 1),
(16779, 'LivingLight', 'OFF', 1570084202, 1),
(16790, 'LivingLight', 'OFF', 1570084323, 1),
(16801, 'LivingLight', 'OFF', 1570084442, 1),
(16812, 'LivingLight', 'ON', 1570084563, 1),
(16823, 'LivingLight', 'ON', 1570084683, 1),
(16834, 'LivingLight', 'OFF', 1570084803, 1),
(16845, 'LivingLight', 'OFF', 1570084922, 1)
;
查询 #1
SELECT
device_id, MIN(timestamp) AS start, MAX(timestamp) AS stop
FROM
(
SELECT
@c := IF(@s <> status OR @d <> device_id , @c+1, @c) AS chng,
@s := status AS status,
@d := device_id AS device_id,
timestamp
FROM
(
SELECT device_id, status, timestamp
FROM device_stats
WHERE device_id = 'LivingLight'
AND timestamp BETWEEN 1570080242 AND 1570084922
ORDER BY device_id, timestamp
) t1
CROSS JOIN (SELECT @s := '',
@d := '',
@c := 0) vars
) t2
WHERE t2.status = 'ON'
GROUP BY device_id, chng;
| device_id | start | stop |
| ----------- | ---------- | ---------- |
| LivingLight | 1570080963 | 1570082882 |
| LivingLight | 1570084563 | 1570084683 |
View on DB Fiddle
【讨论】:
您可以将此查询优化为具有较少嵌套的子查询。如果您将MySQL user variable init (post of mine) 移至 WHERE 子句...但这可能很棘手,但我可以成为一个不错的技巧:- ) @RaymondNijland 实际上,请检查:mysqlserverteam.com/… 由于(有时)触发了 ORDER BY 优化,我更喜欢先在子查询中明确排序数据,然后在外部查询中执行所有操作计算。 @RaymondNijland 没关系;我明白你的意思了。在查看了该答案中的第三个查询之后。 没问题,我理解你的论点,因为 WHERE 总是在 ORDER BY 之前执行,它应该总是正常工作..但是像你一样使用额外的子查询更容易正确..以上是关于如何从每 2 分钟存储的状态日志中确定事件的开始/结束时间的主要内容,如果未能解决你的问题,请参考以下文章