Opinions on sensor / reading / alert database design

后端 未结 3 1098
别那么骄傲
别那么骄傲 2020-12-06 02:50

I\'ve asked a few questions lately regarding database design, probably too many ;-) However I beleive I\'m slowly getting to the heart of the matter with my design and am s

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-06 03:25

    The main "triangle" you have to deal with here is Sensor, [Sensor]Reading, and Alert. Presuming you have to track activity as it is occuring (as opposed to a "load it all at once" design), your third solution is similar to something we did recently. A few tweaks and it would look like:

    [Location] 
    LocationId 
    
    [Sensor] 
    SensorId 
    LocationId 
    CurrentSensorState  --  Denormalized data!
    
    [SensorReading] 
    SensorReadingId 
    SensorState
    Value 
    Timestamp 
    
    [SensorStateLog] 
    SensorId 
    Timestamp 
    SensorState
    Status   --  Does what?
    IsInAlert 
    (Primary key is {SensorId, Timestamp})
    

    "SensorState" could be SensorStateId, with an associated lookup table listing (and constraining) all possible states.

    The idea is, you Sensor contains one row per sensor and shows it's current state. SensorReading is updated continuously with sensor readings. If and when a given sensors current state changes (i.e. new Reading's state differs from Sensor's current state), you change the current state and add a row to the SensorStateLog showing the change in state. (Optionally, you could update the "prior" entry for that sensor with a "state ended" timestamp, but that's fussy code to write.)

    CurrentSensorState in the Sensor table is denormalized data, but if properly maintained (and if you have millions of rows) it will make querying current state vastly more efficient and so may be worth the effort.

    The obvious downside of all this is that Alerts are no longer an entity, and they become that much harder to track and identify. If these must be readily and immediately identifiable and usable, your third scheme won't do what you need it to do.

提交回复
热议问题