我的应用类似于一个手机号码缴费的系统,根据手机号码的缴费订单之前销售的代理商进行返利。 当前的表结构如下: 用户表(users),其中有包含下游用户id的一个数组; 订单表(orders),其中有包含号码id的一个数组; 充值订单表(rechargeOrders),包含号码id和充值金额
当前需要按照用户的维度统计应结算的返利,代码如下:
db.users.aggregate([ {$unwind: “$acceptOrder”}, {$project: {_id: “$_id”, company: “$company”, acceptOrder: “$acceptOrder”}}, {$lookup: { from: “orders”, localField: “acceptOrder”, foreignField: “_id”, as: “orders” }}, {$unwind: “$orders”}, {$match: {“orders.freePackage”: true}}, {$project: {_id: “$_id”, company: “$company”, haveNo: “$orders.haveNo”}}, {$unwind: “$haveNo”}, {$lookup: { from: “rechargeorders”, localField: “haveNo”, foreignField: “_simcardId”, as: “rechargeOrders” }}, {$unwind: “$rechargeOrders”}, {$match: { “rechargeOrders.paid”: true, “rechargeOrders.createdAt”: {$lt: ISODate(“2017-01-11T00:00:00.000+08:00”) }}}, {$project: {_id: “$_id”, company: “$company”, fee: “$rechargeOrders.fee”, rebateFee: “$rechargeOrders.rebateFee”, rebatePaid: “$rechargeOrders.rebatePaid”}}, {$group: { _id: {_id: “$_id”, company: “$company”}, fee: {$sum: “$fee”}, rebateFeeTotal: {$sum: “$rebateFee”}, count: {$sum: 1}, rebateFeePaid: {$sum: {$cond: ["$rebatePaid", “$rebateFee”, 0]}}, rebateFeeUnpaid: {$sum: {$cond: ["$rebatePaid", 0, “$rebateFee”]}}, }}, {$project: {_id: “$_id._id”, company: “$_id.company”, fee: {$divide: ["$fee", 100]}, rebateFeeTotal: {$divide: ["$rebateFeeTotal", 100]}, count: “$count”, rebateFeePaid: {$divide: ["$rebateFeePaid", 100]}, rebateFeeUnpaid: {$divide: ["$rebateFeeUnpaid", 100]}}} ])
当前的数据库版本为3.4.1,用户表有100多条记录,订单表有600条记录,充值订单表有20k条记录。每个表的_id字段和一对多的数组都设置了索引。当前出现的问题是,在这样的数量级的情况下,使用上面的语句查询起来需要1700s左右。经过分析,瓶颈应该是在加上$group这句之后,在$group之前的语句最多只需要3s的时间。实在不知道应该如何优化?