perf: keep default ledger queries on platform time indexes

This commit is contained in:
Yvan
2026-11-06 07:22:51 +08:00
parent 0f0ec2e6d7
commit 7df74bc8e8
2 changed files with 72 additions and 0 deletions
+30
View File
@@ -12,6 +12,30 @@ import (
type ledgerRepo struct{ data Provider }
func NewLedgerRepo(data Provider) business.LedgerRepo { return &ledgerRepo{data: data} }
// Enumerating the stored platform codes lets MySQL use the leading column of
// the time indexes even when the caller requests every platform. Read them
// from the table so historical or newly imported codes remain in scope.
func resolveLedgerSources(db *gorm.DB, table string, filter *business.LedgerFilter) error {
if len(filter.Sources) > 0 {
return nil
}
var sources []struct{ SourceDB *string }
if err := db.Table(table).Select("source_db").Distinct().Scan(&sources).Error; err != nil {
return business.ErrStorage
}
for _, source := range sources {
if source.SourceDB == nil {
// Legacy production columns are NOT NULL. Preserve unfiltered
// semantics if another supported storage layout contains NULL.
return nil
}
}
for _, source := range sources {
filter.Sources = append(filter.Sources, *source.SourceDB)
}
return nil
}
func ledgerBasePromotes(db *gorm.DB, scope business.LedgerScope, filter business.LedgerFilter, alias string) *gorm.DB {
q := db.Table("tab_promote " + alias)
if len(filter.Sources) > 0 {
@@ -149,6 +173,9 @@ func (r *ledgerRepo) Registrations(ctx context.Context, scope business.LedgerSco
if err != nil {
return nil, err
}
if err := resolveLedgerSources(db, "tab_user", &filter); err != nil {
return nil, err
}
q := registrationQuery(db, scope, filter, timeExpression)
if err := q.Count(&page.Total).Error; err != nil {
return nil, business.ErrStorage
@@ -274,6 +301,9 @@ func (r *ledgerRepo) Spends(ctx context.Context, scope business.LedgerScope, fil
page.TotalPayAmount = nil
return page, nil
}
if err := resolveLedgerSources(db, "tab_spend", &filter); err != nil {
return nil, err
}
q := spendQuery(db, scope, filter, false)
if err := q.Count(&page.Total).Error; err != nil {
return nil, business.ErrStorage