为什么mongo不使用正确的索引?

时间:2018-06-12 13:38:09

标签: mongodb

我正在使用MongoDB来存储具有以下结构的数据:

{ "_id" : ObjectId("5a497a1f451bf00880331913"), "device" : "deviceId", "topic" : "topicId", "vhost" : "vhostId", "date" : ISODate("2018-01-01T00:00:31.220Z"), "value" : 452 }

使用以下索引:

[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "thingsplay.data"
        },
        {
                "v" : 2,
                "key" : {
                        "device" : 1,
                        "topic" : 1,
                        "date" : 1
                },
                "name" : "device_1_topic_1_date_1",
                "ns" : "thingsplay.data",
                "background" : true
        },
        {
                "v" : 2,
                "key" : {
                        "device" : 1,
                        "topic" : 1,
                        "vhost" : 1
                },
                "name" : "device_1_topic_1_vhost_1",
                "ns" : "thingsplay.data",
                "background" : true
        }
]

但是当我使用设备,主题和日期字段进行查询时,它使用device_1_topic_1_vhost_1索引......

对于另一个带有解释选项的请求,我刚刚获得了正确的获胜计划(device_1_topic_1_date_1),但是当我执行它时...我仍在等待它完成并且db.currentOp()我看不到它使用任何索引......

{
        "desc" : "conn2172",
        "threadId" : "2836",
        "connectionId" : 2172,
        "client" : "x.x.x.x:50177",
        "appName" : "MongoDB Shell",
        "clientMetadata" : {
                "application" : {
                        "name" : "MongoDB Shell"
                },
                "driver" : {
                        "name" : "MongoDB Internal Client",
                        "version" : "3.6.2"
                },
                "os" : {
                        "type" : "Windows",
                        "name" : "Microsoft Windows Server 2008 R2",
                        "architecture" : "x86_64",
                        "version" : "6.1 SP1 (build 7601)"
                }
        },
        "active" : true,
        "opid" : 22137990,
        "secs_running" : 322,
        "microsecs_running" : NumberLong(322246758),
        "op" : "command",
        "ns" : "company.data",
        "query" : {
                "explain" : {
                        "find" : "data",
                        "filter" : {
                                "device" : "deviceId",
                                "topic" : "topicId",
                                "date" : {
                                        "$gt" : ISODate("2018-01-01T00:00:00Z"),

                                        "$lt" : ISODate("2018-06-12T16:18:00Z")
                                }
                        }
                },
                "verbosity" : "executionStats"
        },
        "numYields" : 19245,
        "locks" : {
                "Global" : "r",
                "Database" : "r",
                "Collection" : "r"
        },
        "waitingForLock" : false,
        "lockStats" : {
                "Global" : {
                        "acquireCount" : {
                                "r" : NumberLong(38492)
                        }
                },
                "Database" : {
                        "acquireCount" : {
                                "r" : NumberLong(19246)
                        }
                },
                "Collection" : {
                        "acquireCount" : {
                                "r" : NumberLong(19246)
                        }
                }
        }
}

我不明白为什么索引没有被使用......

0 个答案:

没有答案