From 9345fe60b5a7e4e6104a66f50f6416be7ef8fcfa Mon Sep 17 00:00:00 2001 From: lingniu Date: Fri, 3 Jul 2026 07:36:53 +0800 Subject: [PATCH] refactor(go): drop redundant daily mileage vin index --- docs/architecture/storage-minimal-contract.md | 1 + go/vehicle-gateway/internal/stats/daily_metric_test.go | 3 +++ go/vehicle-gateway/internal/stats/schema.go | 1 - 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/architecture/storage-minimal-contract.md b/docs/architecture/storage-minimal-contract.md index 0f27d65e..95d5664e 100644 --- a/docs/architecture/storage-minimal-contract.md +++ b/docs/architecture/storage-minimal-contract.md @@ -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`。 diff --git a/go/vehicle-gateway/internal/stats/daily_metric_test.go b/go/vehicle-gateway/internal/stats/daily_metric_test.go index b43650de..325411cf 100644 --- a/go/vehicle-gateway/internal/stats/daily_metric_test.go +++ b/go/vehicle-gateway/internal/stats/daily_metric_test.go @@ -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)) } diff --git a/go/vehicle-gateway/internal/stats/schema.go b/go/vehicle-gateway/internal/stats/schema.go index 078874d7..eb846a66 100644 --- a/go/vehicle-gateway/internal/stats/schema.go +++ b/go/vehicle-gateway/internal/stats/schema.go @@ -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) )`