mongodb - MongoDB查询性能优化
问题描述
我的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)
}
}
}
所以我有2个问题,
- 如何提高查询性能?
- 我看到索引
{"inventory.productId":1, _id:1}
并且{_id:1,"inventory.productId":1}
大小不同。它们之间有什么区别?
解决方案
推荐阅读
- c++ - 为什么 push_back 变成向量
> 导致段错误? - docker - 从 BYFN 上的 org1 删除 peer1 时出错:从 BYFN 更改的文件是否比第一个网络文件夹多?
- ios - 启动屏幕故事板未显示我的图像
- haskell - 错误:输入“where”时解析错误 | 4 | 其中 fyx = [x]:y | ^^^^^
- javascript - Fastify 插件 fastify-autoload 错误插件必须是一个函数
- javascript - Javascript给定一个带有html标签名称和字符串的数组返回没有dom的html
- c++17 - 在 C++ 中括号前的下划线是什么?
- android - 当您生成签名的 apk Firebase 数据库无法正常工作
- r - 获得方差-协方差矩阵的函数之间的差异(var和cova)
- azure - 获取 Get-AzSecurityAlert 所需日期的结果