How Mongo Query planner choose his index

喜你入骨 提交于 2020-03-05 07:47:28

问题


Mongo version : 3.4.3-10-g865d2fb

I have a request like this :

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")

The request during 16 seconds and explain returns these results :

155769 documents parse in index !!!

{
"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,
    ...
}
...
}

In my collection, I have a lot of indexes (65) and my collection contains 3 millions of documents.

Only two indexes interest me here :

aIndex : { "a" : 1 }

And

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

By default, mongo use { "a" : 1 } and the request takes 16 seconds. If I use hint(beIndex), the request takes 0,011 second and totalKeysExamined = 0 and totalDocsExamined = 0.

Why MongoDB don't use the beIndex that is more effective ?


回答1:


This behavior is a known bug. See SERVER-13732. This was fixed in MongoDB 3.6.

As a workaround, distributing the top-level filter on a into each $or clause should allow the query planner to make a better index choice.



来源:https://stackoverflow.com/questions/51955708/how-mongo-query-planner-choose-his-index

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!