gpt4 book ai didi

mongodb - 如何使用数组中的多键索引改进 mongo 查询

转载 作者:行者123 更新时间:2023-12-04 03:14:19 25 4
gpt4 key购买 nike

我刚刚发现我们的 1 个查询非常慢。我们在数组中使用多键索引,但不确定如何实际改进此查询。

我们的数据结构:

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/100 万)。我们不能使用覆盖索引,因为我们还需要返回一些其他信息。

还有一些奇怪的事情:我正在查询一个唯一的 accoundId,但是我的 account.accountId 的 indexBounds 是:

"account.accountId" : [
"[MinKey, MaxKey]"
]

感谢您的帮助!

火星

最佳答案

仔细阅读 executionStatus 后,查询实际上正在扫描所有索引,因为“accountId”indexBounds 是 [MinKey, MaxKey]。

我可以用 $elemMatch 解决这个问题

db.model.find({
account:{
$elemMatch:{
accountId:"111",
service:"facebook"
}
}
})

关于mongodb - 如何使用数组中的多键索引改进 mongo 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42482262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com