索引

时间:2017-10-10 08:13:30

标签: mongodb mongodb-query

我在64 GB RAM的笔记本电脑上运行Community MongoDB 3.4.9。我有一个包含1200多万份文档的集合。每个文档至少包含Int64类型的fromto字段。 from - to是唯一的范围。没有重叠范围的文档。该集合有一个索引如下:

{ 
    "v" : NumberInt(1), 
    "unique" : true, 
    "key" : {
        "from" : NumberInt(1), 
        "to" : NumberInt(1)
    }, 
    "name" : "range", 
    "ns" : "db.location", 
    "background" : true
}

服务器/数据库空闲。没有客户。我一遍又一遍地运行下面的查询,我得到大约21秒的持续执行时间。

db.location.find({from:{$lte:NumberLong(3682093364)},to:{$gte:NumberLong(3682093364)}}).limit(1)

and条件的逆转对执行时间没有影响。 explain命令显示以下内容。

{ 
    "queryPlanner" : {
        "plannerVersion" : 1.0, 
        "namespace" : "db.location", 
        "indexFilterSet" : false, 
        "parsedQuery" : {
            "$and" : [
                {
                    "from" : {
                        "$lte" : NumberLong(3682093364)
                    }
                }, 
                {
                    "to" : {
                        "$gte" : NumberLong(3682093364)
                    }
                }
            ]
        }, 
        "winningPlan" : {
            "stage" : "LIMIT", 
            "limitAmount" : 1.0, 
            "inputStage" : {
                "stage" : "FETCH", 
                "inputStage" : {
                    "stage" : "IXSCAN", 
                    "keyPattern" : {
                        "from" : 1.0, 
                        "to" : 1.0
                    }, 
                    "indexName" : "range", 
                    "isMultiKey" : false, 
                    "multiKeyPaths" : {
                        "from" : [

                        ], 
                        "to" : [

                        ]
                    }, 
                    "isUnique" : true, 
                    "isSparse" : false, 
                    "isPartial" : false, 
                    "indexVersion" : 1.0, 
                    "direction" : "forward", 
                    "indexBounds" : {
                        "from" : [
                            "[-inf.0, 3682093364]"
                        ], 
                        "to" : [
                            "[3682093364, inf.0]"
                        ]
                    }
                }
            }
        }, 
        "rejectedPlans" : [

        ]
    }, 
    "executionStats" : {
        "executionSuccess" : true, 
        "nReturned" : 1.0, 
        "executionTimeMillis" : 21526.0, 
        "totalKeysExamined" : 12284007.0, 
        "totalDocsExamined" : 1.0, 
        "executionStages" : {
            "stage" : "LIMIT", 
            "nReturned" : 1.0, 
            "executionTimeMillisEstimate" : 20945.0, 
            "works" : 12284008.0, 
            "advanced" : 1.0, 
            "needTime" : 12284006.0, 
            "needYield" : 0.0, 
            "saveState" : 96299.0, 
            "restoreState" : 96299.0, 
            "isEOF" : 1.0, 
            "invalidates" : 0.0, 
            "limitAmount" : 1.0, 
            "inputStage" : {
                "stage" : "FETCH", 
                "nReturned" : 1.0, 
                "executionTimeMillisEstimate" : 20714.0, 
                "works" : 12284007.0, 
                "advanced" : 1.0, 
                "needTime" : 12284006.0, 
                "needYield" : 0.0, 
                "saveState" : 96299.0, 
                "restoreState" : 96299.0, 
                "isEOF" : 0.0, 
                "invalidates" : 0.0, 
                "docsExamined" : 1.0, 
                "alreadyHasObj" : 0.0, 
                "inputStage" : {
                    "stage" : "IXSCAN", 
                    "nReturned" : 1.0, 
                    "executionTimeMillisEstimate" : 20357.0, 
                    "works" : 12284007.0, 
                    "advanced" : 1.0, 
                    "needTime" : 12284006.0, 
                    "needYield" : 0.0, 
                    "saveState" : 96299.0, 
                    "restoreState" : 96299.0, 
                    "isEOF" : 0.0, 
                    "invalidates" : 0.0, 
                    "keyPattern" : {
                        "from" : 1.0, 
                        "to" : 1.0
                    }, 
                    "indexName" : "range", 
                    "isMultiKey" : false, 
                    "multiKeyPaths" : {
                        "from" : [

                        ], 
                        "to" : [

                        ]
                    }, 
                    "isUnique" : true, 
                    "isSparse" : false, 
                    "isPartial" : false, 
                    "indexVersion" : 1.0, 
                    "direction" : "forward", 
                    "indexBounds" : {
                        "from" : [
                            "[-inf.0, 3682093364]"
                        ], 
                        "to" : [
                            "[3682093364, inf.0]"
                        ]
                    }, 
                    "keysExamined" : 12284007.0, 
                    "seeks" : 12284007.0, 
                    "dupsTested" : 0.0, 
                    "dupsDropped" : 0.0, 
                    "seenInvalidated" : 0.0
                }
            }
        }, 
        "allPlansExecution" : [

        ]
    }, 
    "serverInfo" : {
        "host" : "LAPTOP-Q96TVSN8", 
        "port" : 27017.0, 
        "version" : "3.4.9", 
        "gitVersion" : "876ebee8c7dd0e2d992f36a848ff4dc50ee6603e"
    }, 
    "ok" : 1.0
}

提供hint并没有什么不同。 explain似乎表明正在使用正确(且唯一)的索引,但大多数执行时间(20秒)都花费在IXSCAN中。 MongoDB日志显示扫描了许多索引项,但只触摸并返回了一个文档。考虑到数据库上存在ZERO并发操作,它还显示了大量的锁和产量。 SSD磁盘上的底层引擎为wiredTiger。 MongoDB RAM的使用量为7 GB。

2017-10-10T10:06:14.456+0200 I COMMAND  [conn33] command db.location appName: "MongoDB Shell" command: explain { explain: { find: "location", filter: { from: { $lte: 3682093364 }, to: { $gte: 3682093364 } }, limit: 1.0, singleBatch: false }, verbosity: "allPlansExecution" } numYields:96299 reslen:1944 locks:{ Global: { acquireCount: { r: 192600 } }, Database: { acquireCount: { r: 96300 } }, Collection: { acquireCount: { r: 96300 } } } protocol:op_command 21526ms

考虑到我的范围从不重叠,是否有更好的方法来构建文档以便查找更快?有什么明显的东西我做错了吗?

更新:

当我删除索引时,使用COLLSCAN并且文档一致发现8-9秒。

1 个答案:

答案 0 :(得分:1)

我不想回答我自己的问题,但我很高兴找到解决方案。

尽管创建这样的复合索引是有意义的,但考虑到非重叠范围的细节,事实证明搜索范围过于宽泛。输入数越高,查找结果所需的时间越长,越来越多的索引条目满足from <= number,搜索范围中的最后结果实际上是我们要查找的结果(索引是从从左到右)。

解决方案是将索引修改为{ from: -1 }{ to: 1 }。在这种情况下,复合索引实际上不是必需的,因为范围不重叠,索引找到的第一个文档就是返回的文档。现在就像预期的那样闪电般快速。

你生活和学习......

相关问题