perf: avoid correlated channel scans in portrait queries

This commit is contained in:
Yvan
2026-11-05 17:11:59 +08:00
parent a43d140339
commit aaee077253
3 changed files with 11 additions and 9 deletions
+3 -3
View File
@@ -42,7 +42,7 @@ func (r *channelPortraitRepo) EarliestDate(ctx context.Context, scope business.C
mode = 1
}
var result struct{ FirstDate *string }
q := db.Table("tab_sw_report_day r").Where("r.source_db = ? AND r.dedup_mode = ? AND r.game_id > 0", scope.Root.SourceDB, mode).Where("EXISTS (?)", channelGameTree(db, scope).Select("1").Where("p.source_db=r.source_db AND p.id=r.promote_id"))
q := db.Table("tab_sw_report_day r").Where("r.source_db = ? AND r.dedup_mode = ? AND r.game_id > 0", scope.Root.SourceDB, mode).Where("(r.source_db,r.promote_id) IN (?)", channelGameTree(db, scope).Select("p.source_db,p.id"))
if err := q.Select("CAST(MIN(r.stat_date) AS CHAR) AS first_date").Scan(&result).Error; err != nil {
return "", false, business.ErrStorage
}
@@ -56,7 +56,7 @@ func channelTierKeys(db *gorm.DB, scope business.ChannelGameScope) *gorm.DB {
if scope.IDCardDedup {
mode = 1
}
return db.Table("tab_sw_report_day_key r").Where("r.source_db = ? AND r.dedup_mode = ? AND r.game_id > 0 AND r.metric = 1 AND r.stat_date >= ? AND r.stat_date <= ? AND r.identity_key IS NOT NULL", scope.Root.SourceDB, mode, scope.BeginDate, scope.EndDate).Where("EXISTS (?)", channelGameTree(db, scope).Select("1").Where("p.source_db=r.source_db AND p.id=r.promote_id"))
return db.Table("tab_sw_report_day_key r").Where("r.source_db = ? AND r.dedup_mode = ? AND r.game_id > 0 AND r.metric = 1 AND r.stat_date >= ? AND r.stat_date <= ? AND r.identity_key IS NOT NULL", scope.Root.SourceDB, mode, scope.BeginDate, scope.EndDate).Where("(r.source_db,r.promote_id) IN (?)", channelGameTree(db, scope).Select("p.source_db,p.id"))
}
func (r *channelPortraitRepo) TierData(ctx context.Context, scope business.ChannelGameScope) (*business.ChannelTierData, error) {
result := &business.ChannelTierData{Available: true, RegisterKeys: []string{}, Payments: []*business.ChannelIdentityPay{}}
@@ -94,7 +94,7 @@ func (r *channelPortraitRepo) TierData(ctx context.Context, scope business.Chann
}
}
// 分层金额按历史成功支付累计,不受画像周期或注册日期范围限制。
q := db.Table("tab_spend s").Where("s.source_db = ? AND s.pay_status = 1", scope.Root.SourceDB).Where("EXISTS (?)", channelGameTree(db, scope).Select("1").Where("p.source_db=s.source_db AND p.id=s.promote_id"))
q := db.Table("tab_spend s").Where("s.source_db = ? AND s.pay_status = 1", scope.Root.SourceDB).Where("(s.source_db,s.promote_id) IN (?)", channelGameTree(db, scope).Select("p.source_db,p.id"))
if scope.IDCardDedup {
var rows []struct {
IDCard *string `gorm:"column:idcard"`