perf(finance): 优化财务仪表盘聚合查询

This commit is contained in:
yml2213
2026-11-02 19:11:22 +08:00
parent 1b23b9b14b
commit 1c18dbcb64
4 changed files with 49 additions and 5 deletions
@@ -50,11 +50,11 @@ func ownerWalletIncomeForSettledOrdersSubquery(db *gorm.DB, query DashboardQuery
// paymentOriginalAmountForRefundsInRangeSubquery 仅为当前区间内的退款单回查原支付金额。
// 退款统计原先会对全部历史支付单按 order_id 聚合;数据增长后这部分即使只看一天也会很慢。
func paymentOriginalAmountForRefundsInRangeSubquery(db *gorm.DB, query DashboardQuery) *gorm.DB {
refundOrders := db.Table("payment_orders AS refund").
refundOrders := db.Table("payment_orders AS refund USE INDEX (idx_payment_orders_dashboard_created)").
Select("DISTINCT refund.order_id").
Where("refund.biz_type IN ? AND refund.status = ?", refundBizTypes(), "refunded").
Where("refund.created_at >= ? AND refund.created_at <= ?", query.StartDate, query.EndDate)
return db.Table("payment_orders AS paid").
return db.Table("payment_orders AS paid USE INDEX (idx_payment_orders_dashboard_order)").
Select("paid.order_id, MAX(paid.amount_cent) AS amount_cent").
Joins("JOIN (?) AS refund_order ON refund_order.order_id = paid.order_id", refundOrders).
Where("paid.biz_type IN ? AND paid.status = ?", payBizTypes(), "paid").