首页 > 解决方案 > MongoDB 索引未与聚合查询一起使用

问题描述

语境

=> 检查编辑!

我正在运行一些查询,其中应用了排序并且应该利用索引。对于某些索引,对于给定环境中的某些查询(聚合),查询会绕过索引,我还无法弄清楚原因。

设置

我有一个包含 3 个索引的 mongo 集合:

我正在使用 2 个查询来测试每个索引的使用情况:

B. 查找和排序查询

db.getCollection('properties_po').find().sort({"definition.financial.profitability.highlights.prof_net" : 1.0 }).limit(1);

db.getCollection('properties_po').find().sort({"definition.name" : 1.0}).limit(1);

B. 用排序查询聚合

db.getCollection("properties_po").aggregate([{"$sort":{"definition.name":1.0}},{"$limit":1}])

db.getCollection("properties_po").aggregate([{"$sort":{"definition.financial.profitability.highlights.prof_net":1.0}},{"$limit":1}])

db.properties_po.aggregate( [ { $indexStats: { } } ] )用来检查每次查询后索引使用量是否增加。

结果

环境 1(本地 mongodb 3.4):

两个索引上的两个查询都按预期工作,每次查询后都会增加每个索引的使用量。

环境 2(产品 monhodb 3.6):

查询 A 按预期工作,每次查询后和每个索引都会增加索引使用量。

查询 B 适用于索引“definition.name”,其中索引使用量正确增加。

查询 B不适用于未使用索引(非常慢的查询)的索引“definition.financial.profitability.highlights.prof_net”,因为它反映了索引使用量未增加。

我不确定在哪里看,可能是我缺少一些 mongodb 配置,一些嵌套文档属性限制,或者我的索引可能没有正确创建。

谢谢你的帮助


编辑 18/03 它与 mongodb 版本无关,因为我刚刚测试了在产品服务器上创建相同的集合,并且在那里我也有 2 个不同的结果。

集合 1: 15026 条记录 带有说明的聚合查询的输出:

db.getCollection("properties").explain("executionStats").aggregate([{"$sort":{"definition.financial.profitability.highlights.prof_net":1.0}},{"$limit":1}])

-

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

            }, 
            "queryPlanner" : {
                "plannerVersion" : 1.0, 
                "namespace" : "collec.properties", 
                "indexFilterSet" : false, 
                "parsedQuery" : {

                }, 
                "winningPlan" : {
                    "stage" : "COLLSCAN", 
                    "direction" : "forward"
                }, 
                "rejectedPlans" : [

                ]
            }, 
            "executionStats" : {
                "executionSuccess" : true, 
                "nReturned" : 15026.0, 
                "executionTimeMillis" : 1522.0, 
                "totalKeysExamined" : 0.0, 
                "totalDocsExamined" : 15026.0, 
                "executionStages" : {
                    "stage" : "COLLSCAN", 
                    "nReturned" : 15026.0, 
                    "executionTimeMillisEstimate" : 1506.0, 
                    "works" : 15028.0, 
                    "advanced" : 15026.0, 
                    "needTime" : 1.0, 
                    "needYield" : 0.0, 
                    "saveState" : 128.0, 
                    "restoreState" : 128.0, 
                    "isEOF" : 1.0, 
                    "invalidates" : 0.0, 
                    "direction" : "forward", 
                    "docsExamined" : 15026.0
                }
            }
        }
    }, 
    {
        "$sort" : {
            "sortKey" : {
                "definition.financial.profitability.highlights.prof_net" : 1.0
            }, 
            "limit" : NumberLong(1)
        }
    }
], 
"ok" : 1.0

}

集合 2: 2 条记录

相同的查询输出:

db.getCollection("properties_pl").explain("executionStats").aggregate([{"$sort":{"definition.financial.profitability.highlights.prof_net":1.0}},{"$limit":1}])

-

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

            }, 
            "sort" : {
                "definition.financial.profitability.highlights.prof_net" : 1.0
            }, 
            "limit" : NumberLong(1), 
            "queryPlanner" : {
                "plannerVersion" : 1.0, 
                "namespace" : "patrimmoine.properties_pl", 
                "indexFilterSet" : false, 
                "parsedQuery" : {

                }, 
                "winningPlan" : {
                    "stage" : "FETCH", 
                    "inputStage" : {
                        "stage" : "IXSCAN", 
                        "keyPattern" : {
                            "definition.financial.profitability.highlights.prof_net" : 1.0
                        }, 
                        "indexName" : "definition.financial.profitability.highlights.prof_net_1", 
                        "isMultiKey" : false, 
                        "multiKeyPaths" : {
                            "definition.financial.profitability.highlights.prof_net" : [

                            ]
                        }, 
                        "isUnique" : false, 
                        "isSparse" : false, 
                        "isPartial" : false, 
                        "indexVersion" : 2.0, 
                        "direction" : "forward", 
                        "indexBounds" : {
                            "definition.financial.profitability.highlights.prof_net" : [
                                "[MinKey, MaxKey]"
                            ]
                        }
                    }
                }, 
                "rejectedPlans" : [

                ]
            }, 
            "executionStats" : {
                "executionSuccess" : true, 
                "nReturned" : 2.0, 
                "executionTimeMillis" : 0.0, 
                "totalKeysExamined" : 2.0, 
                "totalDocsExamined" : 2.0, 
                "executionStages" : {
                    "stage" : "FETCH", 
                    "nReturned" : 2.0, 
                    "executionTimeMillisEstimate" : 0.0, 
                    "works" : 3.0, 
                    "advanced" : 2.0, 
                    "needTime" : 0.0, 
                    "needYield" : 0.0, 
                    "saveState" : 1.0, 
                    "restoreState" : 1.0, 
                    "isEOF" : 1.0, 
                    "invalidates" : 0.0, 
                    "docsExamined" : 2.0, 
                    "alreadyHasObj" : 0.0, 
                    "inputStage" : {
                        "stage" : "IXSCAN", 
                        "nReturned" : 2.0, 
                        "executionTimeMillisEstimate" : 0.0, 
                        "works" : 3.0, 
                        "advanced" : 2.0, 
                        "needTime" : 0.0, 
                        "needYield" : 0.0, 
                        "saveState" : 1.0, 
                        "restoreState" : 1.0, 
                        "isEOF" : 1.0, 
                        "invalidates" : 0.0, 
                        "keyPattern" : {
                            "definition.financial.profitability.highlights.prof_net" : 1.0
                        }, 
                        "indexName" : "definition.financial.profitability.highlights.prof_net_1", 
                        "isMultiKey" : false, 
                        "multiKeyPaths" : {
                            "definition.financial.profitability.highlights.prof_net" : [

                            ]
                        }, 
                        "isUnique" : false, 
                        "isSparse" : false, 
                        "isPartial" : false, 
                        "indexVersion" : 2.0, 
                        "direction" : "forward", 
                        "indexBounds" : {
                            "definition.financial.profitability.highlights.prof_net" : [
                                "[MinKey, MaxKey]"
                            ]
                        }, 
                        "keysExamined" : 2.0, 
                        "seeks" : 1.0, 
                        "dupsTested" : 0.0, 
                        "dupsDropped" : 0.0, 
                        "seenInvalidated" : 0.0
                    }
                }
            }
        }
    }
], 
"ok" : 1.0

}

因此,一个查询确实没有使用任何索引并执行“COLLSCAN”,而另一个查询正在使用“IXSCAN”执行“FETCH”。

所以我现在的查询是,集合的内容如何改变查询的行为?

标签: mongodb

解决方案


推荐阅读