Write a query to optimize a slow-performing database join on a table containing millions of sensor telemetry logs.
Use the provided PostgreSQL tables and restrict analysis to January 2026. Return only active sensors that have telemetry during that period.
sensor_id, site_code, sensor_name, latest_recorded_at, latest_temperature_c, previous_temperature_c, and temperature_delta_clog_idsensor_id ascendingrecorded_at >= TIMESTAMP '2026-01-01 00:00:00' and recorded_at < TIMESTAMP '2026-02-01 00:00:00'.previous_temperature_c as the preceding reading for that sensor within January, ordered by recorded_at ascending and log_id ascending.previous_temperature_c as NULL when no preceding January reading exists.temperature_delta_c as latest_temperature_c - previous_temperature_c, yielding NULL whenever either value is NULL.| Column | Type | Description |
|---|---|---|
| sensor_idPK | INTEGER | Unique sensor identifier |
| site_code | VARCHAR(20) | Site identifier assigned to the sensor |
| sensor_name | VARCHAR(100) | Human-readable sensor name |
| is_active | BOOLEAN | Whether the sensor is currently active |
| Column | Type | Description |
|---|---|---|
| log_idPK | BIGINT | Unique telemetry log identifier |
| sensor_id | INTEGER | Sensor that produced the reading |
| recorded_at | TIMESTAMP | Timestamp when the reading was recorded |
| temperature_c | NUMERIC(6,2) | Temperature reading in degrees Celsius |
| status | VARCHAR(20) | Telemetry processing status |