-- 指标聚合表唯一约束:GORM 的 ON CONFLICT upsert 需要唯一索引才能真正生效, -- 否则每次聚合都会插入重复行(原 idx_agg 为非唯一索引)。 // Logan.Li -- 先按 (hostname, metric, interval, bucket_time) 去重,仅保留最大 id。 // Logan.Li DELETE a FROM monitor_metric_aggregates a JOIN monitor_metric_aggregates b ON a.hostname = b.hostname AND a.metric = b.metric AND a.`interval` = b.`interval` AND a.bucket_time = b.bucket_time AND a.id < b.id; CREATE UNIQUE INDEX uniq_metric_agg ON monitor_metric_aggregates (hostname, metric, `interval`, bucket_time);