我有一个包含3000万个文档的馆藏,并且我正在尝试进行实时搜索,以通过关键字选择文档并将其分类为certin(它首先显示具有图片并提出声明的用户字段设置为TRUE。
这是我的文档的样子:
{
"_id" : ObjectId("5b7c23a79dfbc868d2c0f805"),
"business" : {
"name" : "Cartersville Garage Door",
"nameArr" : [
"cartersville",
"garage",
"door"
],
"slug" : "cartersville-garage-door",
"img" : {
"profile" : {
"full" : "5b7c23a79dfbc868d2c0f805_1000x1000_cartersville-garage-door-united-states-georgia_1.jpg",
"thumb" : "5b7c23a79dfbc868d2c0f805_160x160_cartersville-garage-door-united-states-georgia_1.jpg",
"tiny" : "5b7c23a79dfbc868d2c0f805_40x40_cartersville-garage-door-united-states-georgia_1.jpg"
},
},
},
}
这是我的查询的样子,需要53秒才能完成,如果我删除排序部分,则很快就会达到几毫秒。
db.getCollection("users").find(
{
"business.nameArr" : {
"$regex" : "^car"
}
}
).sort(
{
"business.claim" : -1,
"business.img.profile.full" : -1
}
).limit(10);
我在以下位置有一个索引:
{
"key" : {
"business.nameArr" : NumberInt(1),
"business.claim" : NumberInt(-1),
"business.img.profile.full" : NumberInt(-1)
},
"name" : "nameArr-claim-profileImg",
"background" : true,
}
这是explain():
{
"queryPlanner" : {
"plannerVersion" : NumberInt(1),
"namespace" : "bunity.users",
"indexFilterSet" : false,
"parsedQuery" : {
"business.nameArr" : {
"$regex" : "^car"
}
},
"winningPlan" : {
"stage" : "SORT",
"sortPattern" : {
"business.claim" : -1.0,
"business.img.profile.full" : -1.0
},
"limitAmount" : NumberInt(10),
"inputStage" : {
"stage" : "SORT_KEY_GENERATOR",
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"business.nameArr" : NumberInt(1),
"business.claim" : NumberInt(-1),
"business.img.profile.full" : NumberInt(-1)
},
"indexName" : "nameArr-claim-profileImg",
"isMultiKey" : true,
"multiKeyPaths" : {
"business.nameArr" : [
"business.nameArr"
],
"business.claim" : [
],
"business.img.profile.full" : [
]
},
"isUnique" : false,
"isSparse" : true,
"isPartial" : false,
"indexVersion" : NumberInt(2),
"direction" : "forward",
"indexBounds" : {
"business.nameArr" : [
"[\"car\", \"cas\")",
"[/^car/, /^car/]"
],
"business.claim" : [
"[MaxKey, MinKey]"
],
"business.img.profile.full" : [
"[MaxKey, MinKey]"
]
}
}
}
}
},
"rejectedPlans" : [
]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : NumberInt(10),
"executionTimeMillis" : NumberInt(53433),
"totalKeysExamined" : NumberInt(807173),
"totalDocsExamined" : NumberInt(790436),
"executionStages" : {
"stage" : "SORT",
"nReturned" : NumberInt(10),
"executionTimeMillisEstimate" : NumberInt(53286),
"works" : NumberInt(807186),
"advanced" : NumberInt(10),
"needTime" : NumberInt(807175),
"needYield" : NumberInt(0),
"saveState" : NumberInt(6308),
"restoreState" : NumberInt(6308),
"isEOF" : NumberInt(1),
"invalidates" : NumberInt(0),
"sortPattern" : {
"business.claim" : -1.0,
"business.img.profile.full" : -1.0
},
"memUsage" : NumberInt(16315),
"memLimit" : NumberInt(33554432),
"limitAmount" : NumberInt(10),
"inputStage" : {
"stage" : "SORT_KEY_GENERATOR",
"nReturned" : NumberInt(790436),
"executionTimeMillisEstimate" : NumberInt(53066),
"works" : NumberInt(807175),
"advanced" : NumberInt(790436),
"needTime" : NumberInt(16738),
"needYield" : NumberInt(0),
"saveState" : NumberInt(6308),
"restoreState" : NumberInt(6308),
"isEOF" : NumberInt(1),
"invalidates" : NumberInt(0),
"inputStage" : {
"stage" : "FETCH",
"nReturned" : NumberInt(790436),
"executionTimeMillisEstimate" : NumberInt(51616),
"works" : NumberInt(807174),
"advanced" : NumberInt(790436),
"needTime" : NumberInt(16737),
"needYield" : NumberInt(0),
"saveState" : NumberInt(6308),
"restoreState" : NumberInt(6308),
"isEOF" : NumberInt(1),
"invalidates" : NumberInt(0),
"docsExamined" : NumberInt(790436),
"alreadyHasObj" : NumberInt(0),
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : NumberInt(790436),
"executionTimeMillisEstimate" : NumberInt(1753),
"works" : NumberInt(807174),
"advanced" : NumberInt(790436),
"needTime" : NumberInt(16737),
"needYield" : NumberInt(0),
"saveState" : NumberInt(6308),
"restoreState" : NumberInt(6308),
"isEOF" : NumberInt(1),
"invalidates" : NumberInt(0),
"keyPattern" : {
"business.nameArr" : NumberInt(1),
"business.claim" : NumberInt(-1),
"business.img.profile.full" : NumberInt(-1)
},
"indexName" : "nameArr--claim-profileImg",
"isMultiKey" : true,
"multiKeyPaths" : {
"business.nameArr" : [
"business.nameArr"
],
"business.claim" : [
],
"business.img.profile.full" : [
]
},
"isUnique" : false,
"isSparse" : true,
"isPartial" : false,
"indexVersion" : NumberInt(2),
"direction" : "forward",
"indexBounds" : {
"business.nameArr" : [
"[\"car\", \"cas\")",
"[/^car/, /^car/]"
],
"business.claim" : [
"[MaxKey, MinKey]"
],
"business.img.profile.full" : [
"[MaxKey, MinKey]"
]
},
"keysExamined" : NumberInt(807173),
"seeks" : NumberInt(2),
"dupsTested" : NumberInt(807172),
"dupsDropped" : NumberInt(16736),
"seenInvalidated" : NumberInt(0)
}
}
}
},
"allPlansExecution" : [
]
},
}
有没有想到如何使此查询运行更快?