繁体   English   中英

MongoDB即使使用索引也发现查询非常慢

[英]MongoDB find query very slow even with index

我不明白为什么下面的查询这么慢,尽管有索引,但说明却显示要花74+秒才能完成。

我可以向您保证, dev_id_id都已建立索引,这似乎根本没有帮助。

db.DeviceLoginLog.find({"dev_id": "xxx"}).skip(0).limit(10).sort({"_id": -1}).explain("executionStats");

谁能帮助我解释解释输出并建议我如何加快速度?

{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "example.DeviceLoginLog",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "dev_id" : {
                                "$eq" : "xxx"
                        }
                },
                "winningPlan" : {
                        "stage" : "SORT",
                        "sortPattern" : {
                                "_id" : -1
                        },
                        "limitAmount" : 10,
                        "inputStage" : {
                                "stage" : "KEEP_MUTATIONS",
                                "inputStage" : {
                                        "stage" : "FETCH",
                                        "inputStage" : {
                                                "stage" : "IXSCAN",
                                                "keyPattern" : {
                                                        "dev_id" : 1
                                                },
                                                "indexName" : "dev_id_1",
                                                "isMultiKey" : false,
                                                "direction" : "forward",
                                                "indexBounds" : {
                                                        "dev_id" : [
                                                                "[\"xxx\", \"xxx\"]"
                                                        ]
                                                }
                                        }
                                }
                        }
                },
                "rejectedPlans" : [
                        {
                                "stage" : "LIMIT",
                                "limitAmount" : 4,
                                "inputStage" : {
                                        "stage" : "FETCH",
                                        "filter" : {
                                                "dev_id" : {
                                                        "$eq" : "xxx"
                                                }
                                        },
                                        "inputStage" : {
                                                "stage" : "IXSCAN",
                                                "keyPattern" : {
                                                        "_id" : 1
                                                },
                                                "indexName" : "_id_",
                                                "isMultiKey" : false,
                                                "direction" : "backward",
                                                "indexBounds" : {
                                                        "_id" : [
                                                                "[MaxKey, MinKey]"
                                                        ]
                                                }
                                        }
                                }
                        }
                ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 10,
                "executionTimeMillis" : 74867,
                "totalKeysExamined" : 9203,
                "totalDocsExamined" : 9203,
                "executionStages" : {
                        "stage" : "SORT",
                        "nReturned" : 10,
                        "executionTimeMillisEstimate" : 49860,
                        "works" : 9537,
                        "advanced" : 10,
                        "needTime" : 9204,
                        "needFetch" : 321,
                        "saveState" : 1864,
                        "restoreState" : 1864,
                        "isEOF" : 1,
                        "invalidates" : 1064,
                        "sortPattern" : {
                                "_id" : -1
                        },
                        "memUsage" : 1890,
                        "memLimit" : 33554432,
                        "limitAmount" : 10,
                        "inputStage" : {
                                "stage" : "KEEP_MUTATIONS",
                                "nReturned" : 9203,
                                "executionTimeMillisEstimate" : 49820,
                                "works" : 9525,
                                "advanced" : 9203,
                                "needTime" : 0,
                                "needFetch" : 321,
                                "saveState" : 1864,
                                "restoreState" : 1864,
                                "isEOF" : 1,
                                "invalidates" : 1064,
                                "inputStage" : {
                                        "stage" : "FETCH",
                                        "nReturned" : 9203,
                                        "executionTimeMillisEstimate" : 49820,
                                        "works" : 9525,
                                        "advanced" : 9203,
                                        "needTime" : 0,
                                        "needFetch" : 321,
                                        "saveState" : 1864,
                                        "restoreState" : 1864,
                                        "isEOF" : 1,
                                        "invalidates" : 1064,
                                        "docsExamined" : 9203,
                                        "alreadyHasObj" : 0,
                                        "inputStage" : {
                                                "stage" : "IXSCAN",
                                                "nReturned" : 9203,
                                                "executionTimeMillisEstimate" : 10,
                                                "works" : 9204,
                                                "advanced" : 9203,
                                                "needTime" : 0,
                                                "needFetch" : 0,
                                                "saveState" : 1864,
                                                "restoreState" : 1864,
                                                "isEOF" : 1,
                                                "invalidates" : 1064,
                                                "keyPattern" : {
                                                        "dev_id" : 1
                                                },
                                                "indexName" : "dev_id_1",
                                                "isMultiKey" : false,
                                                "direction" : "forward",
                                                "indexBounds" : {
                                                        "dev_id" : [
                                                                "[\"xxx\", \"xxx\"]"
                                                        ]
                                                },
                                                "keysExamined" : 9203,
                                                "dupsTested" : 0,
                                                "dupsDropped" : 0,
                                                "seenInvalidated" : 0,
                                                "matchTested" : 0
                                        }
                                }
                        }
                }
        },
        "serverInfo" : {
                "host" : "iZ231ear7c9Z",
                "port" : 27017,
                "version" : "3.0.3",
                "gitVersion" : "b40106b36eecd1b4407eb1ad1af6bc60593c6105"
        },
        "ok" : 1
}

提前致谢!

该查询使用索引{dev_id: 1}进行查找,然后必须将匹配的文档提取到内存中以进行排序而不使用索引。 创建一个复合索引为{dev_id: 1, _id: -1}将提高性能。

请参阅索引的排序和非前缀子集

"totalKeysExamined" : 9203,
"totalDocsExamined" : 9203,

这表明查询必须检查每个单独的文档。 您没有在_id字段上使用索引。 另外,根据您要返回的字段,请考虑创建一个允许进行覆盖查询的索引。

涵盖查询是可以完全使用索引满足的查询,而不必检查任何文档。 同时满足以下两个条件时,索引将覆盖查询:

  • 查询中的所有字段都是索引的一部分,并且
  • 结果中返回的所有字段都在同一索引中。

https://docs.mongodb.com/manual/core/query-optimization/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM