什么是解释中的multiKeyPaths,以及缺少哪些导致索引使用不良的原因

时间:2017-10-30 11:34:41

标签: mongodb mongodb-query mongodb-indexes

我有一个包含4个碎片的大集合。

当我在索引数组字段“array.number”上运行查询时,如下所示:

var query = { "array" : { $elemMatch: { "number" : { $gte : "10", $lt : "20" } } } };

并检查explain,我将获得这些获胜计划(缩写为清晰):

碎片0/2/3:

"inputStage": {
  "stage": "IXSCAN",
  ...
  "isMultiKey": true,
  "indexBounds": {
    "array.number": [
      "[\"10\", {})"
    ]
  }
}

Shard1:

"inputStage": {
  "stage": "IXSCAN",
  ...
  "isMultiKey": true,
  "multiKeyPaths": {
    "array.number": [
      "array"
    ]
  },
  "indexBounds": {
    "array.number": [
      "[\"10\", \"20\")"
    ]
  }
}

因此shard1给出了索引的预期最佳使用,限制inputStage仅超过10-20,而其他分片仅使用索引的下限。分片对象之间的唯一区别是multiKeyPaths部分,它在分片0/2/3中缺失。

知道为什么会这样,以及我们如何才能使我们的其他分片正确使用我们的索引?

更新

以下是对以下查询的完整解释响应:

var query = { "array" : { $elemMatch: { "number" : { $gte : "10", $lt : "20" } } } };
db.collection.find(query).explain()

响应:

