Mongo 3.4 是否为此查询使用索引?
Is Mongo 3.4 using indexes for this query or not?
我正在使用分片副本集。拥有一个具有以下索引的大型集合:
{
"shop" : 1,
"email" : 1
}
我正在尝试找到逐一查询每个元素的最佳方法:
db.user.explain('executionStats').find({shop:151, email:{$exists:true}}, {limit:1}))
结果如下。
我的问题是:
查询是否使用索引?
如果是,我如何阅读这个 explain 结果?
第二:
最好使用这个查询,循环游标,然后再查询
.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)
}
您的查询正在使用索引。您可以在 inputStage
中看到
"stage" : "IXSCAN"
您可以check it in the docs更好地理解其含义。
关于你的第二个问题,如果你想获取所有结果,使用 findOne 循环并不比正常查询更好(性能方面)。
我正在使用分片副本集。拥有一个具有以下索引的大型集合:
{
"shop" : 1,
"email" : 1
}
我正在尝试找到逐一查询每个元素的最佳方法:
db.user.explain('executionStats').find({shop:151, email:{$exists:true}}, {limit:1}))
结果如下。
我的问题是: 查询是否使用索引? 如果是,我如何阅读这个 explain 结果?
第二: 最好使用这个查询,循环游标,然后再查询
.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)
}
您的查询正在使用索引。您可以在 inputStage
"stage" : "IXSCAN"
您可以check it in the docs更好地理解其含义。
关于你的第二个问题,如果你想获取所有结果,使用 findOne 循环并不比正常查询更好(性能方面)。