Mongo选择了错误的索引

时间:2015-04-20 11:04:52

标签: mongodb

我正在使用mongo db 2.6.9。 我在一个集合上创建了两个索引,我不理解查询刨床总是选择错误索引的共鸣。

我想了解我所缺少的东西。

第一个指数:

{
   "TimeStamp":1,
   "A":1,
   "B":1,
}

第二个指数:

{
   "TimeStamp":1,
   "A":1,
   "C":1,
}

当我使用以下查询时,计划程序会选择第一个索引:

db.collection.find({"TimeStamp":{ "$gte" : ISODate("2015-04-14T00:00:00Z"), "$lt" : ISODate("2015-04-15T00:00:00Z") },"C":2137,"A":1}).explain()

解释({verbose:1})结果:

{
    "cursor" : "BtreeCursor IX_First",
    "isMultiKey" : false,
    "n" : 0,
    "nscannedObjects" : 0,
    "nscanned" : 0,
    "nscannedObjectsAllPlans" : 0,
    "nscannedAllPlans" : 0,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 0,
    "indexBounds" : {
            "TimeStamp" : [
                    [
                            ISODate("2015-04-14T00:00:00Z"),
                            ISODate("2015-04-15T00:00:00Z")
                    ]
            ],
            "A" : [
                    [
                            1,
                            1
                    ]
            ],
            "B" : [
                    [
                            {
                                    "$minElement" : 1
                            },
                            {
                                    "$maxElement" : 1
                            }
                    ]
            ],
    },
    "allPlans" : [
            {
                    "cursor" : "BtreeCursor IX_First",
                    "isMultiKey" : false,
                    "n" : 0,
                    "nscannedObjects" : 0,
                    "nscanned" : 0,
                    "scanAndOrder" : false,
                    "indexOnly" : false,
                    "nChunkSkips" : 0,
                    "indexBounds" : {
            "TimeStamp" : [
                    [
                            ISODate("2015-04-14T00:00:00Z"),
                            ISODate("2015-04-15T00:00:00Z")
                    ]
            ],
            "A" : [
                    [
                            1,
                            1
                    ]
            ],
            "B" : [
                    [
                            {
                                    "$minElement" : 1
                            },
                            {
                                    "$maxElement" : 1
                            }
                    ]
            ],
    },
            {
                    "cursor" : "BtreeCursor IIX_Second",
                    "isMultiKey" : false,
                    "n" : 0,
                    "nscannedObjects" : 0,
                    "nscanned" : 0,
                    "scanAndOrder" : false,
                    "indexOnly" : false,
                    "nChunkSkips" : 0,
                    "indexBounds" : {
                            "TimeStamp" : [
                                    [
                                            ISODate("2015-04-14T00:00:00Z"),
                                            ISODate("2015-04-15T00:00:00Z")
                                    ]
                            ],
                            "A" : [
                                    [
                                            1,
                                            1
                                    ]
                            ],
                            "C" : [
                                    [
                                            2137,
                                            2137
                                    ]
                            ]
                    }
            }
    ],
    "server" : "mongo2:27017",
    "filterSet" : false,
    "stats" : {
            "type" : "KEEP_MUTATIONS",
            "works" : 2,
            "yields" : 0,
            "unyields" : 0,
            "invalidates" : 0,
            "advanced" : 0,
            "needTime" : 0,
            "needFetch" : 0,
            "isEOF" : 1,
            "children" : [
                    {
                            "type" : "FETCH",
                            "works" : 1,
                            "yields" : 0,
                            "unyields" : 0,
                            "invalidates" : 0,
                            "advanced" : 0,
                            "needTime" : 0,
                            "needFetch" : 0,
                            "isEOF" : 1,
                            "alreadyHasObj" : 0,
                            "forcedFetches" : 0,
                            "matchTested" : 0,
                            "children" : [
                                    {
                                            "type" : "IXSCAN",
                                            "works" : 1,
                                            "yields" : 0,
                                            "unyields" : 0,
                                            "invalidates" : 0,
                                            "advanced" : 0,
                                            "needTime" : 0,
                                            "needFetch" : 0,
                                            "isEOF" : 1,
                                            "keyPattern" : "{ TimeStamp: 1, A: 1, B: 1}",
                                            "isMultiKey" : 0,
                                            "boundsVerbose" : "field #0['TimeStamp']: [new Date(1428969600000), new Date(1429056000000)), field #1['A']: [1.0, 1.0], field #2['B']: [MinKey, MaxKey]",
                                            "yieldMovedCursor" : 0,
                                            "dupsTested" : 0,
                                            "dupsDropped" : 0,
                                            "seenInvalidated" : 0,
                                            "matchTested" : 0,
                                            "keysExamined" : 0,
                                            "children" : [ ]
                                    }
                            ]
                    }
            ]
    }

}

1 个答案:

答案 0 :(得分:0)

这是因为在这种情况下索引字段的顺序很重要。尝试交换A和C:

db.collection.find({"TimeStamp":{ "$gte" : ISODate("2015-04-14T00:00:00Z"), "$lt" : ISODate("2015-04-15T00:00:00Z") },"A":1,"C":2137}).explain()