带id的Mongodb排序检查所有记录?

时间:2017-11-16 12:01:02

标签: mongodb

我正在尝试分析以下查询性能。在集合中使用50k条目执行查询需要23秒才能响应。

db.getCollection('collection').find({ipAddress: "127.0.0.1"}).sort({"_id": -1})

在此查询中,ipAddress已编制索引,默认情况下会对id进行索引。我用explain命令检查了,响应是

"totalKeysExamined" : 49827,
"totalDocsExamined" : 49827,

但是当我删除排序并且响应是

"totalKeysExamined" : 23877,
"totalDocsExamined" : 23877,

我想知道Mongodb用id检查所有记录吗?

编辑:

添加解释查询的整个响应

{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "collection",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "ipAddress" : {
            "$eq" : "127.0.0.1"
        }
    },
    "winningPlan" : {
        "stage" : "FETCH",
        "filter" : {
            "ipAddress" : {
                "$eq" : "127.0.0.1"
            }
        },
        "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                "_id" : 1
            },
            "indexName" : "_id_",
            "isMultiKey" : false,
            "isUnique" : true,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "backward",
            "indexBounds" : {
                "_id" : [ 
                    "[MaxKey, MinKey]"
                ]
            }
        }
    },
    "rejectedPlans" : [ 
        {
            "stage" : "SORT",
            "sortPattern" : {
                "_id" : -1.0
            },
            "inputStage" : {
                "stage" : "SORT_KEY_GENERATOR",
                "inputStage" : {
                    "stage" : "FETCH",
                    "inputStage" : {
                        "stage" : "IXSCAN",
                        "keyPattern" : {
                            "ipAddress" : 1.0
                        },
                        "indexName" : "ipAddress_1",
                        "isMultiKey" : false,
                        "isUnique" : false,
                        "isSparse" : false,
                        "isPartial" : false,
                        "indexVersion" : 1,
                        "direction" : "forward",
                        "indexBounds" : {
                            "ipAddress" : [ 
                                "[\"127.0.0.1\", \"127.0.0.1\"]"
                            ]
                        }
                    }
                }
            }
        }
    ]
},
"executionStats" : {
    "executionSuccess" : true,
    "nReturned" : 33809,
    "executionTimeMillis" : 25572,
    "totalKeysExamined" : 105260,
    "totalDocsExamined" : 105260,
    "executionStages" : {
        "stage" : "FETCH",
        "filter" : {
            "ipAddress" : {
                "$eq" : "127.0.0.1"
            }
        },
        "nReturned" : 33809,
        "executionTimeMillisEstimate" : 24894,
        "works" : 105261,
        "advanced" : 33809,
        "needTime" : 71451,
        "needYield" : 0,
        "saveState" : 1376,
        "restoreState" : 1376,
        "isEOF" : 1,
        "invalidates" : 0,
        "docsExamined" : 105260,
        "alreadyHasObj" : 0,
        "inputStage" : {
            "stage" : "IXSCAN",
            "nReturned" : 105260,
            "executionTimeMillisEstimate" : 2020,
            "works" : 105261,
            "advanced" : 105260,
            "needTime" : 0,
            "needYield" : 0,
            "saveState" : 1376,
            "restoreState" : 1376,
            "isEOF" : 1,
            "invalidates" : 0,
            "keyPattern" : {
                "_id" : 1
            },
            "indexName" : "_id_",
            "isMultiKey" : false,
            "isUnique" : true,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "backward",
            "indexBounds" : {
                "_id" : [ 
                    "[MaxKey, MinKey]"
                ]
            },
            "keysExamined" : 105260,
            "dupsTested" : 0,
            "dupsDropped" : 0,
            "seenInvalidated" : 0
        }
    }
},
"serverInfo" : {
    "host" : "client",
    "port" : 27017,
    "version" : "3.2.16",
    "gitVersion" : "1"
},
"ok" : 1.0
}

1 个答案:

答案 0 :(得分:1)

您收藏的1/3与过滤器{ipAddress: "127.0.0.1"}匹配。

以正确的顺序获取所有100k文档的速度更快

"stage" : "IXSCAN", "keyPattern" : {"_id" : 1}

然后过滤掉那些不匹配的内容:

"stage" : "FETCH", "filter" : {"ipAddress" : {"$eq" : "127.0.0.1"}}

而不是通过过滤器

获取

"stage" : "IXSCAN", "keyPattern" : {"ipAddress" : 1.0}

然后在内存中对40k文件进行排序

"stage" : "SORT","sortPattern" : {"_id" : -1.0}

删除排序时,可以更快地使用{"ipAddress" : 1.0}索引来仅提取匹配的文档。

您可能会发现索引{ipAddress: 1, _id: 1}更有效率,特别是如果您将它与相同的投影选项结合使用。

作为旁注,它看起来并不健康。我可以想象FETCH阶段如此缓慢,因为从磁盘加载了大量文档,交换回来等等。但我认为IXSCAN阶段没有正当理由

        "stage" : "IXSCAN",
        "nReturned" : 105260,
        "executionTimeMillisEstimate" : 2020,

超过2秒。 100k _ids的IXSCAN应该快10倍。