简体   繁体   English

查找操作缓慢

[英]Lookup Operation Slow

I have the following query: 我有以下查询:

query = [{
    $match: {
      $and: [{
        feed: {
          $in: feeds
        }
      }, {
        updatedAt: {
          $gte: new Date(date)
        }
      }
    ]
    }
  },
  {
    $lookup: {
      from: "feed",
      localField: "feed",
      foreignField: "_id",
      as: "feed"
    }
  },
  {
    $unwind: {
      path: "$feed",
      preserveNullAndEmptyArrays: true
    }
  },
  {
    $lookup: {
      from: "source",
      localField: "feed.source",
      foreignField: "_id",
      as: "feed.source",
    }
  },
  {
    $sort: {
      updatedAt: -1
    }
  },
  {
    $limit: limit
  },
  {
    $skip: skip
  }
];

The query is taking too long, about 10 seconds to be specific. 该查询花费的时间太长,大约需要10秒。

I read https://docs.mongodb.com/manual/core/aggregation-pipeline-optimization/ but it is still rather slow. 我读了https://docs.mongodb.com/manual/core/aggregation-pipeline-optimization/,但速度仍然很慢。

I am looking to optimize as much as possible, is there a way? 我正在寻找尽可能优化的方法,有没有办法? The collection is indexed for $updatedAt: -1 该集合的索引为$ updatedAt:-1

Any ideas? 有任何想法吗?

If you want this to run fast I'd advise removing the $lookup pipeline stages and modeling your document based on its usage. 如果要快速运行,建议您删除$lookup管道阶段,并根据其用法对文档建模。

You could however add an index on the first match part of the aggregation query (updatedAt, feed). 但是,您可以在聚合查询的第一个匹配部分(updatedAt,提要)上添加索引。 But you'll still have the problem that you are still looking up from 2 other collections which wont use any indexes. 但是您仍然会遇到一个问题,即您仍在从另外两个不使用任何索引的集合中查找。

The $match and $sort pipeline operators can take advantage of an index when they occur at the beginning of the pipeline. 当$ match和$ sort管道运算符出现在管道的开头时,它们可以利用索引。

https://docs.mongodb.com/master/core/aggregation-pipeline/#pipeline-operators-and-indexes https://docs.mongodb.com/master/core/aggregation-pipeline/#pipeline-operators-and-indexes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM