【问题标题】:MongoDB Index ensureIndex Time to RefreshMongoDB 索引 ensureIndex 刷新时间
【发布时间】: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/…
  • 你能用解释结果编辑你的问题吗?

标签: mongodb casbah


【解决方案1】:

关于“nscanned”超出计数,这很可能是因为您实际上拥有的索引条目比您拥有的文档多得多:列表中的每个项目都是一个索引条目。似乎在这里,每个文档的列表中平均有 2 个项目。 “nscannedObjects”遵循相同的原则,因为每当查看文档时该计数器都会增加,即使之前已将同一文档作为同一查询的一部分查看。

【讨论】:

  • 所以,如果我有 2000 个文档。平均而言,每个文档都有一个包含 3 个子文档的数组。如果在 2000 个文档中,有 2/3 个子文档匹配 n: "Name", v: "Kevin", l: {$gt: 0, $lt: 15},那么 nScanned 应该等于 4000(2000 个文档乘以每个文档匹配 2 个子文档)?
猜你喜欢
  • 1970-01-01
  • 2011-10-23
  • 1970-01-01
  • 2014-02-09
  • 2011-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-18
相关资源
最近更新 更多