简体   繁体   English

mongodb-使用COLLSCAN而不是索引的$ lookup管道

[英]mongodb - $lookup pipeline using COLLSCAN instead of index

I'm trying to use indexing on my $lookup pipeline but it doesn't seem to be working as intended. 我正在尝试在$lookup管道上使用索引,但是它似乎没有按预期工作。

Here's my query: 这是我的查询:

db.map_levels.explain().aggregate([
    {
        $lookup:
        {
            from: 
                "map_level_revisions",
            pipeline:
            [
                {
                    $match:
                    {
                        $expr:
                        {
                            $eq:
                            [
                                "$account_id",
                                ObjectId("5b66ca21d6b54f479bef62a4")
                            ]
                        }
                    }
                }
            ],
            as:
                "revisions"
        }
    },
])

and here's the explanation: 这是解释:

{
    "stages" : [
        {
            "$cursor" : {
                "query" : {

                },
                "queryPlanner" : {
                    "plannerVersion" : 1,
                    "namespace" : "test-creator.map_levels",
                    "indexFilterSet" : false,
                    "parsedQuery" : {

                    },
                    "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "direction" : "forward"
                    },
                    "rejectedPlans" : [ ]
                }
            }
        },
        {
            "$lookup" : {
                "from" : "map_level_revisions",
                "as" : "revisions",
                "let" : {

                },
                "pipeline" : [
                    {
                        "$match" : {
                            "$expr" : {
                                "$eq" : [
                                    "$account_id",
                                    ObjectId("5b66ca21d6b54f479bef62a4")
                                ]
                            }
                        }
                    }
                ]
            }
        }
    ],
    "ok" : 1
}

How do I make it use an index instead? 如何使用索引代替它?

Just a note, the query does return the document. 仅需注意,查询确实会返回文档。

The collection scan in your explain output is referring to the map_levels collection, as noted in the queryPlanner.namespace value. 解释输出中的集合扫描引用了map_levels集合,如queryPlanner.namespace值中所述。 The $lookup stage merges data from another collection into the current pipeline. $lookup阶段将来自另一个集合的数据合并到当前管道中。 Since you haven't specified any query stages before the $lookup , the map_levels collection will be iterated using a collection scan. 由于您没有在$lookup之前指定任何查询阶段,因此将使用集合扫描来迭代map_levels集合。 If an entire collection is being loaded without any filtering or sort criteria, a collection scan has less overhead than iterating an index and fetching the documents. 如果要加载整个集合而没有任何过滤或排序标准,则集合扫描的开销要小于迭代索引和获取文档的开销。

You can avoid the current collection scan by adding a $match stage before your $lookup (assuming you don't want to process the full map_levels collection). 您可以通过在$lookup之前添加$match阶段来避免当前的集合扫描(假设您不想处理完整的map_levels集合)。

How can I check the index used by $lookup ? 如何检查$lookup使用的索引?

Unfortunately query explain output does not (as at MongoDB 4.0) indicate index usage for $lookup stages. 不幸的是,查询说明输出不会(在MongoDB 4.0中)指示$lookup阶段的索引使用情况。 A workaround for this would be running explain using your lookup's pipeline as a top level aggregation query. 为此,将运行一种解决方法,将您的查找pipeline用作顶级聚合查询。

There's a relevant issue to watch/upvote in the MongoDB Issue tracker: SERVER-22622: Improve $lookup explain to indicate query plan on the "from" collection . MongoDB问题跟踪器中有一个相关的问题需要关注/更新: SERVER-22622:改进$ lookup说明以指示“ from”集合上的查询计划

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

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