postgresql - 使用用户定义的事件将实时传感器数据记录到 RDBMS 中

标签 postgresql rdbms sensors

我有一个由两个主要组件、传感器和触发器组成的系统。

系统负责记录从传感器发送的信息,并向用户报告某些触发器处于事件或非事件状态的时间。

关于传感器的一些信息:

  • 所有传感器记录同一组字段
  • 传感器发送带有记录时间的记录

触发器:

  • 触发器是用户定义的
  • 输入只是一个传感器的一个读数(即,不是随时间或多个传感器的读数)
  • 只有活跃/不活跃状态

传感器以多对多的方式分配给触发器。

我遇到的问题与跟踪触发器“何时”处于事件或非事件状态有关。

例如,对于触发器检查值 > 1

sensor_id | reading             | value | trigger_value
1           2011-04-25T20:09:00   0       false
1           2011-04-25T20:11:00   1       false
1           2011-04-25T20:13:00   4       true
1           2011-04-25T20:15:00   5       true
1           2011-04-25T20:17:00   3       true
1           2011-04-25T20:19:00   6       true
1           2011-04-25T20:21:00   1       false

它可能返回:

sensor_id | reading             | event
1           2011-04-25T20:13:00   1 -- 'went active'
1           2011-04-25T20:21:00   0 -- 'went in-active'

我目前的方法涉及记录每个传感器的一组触发器的当前状态。当传感器的下一个输入进入并评估触发器时,它将此与该传感器的当前触发器状态列表进行比较。对于状态的每个变化,“激活”或“去激活”都会被记录下来。

这种方法非常简单,但它会生成数据库中“已经存在”的状态变化数据;然而数据不在单个元组中,它驻留在元组之间的关系中(在同一个表中)。

所以,问题是:

我是否会因为数据“已经存在”而改变我的方法?通过更改架构以减少它对元组之间关系的依赖,或创建 View /存储过程以根据请求对其进行分析

我是否继续使用这个系统,因为它解决了问题并隐藏了同一个表中的元组之间存在时间关系的事实(我知道这很糟糕)。

更一般的形式:

如何在表中存储基于时间的统计数据/数据,并在不破坏 RDBMS 的情况下分析连续统计数据之间的差异。

当前实现结构示例:

-- log incoming sensor data, keyed by sensor and when it was recorded
create table sensor_log (
  sensor_id integer references sensors (sensor_id),
  reading timestamp,
  data_point_a integer NOT NULL, -- example name only
  data_point_b integer NOT NULL,
  -- ...  various other data points
  primary key(sensor_id, reading)
);
-- data storage for trigger configuration
create table triggers (
  trigger_id integer,
  -- ...  configuration for the triggers
  primary key(trigger_id)
);
-- associate triggers with particular sensors on a many to many basis
create table sensor_triggers (
  sensor_id integer references sensors (sensor_id),
  trigger_id integer references triggers (trigger_id),
  -- ...  configuration for the triggers
  primary key(sensor_id, trigger_id)
);
-- record which triggers were active for a particular sensor input
-- not necessary, unless to save on recomputing past trigger activations
create table sensor_trigger_activations (
  sensor_id integer,
  reading timestamp,
  trigger_id integer references triggers (trigger_id),
  primary key (sensor_id, reading, trigger_id),
  foreign key (sensor_id, reading) references sensor_log (sensor_id, reading)
);
-- record trigger 'activations' & 'deactivations'
-- activation: active state preceded by in-active state (for a particular trigger)
-- deactivation: in-active state preceded by an active state ""
-- absense
create table sensor_trigger_events (
  sensor_id integer,
  reading timestamp,
  trigger_id integer,
  event_type smallint CHECK (event_type = 0 OR event_type = 1), -- 0 = deactivation, 1 = activation
  primary_key (sensor_id, reading, trigger_id),
  foreign key (sensor_id, reading) references sensor_log (sensor_id, reading)
);

最佳答案

首先,我认为同一张表中的元组之间的时间关系不一定不好。它不是很直接,所以隐藏它是可以的。

鉴于您的一系列要求,我真的看不出有什么可以改进的地方。

关于postgresql - 使用用户定义的事件将实时传感器数据记录到 RDBMS 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10313617/

相关文章:

function - 使用 eval 函数返回表

java - 当我尝试在 SQL Server 上执行此查询时,为什么会在日期字段上出现此错误?

MySQL 模式设计问题 - 规范化

Android - 如何处理跌倒检测算法

android - 使用 react-native-sensors 检测摇动设备事件?

sql - 如何强制两个子表之间的耦合表引用同一个主表?

sql - Postgresql 中此 CTE 的正确方法

sql - 只列出重复的名字

传感器时间戳数据表中 MySQL 查询时间太长

windows - 在 Windows 上的 emacs 中运行 M-x sql-postgres