perf: 修复 cards.go 和 users.go 中的 N+1 查询问题
cards.go: - 合并循环中的 Count 查询为单次 GROUP BY 查询 users.go: - 合并 ownApps 循环查询为单次 IN 查询 - 合并 agentApps 循环查询为批量查询 - 减少数据库查询次数从 O(n) 到 O(1)
This commit is contained in:
@@ -125,53 +125,63 @@ func handleGetUsers(c *gin.Context) {
|
||||
|
||||
appHeartbeatTimeoutMap = make(map[uint]int)
|
||||
|
||||
for _, app := range ownApps {
|
||||
ownAppIDs := make([]uint, len(ownApps))
|
||||
for i, app := range ownApps {
|
||||
ownAppIDs[i] = app.ID
|
||||
timeout := app.HeartbeatTimeout
|
||||
if timeout == 0 {
|
||||
timeout = 300
|
||||
}
|
||||
appHeartbeatTimeoutMap[app.ID] = timeout
|
||||
}
|
||||
|
||||
var appUsers []model.AppUser
|
||||
if err := database.DB.Preload("Application").Where("application_id = ?", app.ID).Find(&appUsers).Error; err != nil {
|
||||
if len(ownAppIDs) > 0 {
|
||||
if err := database.DB.Preload("Application").Where("application_id IN ?", ownAppIDs).Find(&users).Error; err != nil {
|
||||
response.Error(c, 500, "获取用户列表失败")
|
||||
return
|
||||
}
|
||||
users = append(users, appUsers...)
|
||||
}
|
||||
|
||||
for _, agentApp := range agentApps {
|
||||
var app model.Application
|
||||
if err := database.DB.First(&app, agentApp.ApplicationID).Error; err != nil {
|
||||
log.Printf("[DEBUG] Failed to get application %d: %v", agentApp.ApplicationID, err)
|
||||
continue
|
||||
}
|
||||
agentAppIDs := make([]uint, len(agentApps))
|
||||
for i, agentApp := range agentApps {
|
||||
agentAppIDs[i] = agentApp.ApplicationID
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Processing agent app: ApplicationID=%d, AppName=%s", app.ID, app.Name)
|
||||
var agentAppModels []model.Application
|
||||
if len(agentAppIDs) > 0 {
|
||||
database.DB.Where("id IN ?", agentAppIDs).Find(&agentAppModels)
|
||||
}
|
||||
|
||||
agentAppMap := make(map[uint]model.Application)
|
||||
for _, app := range agentAppModels {
|
||||
agentAppMap[app.ID] = app
|
||||
timeout := app.HeartbeatTimeout
|
||||
if timeout == 0 {
|
||||
timeout = 300
|
||||
}
|
||||
appHeartbeatTimeoutMap[app.ID] = timeout
|
||||
}
|
||||
|
||||
var cardUserIDs []uint
|
||||
if err := database.DB.Model(&model.Card{}).
|
||||
Where("creator_id = ? AND application_id = ? AND app_user_id IS NOT NULL", userID, app.ID).
|
||||
Pluck("app_user_id", &cardUserIDs).Error; err != nil {
|
||||
log.Printf("[DEBUG] Failed to get card user IDs for app %d: %v", app.ID, err)
|
||||
var allCardUserIDs []uint
|
||||
for _, agentApp := range agentApps {
|
||||
app, exists := agentAppMap[agentApp.ApplicationID]
|
||||
if !exists {
|
||||
continue
|
||||
}
|
||||
log.Printf("[DEBUG] Processing agent app: ApplicationID=%d, AppName=%s", app.ID, app.Name)
|
||||
|
||||
log.Printf("[DEBUG] Found %d card user IDs for app %d: %v", len(cardUserIDs), app.ID, cardUserIDs)
|
||||
var cardUserIDs []uint
|
||||
database.DB.Model(&model.Card{}).
|
||||
Where("creator_id = ? AND application_id = ? AND app_user_id IS NOT NULL", userID, app.ID).
|
||||
Pluck("app_user_id", &cardUserIDs)
|
||||
allCardUserIDs = append(allCardUserIDs, cardUserIDs...)
|
||||
}
|
||||
|
||||
if len(cardUserIDs) > 0 {
|
||||
var appUsers []model.AppUser
|
||||
if err := database.DB.Preload("Application").Where("id IN ?", cardUserIDs).Find(&appUsers).Error; err != nil {
|
||||
log.Printf("[DEBUG] Failed to get users for app %d: %v", app.ID, err)
|
||||
continue
|
||||
}
|
||||
log.Printf("[DEBUG] Found %d users for app %d", len(appUsers), app.ID)
|
||||
if len(allCardUserIDs) > 0 {
|
||||
var appUsers []model.AppUser
|
||||
if err := database.DB.Preload("Application").Where("id IN ?", allCardUserIDs).Find(&appUsers).Error; err != nil {
|
||||
log.Printf("[DEBUG] Failed to get users: %v", err)
|
||||
} else {
|
||||
users = append(users, appUsers...)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user