refactor(go): drop redundant daily mileage vin index

This commit is contained in:
lingniu
2026-07-03 07:36:53 +08:00
parent cb63ceec2e
commit 9345fe60b5
3 changed files with 4 additions and 1 deletions

View File

@@ -44,6 +44,7 @@
- 原因:日统计是正式业务指标,应只统计已经定位到 VIN 的车辆。
- 无 VIN 的 JT808 等数据保留在 raw/Redis/session 中用于排查和待绑定,不进入正式车辆指标。
- 状态schema 使用 `PRIMARY KEY (vin, stat_date, protocol)`,不保留自增 `id``created_at`;首末总里程和样本数保留为日里程计算状态。
- 索引:主键已覆盖按 VIN 查询,不再额外维护 `idx_vin`;只保留按日期、协议日期查询需要的索引。
- 查询:`/api/stats/daily-metrics` 默认不执行 `COUNT(*)``total` 表示本页返回条数;只有报表总页数等场景传 `includeTotal=true`
- 生产RDS 已在上线前删除泛化 `vehicle_daily_metric`,改为专用 `vehicle_daily_mileage`

View File

@@ -129,6 +129,9 @@ func TestWriterEnsuresSchemaAndUpsertsDailyMileage(t *testing.T) {
if !strings.Contains(exec.calls[0].query, "PRIMARY KEY (vin, stat_date, protocol)") {
t.Fatalf("daily mileage table should key by vin/stat_date/protocol: %s", exec.calls[0].query)
}
if strings.Contains(exec.calls[0].query, "KEY idx_vin (vin)") {
t.Fatalf("daily mileage table should not keep redundant vin index covered by the primary key: %s", exec.calls[0].query)
}
if len(exec.calls) != 2 {
t.Fatalf("exec calls = %d", len(exec.calls))
}

View File

@@ -10,7 +10,6 @@ const DailyMileageTableSQL = `CREATE TABLE IF NOT EXISTS vehicle_daily_mileage (
sample_count BIGINT NOT NULL DEFAULT 0,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (vin, stat_date, protocol),
KEY idx_vin (vin),
KEY idx_stat_date (stat_date),
KEY idx_protocol_date (protocol, stat_date)
)`