Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb Mongo查询规划器如何选择索引_Mongodb_Indexing - Fatal编程技术网

Mongodb Mongo查询规划器如何选择索引

Mongodb Mongo查询规划器如何选择索引,mongodb,indexing,Mongodb,Indexing,Mongo版本:3.4.3-10-g865d2fb 我有这样的要求: db.getCollection('c_zop_operations').find({ "a":{ "$in":[ "O", "S", "P" ] }, "$or":[ { "b":"008091", "c":"1187",

Mongo版本:3.4.3-10-g865d2fb

我有这样的要求:

db.getCollection('c_zop_operations').find({

     "a":{
        "$in":[
           "O",
           "S",
           "P"
        ]
     },
     "$or":[
        {
           "b":"008091",
           "c":"1187",
           "d":"F",
           "e":ISODate("2018-07-22T22:00:00.000Z")
        },
... x 39 elements in $or statement
     ]
}).explain("executionStats")
请求在16秒内完成并解释返回以下结果:

155769文档在索引中解析

{
"queryPlanner" : {
    "plannerVersion" : 1,
    ...
    "indexFilterSet" : false,
    "parsedQuery" : {
      ...
    },
    "winningPlan" : {
        "stage" : "FETCH",
        "filter" : {
            "$or" : [ 
                {
                    "$and" : [ 
                        {
                            "c" : {
                                "$eq" : "1187"
                            }
                        }, 
                        {
                            "d" : {
                                "$eq" : "F"
                            }
                        }, 
                        {
                            "b" : {
                                "$eq" : "008091"
                            }
                        }, 
                        {
                            "e" : {
                                "$eq" : ISODate("2018-07-22T22:00:00.000Z")
                            }
                        }
                    ]
                }, 
                x 39 times
                ...
        },
        "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                "a" : 1
            },
            "indexName" : "a",
            "isMultiKey" : false,
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "forward",
            "indexBounds" : {
                "a" : [ 
                    "[\"O\", \"O\"]", 
                    "[\"P\", \"P\"]", 
                    "[\"S\", \"S\"]"
                ]
            }
        }
    },
    "rejectedPlans" : [ 
    ...
    ]
},
"executionStats" : {
    "executionSuccess" : true,
    "nReturned" : 0,
    "executionTimeMillis" : 16010,
    "totalKeysExamined" : 155769,
    "totalDocsExamined" : 155769,
    ...
}
...
}
在我的收藏中,我有很多索引(65),我的收藏包含300万个文档

这里只有两个索引让我感兴趣:

aIndex : { "a" : 1 }

默认情况下,mongo使用{“a”:1},请求耗时16秒。 如果我使用提示(beIndex),请求将花费0011秒,TotalKeysInspected=0,totalDocsExamined=0


为什么MongoDB不使用更有效的beIndex?

这种行为是已知的错误。看见这在MongoDB 3.6中得到了修复

作为一种解决方法,将
a
上的顶级过滤器分配到每个$or子句中应该允许查询计划器做出更好的索引选择

beIndex: { "b" : 1,  "e" : 1 }