【发布时间】:2013-10-31 20:33:33
【问题描述】:
我通过 Casbah(Mongo 的 Scala 库)创建了一个多键复合索引:
db.collection.ensureIndex(MongoDBObject("Header.records.n" -> 1) ++ MongoDBObject("Header.records.v" -> 1) ++ MongoDBObject("Header.records.l" -> 1))
然后,通过 Mongo Shell,我执行了db.collection.find(...).explain,其中nScannedObjects 超过了db.collection.count()。查看 Mongo docs,似乎需要调用 ensureIndex 一次,然后任何写入都会强制更新索引。
但是,我看到一个post 和这个one 只需要调用一次db.collection.ensureIndex(...)。
编辑
>db.collection.find( {"Header.records" : {$all : [
{$elemMatch: {n: "Name", v: "Kevin",
"l" : { "$gt" : 0 , "$lt" : 15}} }]}},
{_id : 1}).explain()
{
"cursor" : "BtreeCursor
Header.records.n_1_Header.records.v_1_Header.records.l_1",
"isMultiKey" : true,
"n" : 4098,
"nscannedObjects" : 9412,
"nscanned" : 9412,
"nscannedObjectsAllPlans" : 9412,
"nscannedAllPlans" : 9412,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 152,
"indexBounds" : {
"Header.records.n" : [
[
"Name",
"Name"
]
],
"Header.records.v" : [
[
"Kevin",
"Kevin"
]
],
"Header.records.l" : [
[
0,
1.7976931348623157e+308
]
]
},
"server" : "ABCD:27017"
注意 nScanned (9412) > count(4248)。
> db.collection.count()
4248
为什么?
【问题讨论】:
-
更新对文档本身的更新是原子的,基本上是即时的
-
但是,为什么我看到
db.collection.count()是4000,但运行db.collection.find(...).explain()显示nScannedObjects也是9000。 -
你能提供实际的解释吗?这听起来像是一个使用不当的索引
-
db.claims.find( {"recordss" : {$all : [ {$elemMatch: {n: "Name", v: "Kevin", "l" : { "$gt" : 0 , "$lt" : 15}} }]}}, {_id : 1}).expla in()我正在使用类似于这篇文章的 n-v-l 数据库结构 - edgystuff.tumblr.com/post/47178201123/… -
你能用解释结果编辑你的问题吗?