Skip to content

在大量级key的情况下,因为缺失索引导致的面板中密钥数量加载缓慢、加载时占用高的问题 #369

@Bliod-Cook

Description

@Bliod-Cook

例行检查 / Checklist

  • 我已确认目前没有类似 issue (I have checked for similar issues)
  • 我已确认我已升级到最新版本 (I have updated to the latest version)
  • 我已完整查看过项目 README,尤其是常见问题部分 (I have read the README, especially the FAQ section)
  • 我理解并愿意跟进此 issue,协助测试和提供反馈 (I am willing to follow up on this issue, assist with testing, and provide feedback)
  • 我理解并认可上述内容,并理解项目维护者精力有限,不遵循规则的 issue 可能会被无视或直接关闭 (I understand and agree to the above, and I understand that the maintainers have limited time, so issues that do not follow the rules may be ignored or closed directly)

问题描述 / Bug Description

由于在dashboard_handler.go

s.DB.Model(&models.APIKey{}).Where("status = ?", models.KeyStatusActive).Count(&activeKeys)
	s.DB.Model(&models.APIKey{}).Where("status = ?", models.KeyStatusInvalid).Count(&invalidKeys)

统计数据时分别查询了status=active和invalid的key
而在types.go

type APIKey struct {
	ID           uint       `gorm:"primaryKey;autoIncrement" json:"id"`
	KeyValue     string     `gorm:"type:text;not null" json:"key_value"`
	KeyHash      string     `gorm:"type:varchar(128);index" json:"key_hash"`
	GroupID      uint       `gorm:"not null;index" json:"group_id"`
	Status       string     `gorm:"type:varchar(50);not null;default:'active'" json:"status"`
	Notes        string     `gorm:"type:varchar(255);default:''" json:"notes"`
	RequestCount int64      `gorm:"not null;default:0" json:"request_count"`
	FailureCount int64      `gorm:"not null;default:0" json:"failure_count"`
	LastUsedAt   *time.Time `json:"last_used_at"`
	CreatedAt    time.Time  `json:"created_at"`
	UpdatedAt    time.Time  `json:"updated_at"`
}

status项没有创建索引,导致每次统计key数量时会导致mysql高占用,且耗时增加

复现步骤 / Steps to Reproduce

  1. 导入大量级的key
  2. 回到仪表板

预期结果 / Expected Behavior

在较短时间内、较低cpu占用下加载密钥数量

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions