【问题标题】:improve query performance mongodb提高查询性能mongodb
【发布时间】:2021-12-15 02:13:57
【问题描述】:

我正在使用聚合查询来检索 20000 条记录。检索它需要很多时间。我将在下面提到我的查询,请帮助我提高查询性能。

查询:

[err, data] = await to(LeadsLog.aggregate([
    {$lookup:{
      from: "leads",
      localField: "leadId",
      foreignField: "_id",
      as: "leadId"
    }},
    {$lookup:{
      from: "company_contacts",
      localField: "leadId.assignedTo",
      foreignField: "_id",
      as: "assignedTo"
    }},
    {
      $unwind:{
       path: "$leadId",
       preserveNullAndEmptyArrays: true
     }
    },
    {
      $match:{"leadId.assignedTo":new mongoose.Types.ObjectId(userId),
               "result":{$eq:null}}
    },
    { '$facet'    : {
        metadata: [ { $count: "total" }, { $addFields: { page: 1 } } ],
        data: [ { $skip: 0 }, { $limit: 20000 } ] 
    } }
] ));

LeadId:

{
    "_id" : ObjectId("617a84b401c98424e00d1310"),
    "status" : true,
    "address" : "Howmif Trail",
    "city" : "Kinawnet",
    "state" : "LA",
    "country" : "LA",
    "pincode" : null,
    "extraFormObject" : null,
    "lead_name" : "Jayden",
    "phone" : "(524) 387-4912",
    "email" : "niligis@taptehe.cm",
    "company" : ObjectId("6155c2758609663d10fff796"),
    "createdBy" : ObjectId("6155c2758609663d10fff798"),
    "createdAt" : ISODate("2021-10-28T11:08:40.433Z"),
    "updatedAt" : ISODate("2021-10-30T04:43:49.490Z")
}

LeadLog:

{
    "_id" : ObjectId("617a84bf01c98424e00daf52"),
    "callLogId" : null,
    "result" : null,
    "assignedTo" : ObjectId("6155c2758609663d10fff798"),
    "extraFormObject" : null,
    "subResult" : null,
    "apptDate" : null,
    "nextcallDate" : ISODate("2021-10-28T11:02:50.516Z"),
    "callDate" : null,
    "leadId" : ObjectId("617a84b401c98424e00d1310"),
    "company" : ObjectId("6155c2758609663d10fff796"),
    "createdAt" : ISODate("2021-10-28T11:08:50.962Z"),
    "updatedAt" : ISODate("2021-10-30T04:43:50.281Z")
}

请帮助我更好的解决方案。谢谢。

【问题讨论】:

    标签: node.js mongodb mongoose mongodb-query query-optimization


    【解决方案1】:

    您可以通过一些简单的调整来改进现有查询:

    • 使中间结果尽可能小;一种常见的方法是尽早推送$match 阶段
    • 尽可能使用Pipeline Coalescence Optimization;一个常见的元组是$lookup + $unwind 组合
    • 索引$match 字段和$lookup 字段

    根据前 2 点,这是我建议的查询形式:

    • 您可以看到result : {$eq: null} 被推送到第一阶段。性能增益将取决于子句的选择性。
    • $lookup$unwind 潜在客户组合在一起以利用合并优化。
    • "leadId.assignedTo": new mongoose.Types.ObjectId(userId) 被提前移动以最小化中间结果大小
    • 不要忘记索引相关的$match 字段和$lookup 字段。根据我的个人经验,良好的索引使用对性能的帮助最大。
    [err, data] = await to(LeadsLog.aggregate([
      {
        $match: {
          "result": {
            $eq: null
          }
        }
      },
      {
        $lookup: {
          from: "leads",
          localField: "leadId",
          foreignField: "_id",
          as: "leadId"
        }
      },
      {
        $unwind: {
          path: "$leadId",
          preserveNullAndEmptyArrays: true
        }
      },
      {
        $match: {
          "leadId.assignedTo": new mongoose.Types.ObjectId(userId)
        }
      },
      {
        $lookup: {
          from: "company_contacts",
          localField: "leadId.assignedTo",
          foreignField: "_id",
          as: "assignedTo"
        }
      },
      {
        "$facet": {
          metadata: [
            {
              $count: "total"
            },
            {
              $addFields: {
                page: 1
              }
            }
          ],
          data: [
            {
              $skip: 0
            },
            {
              $limit: 20000
            }
          ]
        }
      }
    ]));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-25
      • 1970-01-01
      • 2013-08-03
      • 2018-07-03
      相关资源
      最近更新 更多