我有一个由两个主要组成部分组成的系统,传感器和触发器。
系统负责记录传感器发送的信息,并向用户报告某些触发器处于活动或非活动状态的时间。
有关传感器的一些信息:
触发器:
传感器分配给多个触发器。
我遇到的问题与跟踪“何时”触发器处于活动还是非活动状态有关。
例如,对于触发检查值> 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'
我目前的方法是记录每个传感器的触发器集的当前状态。当下一个输入进入传感器并评估触发时,它会将其与该传感器的当前触发状态列表进行比较。对于状态的每次改变,记录“激活”或“去激活”。
这种方法非常简单,但它会生成有关数据库中“已经存在”的状态更改的数据;但是数据不在单个元组中,它驻留在元组之间的关系中(在同一个表中)。
所以,问题是:
我是否改变了我的方法,因为数据“已经存在”;通过更改模式使其更少依赖于元组之间的关系,或创建视图/存储过程来分析它的apon请求
OR
我是否继续使用这个系统,因为它解决了这个问题,并且隐藏了同一个表中元组之间存在时间关系的事实(我知道这是不好的)。
以更一般的形式:
如何在表格中存储基于时间的统计数据/数据,并分析连续统计数据之间的差异,而不会滥用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)
);
答案 0 :(得分:0)
首先,我不认为同一个表中的元组之间的时间关系一定是坏的。它不是真正的直接,所以隐藏它是好的。
鉴于您的要求,我并没有看到太多可以改进的地方。