【发布时间】:2020-07-05 00:03:24
【问题描述】:
我的store 集合中有以下文档结构,
{
"_id": "some_custom_id",
"inventory": [
{
"productId": "some_prod_id",
// ...restAttributes
},
// 500+ such items
]
}
我正在尝试查询coll.find({_id:"some_id","inventory.productId":"some_prod_id"},{...})
查询有时需要很长时间才能返回(10 秒左右)。所以我创建了一个索引 {_id:1,"inventory.productId":1} 但仍然没有性能提升,所以我尝试了 mongo query explain 并发现使用了 _id 索引而不是我创建的索引。然后我创建了另一个索引{"inventory.productId":1, _id:1}仍然没有运气。
这是coll.find({_id:"some_id","inventory.productId":"some_prod_id"}).explain("executionStats")的输出
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "somedb.Stores",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"_id" : {
"$eq" : "114"
}
},
{
"inventory.productId" : {
"$eq" : "41529689"
}
}
]
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"inventory.productId" : {
"$eq" : "41529689"
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"_id" : 1
},
"indexName" : "_id_",
"isMultiKey" : false,
"multiKeyPaths" : {
"_id" : []
},
"isUnique" : true,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"_id" : [
"[\"114\", \"114\"]"
]
}
}
},
"rejectedPlans" : []
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 0,
"totalKeysExamined" : 1,
"totalDocsExamined" : 1,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"inventory.productId" : {
"$eq" : "41529689"
}
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 2,
"advanced" : 1,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 1,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 2,
"advanced" : 1,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"_id" : 1
},
"indexName" : "_id_",
"isMultiKey" : false,
"multiKeyPaths" : {
"_id" : []
},
"isUnique" : true,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"_id" : [
"[\"114\", \"114\"]"
]
},
"keysExamined" : 1,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
"serverInfo" : {
"host" : "somecluster-shard-00-02-1jury.gcp.mongodb.net",
"port" : 27017,
"version" : "4.0.16",
"gitVersion" : "2a5433168a53044cb6b4fa8083e4cfd7ba142221"
},
"ok" : 1.0,
"operationTime" : Timestamp(1585112231, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1585112231, 1),
"signature" : {
"hash" : { "$binary" : "joFIiOgu32NHAVrAO40lHKl7/i8=", "$type" : "00" },
"keyId" : NumberLong(6778940624956555265)
}
}
}
所以我有两个问题,
- 如何提高查询性能?
- 我看到索引
{"inventory.productId":1, _id:1}和{_id:1,"inventory.productId":1}的大小不同。它们有什么区别?
【问题讨论】:
-
您使用的 MongoDB 版本是什么?另外,请发布使用
explain("executionStats").生成的查询计划。 -
@prasad_ 我已经用查询计划更新了问题,我正在使用
4.0.16mongoldb 版本。 -
您的查询计划说它正在使用
_id字段上的默认索引(“indexName”:“id”)。此外,不使用其他索引。 “executionStats”:......有“executionTimeMillis”:0。那么,有什么问题?告诉集合中有多少文档? -
问题是,正如问题中提到的,有时查询需要很长时间才能响应,“executionTimeMillis”很低,因为我执行查询时没有发生并发操作,即使执行时间也会增加mongo 集群有 10 个并发操作。我有大约 500 条记录,每条记录在库存数组中有 500 个项目。我正在使用 m30 mongo atlas 集群,我在服务器端有足够的资源。 @prasad_
-
然后,您必须进一步调查这些并发操作及其资源使用情况。它与此集合或查询无关(我认为,根据查询计划输出)。
标签: mongodb indexing query-optimization