sql - Opinions on sensor / reading / alert database design -
i've asked few questions lately regarding database design, many ;-) beleive i'm getting heart of matter design , boiling down. i'm still wrestling couple of decisions regarding how "alerts" stored in database.
in system, alert entity must acknowledged, acted upon, etc.
initially related readings alerts (very cut down) : -
[location] locationid [sensor] sensorid locationid upperlimitvalue lowerlimitvalue [sensorreading] sensorreadingid value status timestamp [sensoralert] sensoralertid [sensoralertreading] sensoralertid sensorreadingid
the last table associating readings alert, because reading dictate sensor in alert or not.
the problem design allows readings many sensors associated single alert - whereas each alert single sensor , should have readings sensor associated (should bothered db allows though?).
i thought simplify things, why bother sensoralertreading table? instead this:
[location] locationid [sensor] sensorid locationid [sensorreading] sensorreadingid sensorid value status timestamp [sensoralert] sensoralertid sensorid timestamp [sensoralertend] sensoralertid timestamp
basically i'm not associating readings alert - instead know alert active between start , end time particular sensor, , if want readings alert can do.
obviously downside no longer have constraint stopping me deleting readings occurred during alert, i'm not sure constraint neccessary.
now looking in outside developer / dba, make want sick or seem reasonable?
is there perhaps way of doing may missing?
thanks.
edit: here's idea - works in different way. stores each sensor state change, going normal alert in table, , readings associated particular state. seems solve problems - d'ya think? (the thing i'm not sure calling table "sensorstate", can't think there's better name (maybe sensorreadinggroup?) : -
[location] locationid [sensor] sensorid locationid [sensorstate] sensorstateid sensorid timestamp status isinalert [sensorreading] sensorreadingid sensorstateid value timestamp
there must elegant solution this!
revised 01 jan 11 21:50 utc
data model
i think data model should this:▶sensor data model◀. (page 2 relates other question re history).
readers unfamiliar relational modelling standard may find ▶idef1x notation◀ useful.
business (rules developed in commentary)
i did identify business rules, obsolete, have deleted them
these can "read" in relations (read adjacent data model). business rules , implied referential , data integrity can implemented in, , guaranteed by, rules, check constraints, in iso sql database. demonstration of idef1x, in development of both relational keys, , entities , relations. note verb phrases more mere flourish.
apart 3 reference tables, static, identifying entities location, networkslave, , user. sensor central system, ahve given own heading.
location
- a
location
contains one-to-manysensors
- a
location
may have 1 logger
networkslave
- a networkslave collects readings one-to-many networksensors
user
- an
user
may maintain zero-to-manylocations
- an
user
may maintain zero-to-manysensors
- an
user
may maintain zero-to-manynetworkslaves
- an
user
may perform zero-to-manydownloads
- an
user
may make zero-to-manyacknowledgements
, each on 1alert
- an
user
may take zero-to-manyactions
, each of 1actiontype
sensor
a
sensortype
installed zero-to-manysensors
a
logger
(houses and) collectsreadings
1loggersensor
a
sensor
either 1networksensor
or 1loggersensor
- a
networksensor
recordsreadings
collected 1networkslave
.
- a
- a
logger
periodicallydownloaded
one-to-many times- a
loggersensor
recordsreadings
collected 1logger
.
- a
- a
reading
may deemed inalert
, of 1alerttype
- an
alerttype
may happen on zero-to-manyreadings
.
- an
- an
alert
may 1acknowledgement
, 1 user . - an
acknowledgement
may closed 1action
, of 1actiontype
, 1user
- an
actiontype
may taken on zero-to-manyactions
- an
responses comments
sticking
id
columns on moves, interferes determination of identifiers, natural relational keys give database relational "power". surrogate keys, means additional key , index, , hinders relational power; results in more joins otherwise necessary. therefore use them when relational key becomes cumbersome migrate child tables (and accept imposed join).nullable keys classic symptom of unnormalised database. nulls in database bad news performance; nulls in fks means each table doing many things, has many meanings, , results poor code. people "refactor" databases; unnecessary relational database.
resolved:
alert
mayacknowledged
;acknowledgement
mayactioned
.the columns above line primary key (refer notation document).
sensorno
sequential number withinlocationid
; refer business rules, meaningless outsidelocation
; 2 columns form pk. when ready insert sensor (after have checked attempt valid, etc), derived follows. excludes loggersensors, zero:insert sensor values ( @locationid, sensorno = ( select isnull(max(sensorno), 0) + 1 sensor locationid = @locationid ) @sensorcode )
for accuracy or improved meaning, have changed
networkslave monitors networksensor
networkslave collects readings networksensor
.check constraints.
networksensor
,loggersensor
exclusive subtypes ofsensor
, , integrity can set check constraints.alerts, acknowledgements
,actions
not subtypes, integrity set same method, list them together.every relation in data model implemented constraint in child (or subtype) foreign key (child_fk_columns) references parent (pk_columns)
a discriminator required identify subtype
sensor
is.sensorno = 0
loggersensors
; , non-zeronetworksensors
.- the existence of
networksensors
,loggersensors
constrained fk constraintsnetworkslave
,logger
, respectively; sensor. - in
networksensor
, include check constraint ensuresensorno
non-zero in
loggersensor
, include check constraint ensuresensorno
zerothe existence of
acknowledgements
,actions
constrained identified fk constraints (anacknowledgement
cannot exist withoutalert
;action
cannot exist withoutacknowledgement
). conversely,alert
noacknowledgement
in unacknowledged state;alert
,acknowledgement
but noaction
in acknowledged un-actioned state. .
alerts. concept in design kind of (live monitoring , alert) application many small programs, running independently; using database single version of truth. programs insert rows (
readings, alerts
); other programs poll db existence of such rows (and send sms messages, etc; or hand-held units pick alerts relevant unit only). in sense, db may described message box (one program puts rows in, program reads , actions).the assumption is,
readings
sensors
being recorded "live"networkslave
, , every minute or so, new set ofreadings
inserted. background process executes periodically (every minute or whatever), main "monitor" program, have many functions within loop. 1 such function monitorreadings
, producealerts
have occurred since last iteration (of program loop).the following code segment executed within loop, 1 each alerttype. classic projection:
-- assume @loopdatetime contains datetime of last iteration insert alert select locationid, sensorno, readingdtm, "l" -- alerttype "low" sensor s, reading r s.locationid = r.locationid , s.sensorno = r.sensorno , r.readingdtm > @loopdtm , r.value < s.lowerlimit insert alert select locationid, sensorno, readingdtm, "h" -- alerttype "high" sensor s, reading r s.locationid = r.locationid , s.sensorno = r.sensorno , r.readingdtm > @loopdtm , r.value > s.upperlimit
alert
fact, exists row in database. subsequently mayacknowledged
user
(another row/fact), ,actioned
actiontype
user
.other (the creation projection act), ie. general , unvarying case, refer
alert
row inalert
; static object after creation.concerns re changing
users
. taken care of already, follows. @ top of (revised yesterday) answer, state major identifying elements static. have re-sequenced business rules improve clarity.for reasons mention,
user.name
not pkuser
, although remains alternate key (unique) , 1 used human interaction.user.name
cannot duplicated, there cannot more 1fred
; there can in terms offirstname-lastname
; 2fred bloggs
, not in terms ofuser.name
. our second fred needs chooseuser.name
. note identified indices.userid
permanent record, , pk. never deleteuser
, has historical significance. in fact fk constraints stop (never use cascade in real database, pure insanity). no need code or triggers, etc.alternately (to delete
users
never did anything, , releaseuser.name
use) allow delete long there no fk violations (ie.userid
not referenced indownload, acknowledgement, action
).
to ensure
users
current performactions
, addisobsolete
boolean in user (dm updated), , check column when table interrogated function (except reports) can implement viewusercurrent
returnsusers
.same goes
location
,networkslave
. if need differentiate current vs historical, let me know, addisobsolete
them well.i don't know: may purge database of ancient historical data periodically, delete rows (eg) on 10 years old. has done bottom (tables) first, working relations.
feel free ask questions.
note idef1 notation document has been expanded.
Comments
Post a Comment