db.testData.find({"groups":{$gt:10,$lt:15}}).explain()
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 4,
"nscannedObjects" : 10000,
"nscanned" : 10000,
"nscannedObjectsAllPlans" : 10000,
"nscannedAllPlans" : 10000,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 10,
"indexBounds" : {
},
"server" : "ubuntu:27017"
}
> db.testData.ensureIndex({"groups":1})
> db.testData.find({"groups":{$gt:10,$lt:15}}).explain()
{
"cursor" : "BtreeCursor groups_1",
"isMultiKey" : false,
"n" : 4,
"nscannedObjects" : 4,
"nscanned" : 4,
"nscannedObjectsAllPlans" : 4,
"nscannedAllPlans" : 4,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"groups" : [
[
10,
15
]
]
},
"server" : "ubuntu:27017"
}
> db.testData.find({"groups":{$gt:10,$lt:15}}).explain()
{
"cursor" : "BtreeCursor groups_1",
"isMultiKey" : false,
"n" : 4,
"nscannedObjects" : 4,
"nscanned" : 4,
"nscannedObjectsAllPlans" : 4,
"nscannedAllPlans" : 4,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"groups" : [
[
10,
15
]
]
},
"server" : "ubuntu:27017"
}
mongodb查询 加索引 减索引 的查询效果差别 :
nscanned体现了Mongo扫描字段索引的条数,而nscannedObjects则为最终结果中查询过的文档数目。n则表示了返回文档的数目。nscannedObjects至少包含了所有的返回文档,即使Mongo明确了可以通过查看绝对匹配文件的索引。因此可以得出nscanned >= nscannedObjects >= n。