最佳索引以优化此MongoDB聚合管道查询是什么?

huangapple go评论69阅读模式
英文:

What is best index to optimize this mongodb aggregate pipeline Query?

问题

尝试了几个索引,但它会扫描集合中的所有文档。我只需要保持扫描的文档/返回文档的比例在1000以下(这将有助于减少Atlas警报,提醒比率已超过1000)。另外,我需要知道在聚合查询中使用hint是否是一个好选择。

"pipeline": [
  {
    "$match": {
      "status": {
        "$in": [
          "STATUS"
        ]
      },
      "$and": [
        {
          "pId": {
            "$nin": [
              null,
              null
            ]
          }
        },
        {
          "bId": {
            "$nin": [
              null,
              null
            ]
          }
        },
        {
          "dueDate": {
            "$nin": [
              null,
              null
            ]
          }
        },
        {
          "proposedPaymentDate": {
            "$nin": [
              null,
              null
            ]
          }
        },
        {
          "amount": {
            "$gt": 0
          }
        }
      ],
      "pInvoiceId": {
        "$exists": false
      }
    }
  },
  {
    "$sort": {
      "pId": 1,
      "amount": -1
    }
  },
  {
    "$lookup": {
      "from": "pcollection",
      "localField": "pId",
      "foreignField": "lId",
      "as": "p"
    }
  },
  {
    "$unwind": {
      "path": "$p"
    }
  },
  {
    "$match": {
      "p.aId": {
        "$in": [
          {
            "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx"
          },
          {
            "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx"
          },
          {
            "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx"
          },
          {
            "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx"
          },
          {
            "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx"
          },
          {
            "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx"
          }
        ]
      }
    }
  },
  {
    "$skip": 400
  },
  {
    "$limit": 400
  }
]
英文:

Tried few indexes but it goes scans all the documents in collection. I just need to keep the scanned docs / return docs ratio under 1000. (this will helps to reduce the atlas alerts mentioning the ratio has exceed the 1000). Also I need to know usage of hint in aggregate query is a good option or not.

"pipeline": [
      {
        "$match": {
          "status": {
            "$in": [
              "STATUS"
            ]
          },
          "$and": [
            {
              "pId": {
                "$nin": [
                  null,
                  null
                ]
              }
            },
            {
              "bId": {
                "$nin": [
                  null,
                  null
                ]
              }
            },
            {
              "dueDate": {
                "$nin": [
                  null,
                  null
                ]
              }
            },
            {
              "proposedPaymentDate": {
                "$nin": [
                  null,
                  null
                ]
              }
            },
            {
              "amount": {
                "$gt": 0
              }
            }
          ],
          "pInvoiceId": {
            "$exists": false
          }
        }
      },
      {
        "$sort": {
          "pId": 1,
          "amount": -1
        }
      },
      {
        "$lookup": {
          "from": "pcollection",
          "localField": "pId",
          "foreignField": "lId",
          "as": "p"
        }
      },
      {
        "$unwind": {
          "path": "$p"
        }
      },
      {
        "$match": {
          "p.aId": {
            "$in": [
              {
                "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx"
              },
              {
                "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx"
              },
              {
                "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx"
              },
              {
                "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx"
              },
              {
                "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx"
              },
              {
                "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx"
              }
            ]
          }
        }
      },
      {
        "$skip": 400
      },
      {
        "$limit": 400
      }
    ],

答案1

得分: 2

您的条件并不太有意义,您可以尝试以下条件:

[
   {
      "$match": {
         "status": "STATUS",
         "amount": { "$gt": 0 },
         "pInvoiceId": { "$exists": false }
      }
   },
   { "$sort": { "pId": 1, "amount": -1 } },
   {
      "$lookup": {
         "from": "pcollection",
         "localField": "pId",
         "foreignField": "lId",
         "pipeline": [
         {
            "$match": {
               "p.aId": {
                  "$in": [
                     { "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx" },
                     { "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx" },
                     { "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx" },
                     { "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx" },
                     { "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx" },
                     { "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx" }
                  ]
               }
            }
         }
         ],
         "as": "p"
      }
   },
   { "$unwind": { "path": "$p" } },
   { "$skip": 400 },
   { "$limit": 400 }
]

然后最佳索引应该是 { status: 1, pInvoiceId: 1, amount: -1, pId: 1 }

英文:

Your condition does not make much sense, you can try this one:

[
   {
      "$match": {
         "status": "STATUS",
         "amount": { "$gt": 0 },
         "pInvoiceId": { "$exists": false }
      }
   },
   { "$sort": { "pId": 1, "amount": -1 } },
   {
      "$lookup": {
         "from": "pcollection",
         "localField": "pId",
         "foreignField": "lId",
         pipeline: [
         {
            "$match": {
               "p.aId": {
                  "$in": [
                     { "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx" },
                     { "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx" },
                     { "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx" },
                     { "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx" },
                     { "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx" },
                     { "$oid": "xxxxxxxxxxxxxxxxxxxxxxxx" }
                  ]
               }
            }
         }
         ],
         "as": "p"
      }
   },
   { "$unwind": { "path": "$p" } },
   { "$skip": 400 },
   { "$limit": 400 }
]

Then the best index should be { status: 1, pInvoiceId: 1, amount: -1, pId: 1 }

huangapple
  • 本文由 发表于 2023年3月12日 17:08:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定