mongodb查询应该由索引覆盖,但不是

时间:2014-03-11 15:24:51

标签: mongodb compound-index

查询:

db.myColl.find({"M.ST": "mostrepresentedvalueinthecollection", "M.TS": new Date(2014,2,1)}).explain()

解释输出:

"cursor" : "BtreeCursor M.ST_1_M.TS_1",
        "isMultiKey" : false,
        "n" : 587606,
        "nscannedObjects" : 587606,
        "nscanned" : 587606,
        "nscannedObjectsAllPlans" : 587606,
        "nscannedAllPlans" : 587606,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 9992,
        "nChunkSkips" : 0,
        "millis" : 174820,
        "indexBounds" : {
                "M.ST" : [
                        [
                                "mostrepresentedvalueinthecollection",
                                "mostrepresentedvalueinthecollection"
                        ]
                ],
                "M.TS" : [
                        [
                                ISODate("2014-03-01T00:00:00Z"),
                                ISODate("2014-03-01T00:00:00Z")
                        ]
                ]
        },
        "server" : "myServer"

其他详细信息:myColl包含大约40万个文档,平均对象大小为300b。

我不知道为什么indexOnly没有设置为true,我在{" M.ST":1," M.TS":1上有一个复合索引}

mongo主机是一个带有16GB RAM和500GB磁盘空间(旋转磁盘)的unix盒子。 数据库的总索引大小为10gb,我们已经获得了大约1k upserts / sec,其中1K 20是插入,其余是增量。

我们有另一个查询在查询查询中添加第三个字段(名为" M.X"),还有一个复合索引在" M.ST",&#34 ; M.X"," M.TS"。那个快速闪电,只扫描330个文件。

知道可能出现什么问题吗?

感谢。

编辑:这是示例文档的结构:

{
    "_id" : "somestring",
    "D" : {
        "20140301" : {
            "IM" : {
                "CT" : 143
            }
        },
        "20140302" : {
            "IM" : {
                "CT" : 44
            }
        },
        "20140303" : {
            "IM" : {
                "CT" : 206
            }
        },
        "20140314" : {
            "IM" : {
                "CT" : 5
            }
        }
    },
    "Y" : "someotherstring",
    "IM" : {
        "CT" : 1
    },
    "M" : {
        "X" : 99999,
        "ST" : "mostrepresentedvalueinthecollection",
        "TS" : ISODate("2014-03-01T00:00:00.000Z")
    },
}

我们的想法是按月存储一些分析指标," D" field表示包含每月每天数据的文档数组。

2 个答案:

答案 0 :(得分:1)

修改 此功能目前尚未实施。相应的JIRA票证是SERVER-2104。您可以为它提供支持,但是现在,要使用覆盖索引查询,您需要避免使用点符号/嵌入文档。

答案 1 :(得分:0)

我认为你需要在该查询上设置一个投影,告诉mongo它涵盖了哪些索引。

试试这个..

db.myColl.find({"M.ST": "mostrepresentedvalueinthecollection", "M.TS": new Date(2014,2,1)},{ M.ST:1, M.TS:1, _id:0 }).explain()