My Table has these fields:
SENSOR_ID INTEGER,
SENSOR_READING REAL,
TIME_OF_READING TIMESTAMP,
HASCHANGED BOOLEAN
Each sensor fires >10’000 readings/day. I want to log only those datapoints where SENSOR_READING is different from the last logged value.
Alternatively, I may log everything but set HASCHANGED to true whenever SENSOR_READING is different from the last logged value.
What is the most performant syntax for accomplishing that (with PostgreSQL, without php logics)?
You could do something like this
This will require the
sensor_readingcolumn to be indexed but I still doubt that it will actually be faster.You don’t need to store the
haschangedattribute because you can calculate that while retrieving the data:This assumes that
sensor_idisn’t actually unique, otherwise you couldn’t store more than one reading for a sensorIt further assumes that you change that
REALcolumn to aNUMERICcolumn, becauseREALvalues using=isn’t accurate (actually the storing isn’t accurate to begin with)