{
    "queryPlanner" : {
        "mongosPlannerVersion" : 1,
        "winningPlan" : {
            "stage" : "SHARD_MERGE",
            "shards" : [ 
                {
                    "shardName" : "company_rs0",
                    "connectionString" : "company_rs0/shard0-db0:27017,shard0-db1:27017",
                    "serverInfo" : {"host":"shard0-db0","port":27017,"version":"3.4.7","gitVersion":"cf38c1b8a0a8dca4a11737581beafef4fe120bcd"},
                    "plannerVersion" : 1,
                    "namespace" : "company_database.collection",
                    "indexFilterSet" : false,
                    "parsedQuery" : {"array":{"$elemMatch":{"$and":[{"number":{"$lt":"20"}},{"number":{"$gte":"10"}}]}}},
                    "winningPlan" : {
                        "stage" : "FETCH",
                        "filter" : {
                            "array" : {
                                "$elemMatch" : {"$and":[{"number":{"$gte":"10"}},{"number":{"$lt":"20"}}]}
                            }
                        },
                        "inputStage" : {
                            "stage" : "IXSCAN",
                            "numberPattern" : {"array.number":1.0},
                            "indexName" : "array.number_1",
                            "isMultiKey" : true,
                            "isUnique" : false,
                            "isSparse" : false,
                            "isPartial" : false,
                            "indexVersion" : 1,
                            "direction" : "forward",
                            "indexBounds" : {"array.number":["[\"10\", {})"]}
                        }
                    },
                    "rejectedPlans" : [ 
                        {
                            "stage" : "FETCH",
                            "filter" : {
                                "array" : {"$elemMatch":{"$and":[{"number":{"$lt":"20"}},{"number":{"$gte":"10"}}]}}
                            },
                            "inputStage" : {
                                "stage" : "IXSCAN",
                                "numberPattern" : {"array.number":1.0},
                                "indexName" : "array.number_1",
                                "isMultiKey" : true,
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 1,
                                "direction" : "forward",
                                "indexBounds" : {"array.number":["[\"\", \"20\")"]}
                            }
                        }
                    ]
                }, 
                {
                    "shardName" : "company_rs1",
                    "connectionString" : "company_rs1/shard1-db0:27017,shard1-db1:27017",
                    "serverInfo" : {"host":"shard1-db0","port":27017,"version":"3.4.7","gitVersion":"cf38c1b8a0a8dca4a11737581beafef4fe120bcd"},
                    "plannerVersion" : 1,
                    "namespace" : "company_database.collection",
                    "indexFilterSet" : false,
                    "parsedQuery" : {
                        "array" : {"$elemMatch":{"$and":[{"number":{"$lt":"20"}},{"number":{"$gte":"10"}}]}}
                    },
                    "winningPlan" : {
                        "stage" : "FETCH",
                        "filter" : {
                            "array" : {"$elemMatch":{"$and":[{"number":{"$lt":"20"}},{"number":{"$gte":"10"}}]}}
                        },
                        "inputStage" : {
                            "stage" : "IXSCAN",
                            "numberPattern" : {"array.number":1.0},
                            "indexName" : "array.number_1",
                            "isMultiKey" : true,
                            "multiKeyPaths" : {"array.number":["array"]},
                            "isUnique" : false,
                            "isSparse" : false,
                            "isPartial" : false,
                            "indexVersion" : 1,
                            "direction" : "forward",
                            "indexBounds" : {"array.number":["[\"10\", \"20\")"]}
                        }
                    },
                    "rejectedPlans" : []
                }, 
                {
                    "shardName" : "company_rs2",
                    "connectionString" : "company_rs2/shard2-db0:27017,shard2-db1:27017",
                    "serverInfo" : {"host":"shard2-db0","port":27017,"version":"3.4.7","gitVersion":"cf38c1b8a0a8dca4a11737581beafef4fe120bcd"},
                    "plannerVersion" : 1,
                    "namespace" : "company_database.collection",
                    "indexFilterSet" : false,
                    "parsedQuery" : {
                        "array" : {"$elemMatch":{"$and":[{"number":{"$lt":"20"}},{"number":{"$gte":"10"}}]}}
                    },
                    "winningPlan" : {
                        "stage" : "FETCH",
                        "filter" : {
                            "array" : {"$elemMatch":{"$and":[{"number":{"$gte":"10"}},{"number":{"$lt":"20"}}]}}
                        },
                        "inputStage" : {
                            "stage" : "IXSCAN",
                            "numberPattern" : {"array.number":1.0},
                            "indexName" : "array.number_1",
                            "isMultiKey" : true,
                            "isUnique" : false,
                            "isSparse" : false,
                            "isPartial" : false,
                            "indexVersion" : 1,
                            "direction" : "forward",
                            "indexBounds" : {"array.number":["[\"10\", {})"]}
                        }
                    },
                    "rejectedPlans" : [ 
                        {
                            "stage" : "FETCH",
                            "filter" : {
                                "array" : {"$elemMatch":{"$and":[{"number":{"$lt":"20"}},{"number":{"$gte":"10"}}]}}
                            },
                            "inputStage" : {
                                "stage" : "IXSCAN",
                                "numberPattern" : {"array.number":1.0},
                                "indexName" : "array.number_1",
                                "isMultiKey" : true,
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 1,
                                "direction" : "forward",
                                "indexBounds" : {"array.number":["[\"\", \"20\")"]}
                            }
                        }
                    ]
                }, 
                {
                    "shardName" : "company_rs3",
                    "connectionString" : "company_rs3/shard3-db0:27017,shard3-db1:27017",
                    "serverInfo" : {"host":"shard3-db0","port":27017,"version":"3.4.7","gitVersion":"cf38c1b8a0a8dca4a11737581beafef4fe120bcd"},
                    "plannerVersion" : 1,
                    "namespace" : "company_database.collection",
                    "indexFilterSet" : false,
                    "parsedQuery" : {
                        "array" : {"$elemMatch":{"$and":[{"number":{"$lt":"20"}},{"number":{"$gte":"10"}}]}}
                    },
                    "winningPlan" : {
                        "stage" : "FETCH",
                        "filter" : {
                            "array" : {"$elemMatch":{"$and":[{"number":{"$gte":"10"}},{"number":{"$lt":"20"}}]}}
                        },
                        "inputStage" : {
                            "stage" : "IXSCAN",
                            "numberPattern" : {"array.number":1.0},
                            "indexName" : "array.number_1",
                            "isMultiKey" : true,
                            "isUnique" : false,
                            "isSparse" : false,
                            "isPartial" : false,
                            "indexVersion" : 1,
                            "direction" : "forward",
                            "indexBounds" : {"array.number":["[\"10\", {})"]}
                        }
                    },
                    "rejectedPlans" : [ 
                        {
                            "stage" : "FETCH",
                            "filter" : {
                                "array" : {"$elemMatch":{"$and":[{"number":{"$lt":"20"}},{"number":{"$gte":"10"}}]}}
                            },
                            "inputStage" : {
                                "stage" : "IXSCAN",
                                "numberPattern" : {"array.number":1.0},
                                "indexName" : "array.number_1",
                                "isMultiKey" : true,
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 1,
                                "direction" : "forward",
                                "indexBounds" : {"array.number":["[\"\", \"20\")"]}
                            }
                        }
                    ]
                }
            ]
        }
    },
    "ok" : 1.0
}

0 个答案:

没有答案