Mongodb 数据库查询性能优化
我的Mongodb 数据库查询性能优化,mongodb,indexing,query-optimization,Mongodb,Indexing,Query Optimization,我的商店收藏中有以下文档结构 { "_id": "some_custom_id", "inventory": [ { "productId": "some_prod_id", // ...restAttributes }, // 500+ such items ] } 我正在尝试进行查询coll.find({u id:“some\u id”,“inventory.productId:“some\u prod\u id”},{…}) 查
商店
收藏中有以下文档结构
{
"_id": "some_custom_id",
"inventory": [
{
"productId": "some_prod_id",
// ...restAttributes
},
// 500+ such items
]
}
我正在尝试进行查询coll.find({u id:“some\u id”,“inventory.productId:“some\u prod\u id”},{…})
查询有时需要很长时间才能返回(10秒左右)。所以我创建了一个索引{u id:1,“inventory.productId”:1}
,但仍然没有性能提升,所以我尝试了mongo,发现使用了\u id
索引,而不是我创建的索引。然后我创建了另一个索引{“inventory.productId”:1,_id:1}
仍然不走运
这是coll.find({u id:“some\u id”,“inventory.productId:“some\u prod\u id”})的输出。解释(“executionStats”)
所以我有两个问题
{“inventory.productId:1,{u id:1}
和{u id:1,{inventory.productId:1}
的大小不同。他们之间有什么区别您使用的MongoDB版本是什么?另外,请发布使用
解释(“executionStats”)生成的查询计划。
@prasad\uz。我已经用查询计划更新了问题,我使用的是4.0.16
mongoldb版本。您的查询计划说它使用的是\u id
字段(“indexName”:“id”)上的默认索引,。此外,不使用其他索引。“executionStats”:。。。具有“executionTimeMillis”:0。那么,问题是什么?告诉集合中有多少文档?问题是,正如问题中提到的,有时查询需要花费很多时间来响应,“executionTimeMillis”很低,因为在执行查询时没有发生并发操作,即使mongo集群有10个并发操作,执行时间也会增加。我有大约500条记录,每个记录在库存阵列中有500个项目。我使用的是m30 mongo atlas集群,我在服务器端有足够的资源@那么,您必须进一步调查这些并发操作及其资源使用情况。它与此集合或查询无关(我认为,根据查询计划输出),我在查询中使用了hint
,现在正在使用新索引,但大多数情况下使用\u id
索引的查询的性能较低。此外,由于queryPlanner的rejectedPlans为空(附在问题后面),因此没有提示的查询似乎没有在索引之间运行竞赛。
{
"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)
}
}
}