CREATE TABLE telemetry_data (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,

    device_id BIGINT NOT NULL,
    ts BIGINT NOT NULL,

    metric VARCHAR(32) NOT NULL,
    source VARCHAR(64) NOT NULL,

    value DOUBLE NOT NULL,

    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

    INDEX idx_device_metric_ts (device_id, metric, ts),
    INDEX idx_ts (ts),

    FOREIGN KEY (device_id) REFERENCES devices(id)
);

CREATE INDEX idx_processed ON telemetry(processed);