整个嵌入式文档的索引

帕尔文

我在文件上有以下结构

{
  "_id": ObjectId("1234"),
  "name": "TestName",
  "location": { state: "NY", city: "New York" ,"geoid":28042},
  "ts":<timestamp>
..
..
}.. 

以上集合中大约有1 GB的数据。

我在位置上创建了一个索引,即db.mydata.createIndex( { location: 1 } )我有以下三个问题。

  1. 如果在整个文档上创建索引会对性能产生什么影响?
  2. 由于索引是on "location",如果使用"location.state"="NY",则此查询是否使用上面定义的索引?
  3. 我的查询为"location.city"="New York"时是否使用索引"location.state"="NY"这里的订单重要吗?
凯文·史密斯

如果在子文档上创建索引,则location与创建任何其他索引具有相同的影响,因为它将需要进行完整的集合扫描并建立索引。

索引为时location,您将无法进行查询,location.state因为这与整个子文档不匹配。仅当执行以下查询以匹配子文档时,才可以使用索引:

db.test.find({ "location": { state: "NY", city: "New York" ,"geoid":28042} }

这是因为该指数是在球场上location不上location.state,如果你需要匹配statecity我建议您在他们两个字段添加一个索引。

db.test.createIndex({"location.state" : 1, "location.city": 1});

这样,您的查询可以将索引state用于state andcity和。

如果将其放到一个简单的示例中,我们都可以运行,那么我们可以查看执行计划。

如果我们插入带有子文档的文档。

db.test.insertOne({
  subDocument: { field1 : 1, field2 : 2 }
});

现在我们可以在上创建索引subDocument

db.test.createIndex({subDocument:1})

然后,我们field1仅对查询运行一个解释

db.test.find( { "subDocument.field1" : 1 } ).explain()
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "test.test",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "subDocument.field1" : {
                                "$eq" : 1
                        }
                },
                "queryHash" : "CEC76D6B",
                "planCacheKey" : "CEC76D6B",
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "subDocument.field1" : {
                                        "$eq" : 1
                                }
                        },
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
        },
        "serverInfo" : { },
        "ok" : 1
}

如您所见,获胜计划是COLLSCAN

现在,让我们根据整个文档进行查询。

db.test.find({"subDocument": {field1: 1, field2: 2 }}).explain()
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "test.test",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "subDocument" : {
                                "$eq" : {
                                        "field1" : 1,
                                        "field2" : 2
                                }
                        }
                },
                "queryHash" : "3B0F9692",
                "planCacheKey" : "7CF9BAC4",
                "winningPlan" : {
                        "stage" : "FETCH",
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                        "subDocument" : 1
                                },
                                "indexName" : "subDocument_1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "subDocument" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "subDocument" : [
                                                "[{ field1: 1.0, field2: 2.0 }, { field1: 1.0, field2: 2.0 }]"
                                        ]
                                }
                        }
                },
                "rejectedPlans" : [ ]
        },
        "serverInfo" : { },
        "ok" : 1
}

现在我们可以使用索引扫描看到它IXSCAN

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章