我有一個由兩個主要組件(傳感器和觸發器)組成的系統。使用用戶定義的事件將實時傳感器數據記錄到RDBMS中
該系統負責記錄傳感器發送的信息,並向用戶報告某些觸發器處於活動狀態或非活動狀態的時間。有關傳感器的信息
幾件:
- 所有傳感器記錄相同的字段集
- 傳感器將他們的錄音用的時候被記錄
觸發器的時間戳:
- 觸發器是用戶自定義的
- 輸入是用於一個傳感器只有一個讀數(即,不是隨時間或多個傳感器讀數)
- 僅具有活動/非活動狀態
傳感器被分配給觸發器上的多對多基礎。
我遇到的問題涉及跟蹤「何時」觸發器處於活動狀態或非活動狀態。
例如,用於觸發檢查值> 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
難道我跟上這個系統在同一個表中的元組之間存在時間關係(我知道這是不好的)。
在更一般的形式:
你如何存儲在表基於時間的統計信息/數據,並分析連續統計數據之間的差異,而不bastardising 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)
);