perf: keep default ledger queries on platform time indexes
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user