我刚发现我们的一个查询很慢。我们在数组中使用多键索引,但不确定如何才能真正改进此查询。
我们的数据结构:
account: [{
accountId:string,
service:string
}]
索引键是:
{"account.accountId":1, "account.service":1}
这是我的疑问:
db.model.find({"account.accountId":"12345", "account.service":"GameCenter"}).explain();
executionStats(100万条记录)
"executionStats" : {
"nReturned" : 1,
"executionTimeMillis" : 325,
"totalKeysExamined" : 122744,
"totalDocsExamined" : 122743,
"executionStages" : {
"stage" : "SHARD_MERGE",
"nReturned" : 1,
"executionTimeMillis" : 325,
"totalKeysExamined" : 122744,
"totalDocsExamined" : 122743,
"totalChildMillis" : NumberLong(834),
"shards" : [
{
"shardName" : "rs1",
"executionSuccess" : true,
"executionStages" : {
"stage" : "SHARDING_FILTER",
"nReturned" : 0,
"executionTimeMillisEstimate" : 300,
"works" : 40999,
"advanced" : 0,
"needTime" : 40998,
"needYield" : 0,
"saveState" : 320,
"restoreState" : 320,
"isEOF" : 1,
"invalidates" : 0,
"chunkSkips" : 0,
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"account.accountId" : {
"$eq" : "G:8183971619"
}
},
"nReturned" : 0,
"executionTimeMillisEstimate" : 300,
"works" : 40999,
"advanced" : 0,
"needTime" : 40998,
"needYield" : 0,
"saveState" : 320,
"restoreState" : 320,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 40998,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 40998,
"executionTimeMillisEstimate" : 30,
"works" : 40999,
"advanced" : 40998,
"needTime" : 0,
"needYield" : 0,
"saveState" : 320,
"restoreState" : 320,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"account.service" : 1,
"account.accountId" : 1
},
"indexName" : "account.service_1_account.accountId_1",
"isMultiKey" : true,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"account.service" : [
"[\"GameCenter\", \"GameCenter\"]"
],
"account.accountId" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : 40998,
"dupsTested" : 40998,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}
},
{
"shardName" : "rs2",
"executionSuccess" : true,
"executionStages" : {
"stage" : "SHARDING_FILTER",
"nReturned" : 1,
"executionTimeMillisEstimate" : 240,
"works" : 40612,
"advanced" : 1,
"needTime" : 40610,
"needYield" : 0,
"saveState" : 317,
"restoreState" : 317,
"isEOF" : 1,
"invalidates" : 0,
"chunkSkips" : 0,
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"account.accountId" : {
"$eq" : "G:8183971619"
}
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 230,
"works" : 40612,
"advanced" : 1,
"needTime" : 40610,
"needYield" : 0,
"saveState" : 317,
"restoreState" : 317,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 40610,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 40611,
"executionTimeMillisEstimate" : 70,
"works" : 40612,
"advanced" : 40611,
"needTime" : 0,
"needYield" : 0,
"saveState" : 317,
"restoreState" : 317,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"account.service" : 1,
"account.accountId" : 1
},
"indexName" : "account.service_1_account.accountId_1",
"isMultiKey" : true,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"account.service" : [
"[\"GameCenter\", \"GameCenter\"]"
],
"account.accountId" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : 40611,
"dupsTested" : 40611,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}
},
{
"shardName" : "rs3",
"executionSuccess" : true,
"executionStages" : {
"stage" : "SHARDING_FILTER",
"nReturned" : 0,
"executionTimeMillisEstimate" : 270,
"works" : 41136,
"advanced" : 0,
"needTime" : 41135,
"needYield" : 0,
"saveState" : 321,
"restoreState" : 321,
"isEOF" : 1,
"invalidates" : 0,
"chunkSkips" : 0,
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"account.accountId" : {
"$eq" : "G:8183971619"
}
},
"nReturned" : 0,
"executionTimeMillisEstimate" : 270,
"works" : 41136,
"advanced" : 0,
"needTime" : 41135,
"needYield" : 0,
"saveState" : 321,
"restoreState" : 321,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 41135,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 41135,
"executionTimeMillisEstimate" : 90,
"works" : 41136,
"advanced" : 41135,
"needTime" : 0,
"needYield" : 0,
"saveState" : 321,
"restoreState" : 321,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"account.service" : 1,
"account.accountId" : 1
},
"indexName" : "account.service_1_account.accountId_1",
"isMultiKey" : true,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"account.service" : [
"[\"GameCenter\", \"GameCenter\"]"
],
"account.accountId" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : 41135,
"dupsTested" : 41135,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}
}
]
}
},
总而言之,查询确实使用了索引,但扫描了大量文档以获得结果(122k / 1百万)。我们无法使用覆盖索引,因为我们还需要返回其他信息。
还有一些问题:我查询了一个独特的accoundId,但我对account.accountId的indexBounds是:
"account.accountId" : [
"[MinKey, MaxKey]"
]
感谢您的帮助!
火星
答案 0 :(得分:0)
仔细阅读executionStatus后,查询实际上是扫描所有索引,因为“accountId”indexBounds是[MinKey,MaxKey]。
我可以使用$ elemMatch
解决此问题db.model.find({
account:{
$elemMatch:{
accountId:"111",
service:"facebook"
}
}
})