我正在使用分片副本集。拥有一个包含以下索引的大型集合:
{
"shop" : 1,
"email" : 1
}
我试图找到逐个查询每个元素的最佳方式:
db.user.explain('executionStats').find({shop:151, email:{$exists:true}}, {limit:1}))
结果如下。
我的问题是: 查询是否使用索引? 如果是,我如何阅读解释结果?
第二: 最好使用此查询,循环游标,然后再次查询
.find({shop:151,email:{$ exists:true}},{limit:100}))
或
.findOne({shop:151,email:{$ exists:true}}))
连续循环?
{
"queryPlanner" : {
"plannerVersion" : NumberInt(1),
"namespace" : "vt4users.user",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"shop" : {
"$eq" : NumberInt(151)
}
},
{
"email" : {
"$exists" : true
}
}
]
},
"winningPlan" : {
"stage" : "PROJECTION",
"transformBy" : {
"limit" : NumberInt(1)
},
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"email" : {
"$exists" : true
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"shop" : NumberInt(1),
"email" : NumberInt(1)
},
"indexName" : "shop_1_email_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : true,
"isPartial" : false,
"indexVersion" : NumberInt(1),
"direction" : "forward",
"indexBounds" : {
"shop" : [
"[151.0, 151.0]"
],
"email" : [
"[MinKey, MaxKey]"
]
}
}
}
},
"rejectedPlans" : [
]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : NumberInt(3),
"executionTimeMillis" : NumberInt(0),
"totalKeysExamined" : NumberInt(3),
"totalDocsExamined" : NumberInt(3),
"executionStages" : {
"stage" : "PROJECTION",
"nReturned" : NumberInt(3),
"executionTimeMillisEstimate" : NumberInt(0),
"works" : NumberInt(4),
"advanced" : NumberInt(3),
"needTime" : NumberInt(0),
"needYield" : NumberInt(0),
"saveState" : NumberInt(0),
"restoreState" : NumberInt(0),
"isEOF" : NumberInt(1),
"invalidates" : NumberInt(0),
"transformBy" : {
"limit" : NumberInt(1)
},
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"email" : {
"$exists" : true
}
},
"nReturned" : NumberInt(3),
"executionTimeMillisEstimate" : NumberInt(0),
"works" : NumberInt(4),
"advanced" : NumberInt(3),
"needTime" : NumberInt(0),
"needYield" : NumberInt(0),
"saveState" : NumberInt(0),
"restoreState" : NumberInt(0),
"isEOF" : NumberInt(1),
"invalidates" : NumberInt(0),
"docsExamined" : NumberInt(3),
"alreadyHasObj" : NumberInt(0),
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : NumberInt(3),
"executionTimeMillisEstimate" : NumberInt(0),
"works" : NumberInt(4),
"advanced" : NumberInt(3),
"needTime" : NumberInt(0),
"needYield" : NumberInt(0),
"saveState" : NumberInt(0),
"restoreState" : NumberInt(0),
"isEOF" : NumberInt(1),
"invalidates" : NumberInt(0),
"keyPattern" : {
"shop" : NumberInt(1),
"email" : NumberInt(1)
},
"indexName" : "shop_1_email_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : true,
"isPartial" : false,
"indexVersion" : NumberInt(1),
"direction" : "forward",
"indexBounds" : {
"shop" : [
"[151.0, 151.0]"
],
"email" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : NumberInt(3),
"dupsTested" : NumberInt(0),
"dupsDropped" : NumberInt(0),
"seenInvalidated" : NumberInt(0)
}
}
}
},
"serverInfo" : {
"host" : "host",
"port" : NumberInt(27017),
"version" : "3.2.11",
"gitVersion" : "009580ad490190ba33d1c6253ebd8d91808923e4"
},
"ok" : NumberInt(1)
}
答案 0 :(得分:1)
您的查询正在使用索引。您可以在inputStage
"stage" : "IXSCAN"
您可以check it in the docs更好地理解其含义。
关于你的第二个问题,如果你想获取所有结果,使用findOne循环并不比正常执行查询更好(性能明智)。