I have a sensor_status table that logs the status of hundreds of sensors every 45 minutes
CREATE TABLE sensor_status (
status_id INT AUTO_INCREMENT PRIMARY KEY,
sensor_id INT NOT NULL,
status ENUM('Online','Offline','Unknown') NOT NULL,
status_timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
...
);
Over time, this table will grow very large and most queries only care about:
- The latest status for each sensor
- The last 24 hours of data
- The last week of data
Right now, every dashboard/API request scans the full sensor_status table with a WHERE status_timestamp >= … filter, which is becoming slow.
I'm fairly new to db management and organization so I am curious as to what are common practices to "cache" time-series tables without constantly querying the full time series table?
I'm currently considering doing the following:
Separate "latest" table with insert trigger
Snapshot tables with event scheduler. Rebuild a sensor_status_24hr and sensor_status_1wk every day.
Thank you!
PARTITIONis unlikely to help performance.