使用distinct和query的Mongo请求使用什么索引?

时间:2017-08-23 07:13:59

标签: mongodb mongodb-query

我有一个Mongo集合,可以容纳数百万的IoT设备数据。 文件的结构如下:

{ ObjectID:"...", device:"DEVICE3", topic:"TEMP", vhost:"client1", date:ISODate("2017-08-23T08:00:00.000Z"), value:23.5 }

我有一个Rest API,其中包含查找一个特定vhost的所有设备的请求。 该请求如下所示:db.data.distinct("device", { vhost:"client1" })

我在vhost和设备上添加了一个索引:db.data.createIndex( { vhost:1, device:1 })但它仍然是很多被检查的文档。我可以使用哪种索引来优化请求?

"executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 15848881,
        "executionTimeMillis" : 42425,
        "totalKeysExamined" : 15848881,
        "totalDocsExamined" : 15848881,
        "executionStages" : {
                "stage" : "FETCH",
                "nReturned" : 15848881,
                "executionTimeMillisEstimate" : 36240,
                "works" : 15848882,
                "advanced" : 15848881,
                "needTime" : 0,
                "needYield" : 0,
                "saveState" : 123949,
                "restoreState" : 123949,
                "isEOF" : 1,
                "invalidates" : 0,
                "docsExamined" : 15848881,
                "alreadyHasObj" : 0,
                "inputStage" : {
                        "stage" : "IXSCAN",
                        "nReturned" : 15848881,
                        "executionTimeMillisEstimate" : 9890,
                        "works" : 15848882,
                        "advanced" : 15848881,
                        "needTime" : 0,
                        "needYield" : 0,
                        "saveState" : 123949,
                        "restoreState" : 123949,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "keyPattern" : {
                                "vhost" : 1,
                                "device" : 1
                        },
                        "indexName" : "vhost_1_device_1",
                        "isMultiKey" : false,
                        "isUnique" : false,
                        "isSparse" : false,
                        "isPartial" : false,
                        "indexVersion" : 1,
                        "direction" : "forward",
                        "indexBounds" : {
                                "vhost" : [
                                        "[\"client1\", \"client1\"]"
                                ],
                                "device" : [
                                        "[MinKey, MaxKey]"
                                ]
                        },
                        "keysExamined" : 15848881,
                        "dupsTested" : 0,
                        "dupsDropped" : 0,
                        "seenInvalidated" : 0
                }
        }
},

在最后,大约有30个不同的设备。

编辑:

以下是queryPlanner:

"queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "thingsplay.data",
        "indexFilterSet" : false,
        "parsedQuery" : {
                "vhost" : {
                        "$eq" : "client1"
                }
        },
        "winningPlan" : {
                "stage" : "FETCH",
                "inputStage" : {
                        "stage" : "IXSCAN",
                        "keyPattern" : {
                                "vhost" : 1,
                                "device" : 1
                        },
                        "indexName" : "vhost_1_device_1",
                        "isMultiKey" : false,
                        "isUnique" : false,
                        "isSparse" : false,
                        "isPartial" : false,
                        "indexVersion" : 1,
                        "direction" : "forward",
                        "indexBounds" : {
                                "vhost" : [
                                        "[\"client1\", \"client1\"]"
                                ],
                                "device" : [
                                        "[MinKey, MaxKey]"
                                ]
                        }
                }
        },
        "rejectedPlans" : [ ]
},

结果:

[
    "F000105",
    "F000107",
    "F000109",
    "F000110",
    "F000113",
    "F000119",
    "F000121",
    "F000124",
    "F000128",
    "F000131",
    "F000134",
    "F000138",
    "F000144",
    "F000146",
    "F000147",
    "F000148",
    "F000149",
    "F000150",
    "F000153",
    "F000155",
    "F000156",
    "F000159",
    "F000161",
    "F000164",
    "F000166",
    "F000167",
    "F000168",
    "F000169",
    "F000170",
    "F000171",
    "F000172",
    "F000181",
    "F000183",
    "F000184",
    "F000187",
    "F000190",
    "F000192",
    "F000193",
    "F000203",
    "F000204",
    "F000205",
    "F000208",
    "F000209",
    "F000215",
    "F000221",
    "F000223",
    "F000243",
    "F000249",
    "F000250",
    "F000251",
    "F000253",
    "F000255",
    "S0E190E",
    "S0E1A45",
    "S0E1AC0",
    "SYS_STATUS_ID",
    "TS4D9292",
    "TS4D9294",
    "TS4D9296",
    "TS4D9297",
    "TS4D9298",
    "TS4D9299",
    "TS4D929B",
    "TS4D929D",
    "TS4D929F",
    "TS4D92A0",
    "TS4D92A2",
    "TS4D92A6",
    "TS4D92AA",
    "TS4D92B1",
    "TS4D92B2",
    "TS4D92B3",
    "TS4D92B4",
    "TS4D92C2"
]

0 个答案:

没有答案