fix(model): correct idx_created_at_id index column order to (created_at, id) (#5191)
The idx_created_at_id composite index on the logs table was defined as (id, created_at) because the GORM `priority` values on Id and CreatedAt were swapped. Since `id` is the auto-increment primary key, a secondary composite index leading with `id` is redundant with the PK and cannot accelerate `created_at` range scans (a range column must sit at the index prefix). This defeats the common log-listing queries (`WHERE created_at BETWEEN ? AND ? ORDER BY id DESC LIMIT n` in GetAllLogs/GetUserLogs) that the index name implies it should serve — the optimizer falls back to scanning the primary key, degrading to near full-table scans on large logs tables. Swap the priorities so the column order becomes (created_at, id), matching the index name and its intended purpose. idx_user_id_id and idx_created_at_type are unaffected. Note: GORM AutoMigrate does not change the column order of an already-existing index with the same name, so existing deployments must rebuild the index manually (see PR description for per-database DDL). Co-authored-by: wuyupeng <wuyupeng@floatmiracle.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+2
-2
@@ -32,9 +32,9 @@ func applyExplicitLogTextFilter(tx *gorm.DB, column string, value string) (*gorm
|
||||
}
|
||||
|
||||
type Log struct {
|
||||
Id int `json:"id" gorm:"index:idx_created_at_id,priority:1;index:idx_user_id_id,priority:2"`
|
||||
Id int `json:"id" gorm:"index:idx_created_at_id,priority:2;index:idx_user_id_id,priority:2"`
|
||||
UserId int `json:"user_id" gorm:"index;index:idx_user_id_id,priority:1"`
|
||||
CreatedAt int64 `json:"created_at" gorm:"bigint;index:idx_created_at_id,priority:2;index:idx_created_at_type"`
|
||||
CreatedAt int64 `json:"created_at" gorm:"bigint;index:idx_created_at_id,priority:1;index:idx_created_at_type"`
|
||||
Type int `json:"type" gorm:"index:idx_created_at_type"`
|
||||
Content string `json:"content"`
|
||||
Username string `json:"username" gorm:"index;index:index_username_model_name,priority:2;default:''"`
|
||||
|
||||
Reference in New Issue
Block a user