带有查找和计数的 MongoDB 聚合很慢

时间:2021-07-27 17:11:35

标签: mongodb aggregation-framework

我有两个条件 - 一个用于外部表,另一个用于主表。

示例查询是

db.getCollection('ResourceOrder').aggregate(
    [
    {"$match" : 
            { "$and" : [
                { "Resource.Target.Reference" : { "$eq" : "Organization/93a2f92d-6cc4-41e5-bfef-e9429e70b07b" } },
                { "LastUpdated" : { "$gte" : ISODate("2020-03-06T13:20:33Z") } }] 
            } 
    },
    { "$lookup" : { 
        "from" : "ResourceDiagnosticOrder",
        "localField" : "Resource.Detail.Reference",
        "foreignField" : "Reference",
        "as" : "detail_Lookup" } 
    },
    { "$match" : { "$or" : [{ "detail_Lookup.Resource.Status" : { "$eq" : 3 } }, { "detail_Lookup.Resource.Status" : { "$eq" : 4 } }] } },
    { "$count" : "count" }
    ]
    )

外部表是ResourceDiagnosticOrder,外部字段有索引,称为Resource

左表中的Resource.Target是数组,如果重要

请指教,我该如何改进?搜索本身需要可接受的时间,但计数需要 10+ 秒

真正让我烦恼的是,当我根据正确的条件(即数万个 id)从外部表中选择所有 id 并将它们提供给 $in 然后计算工作速度时 有没有办法做一个旧的好的内部连接?

0 个答案:

没有答案
相关问题