使用MongoDB查询地理空间索引非常慢

时间:2016-02-17 11:48:42

标签: mongodb

我使用mongodb存储大量带有2dsphere索引的文档(662430756)。但我的问题是非常缓慢。我使用的是12核Intel(R)Xeon(R)CPU E5-2643 v2 @ 3.50GHz和128 G RAM的专用服务器。我在MongoDB上使用带有XFS和WiredTiger的Ubuntu Server 14.04。

文件架构:

> db.las_prod.findOne()
{
        "_id" : ObjectId("56c301386d1b420a58c12534"),
        "pointsourceid" : 4083,
        "elevation" : 44.01,
        "numberofreturns" : 1,
        "gpstime" : 27779511.554264,
        "returnnumber" : 1,
        "red" : 0,
        "scananglerank" : -12,
        "userdata" : 3,
        "geometry" : {
                "coordinates" : [
                        -0.847718518,
                        44.855975882
                ],
                "type" : "Point"
        },
        "edgeofflightline" : 0,
        "classification" : 1,
        "blue" : 0,
        "intensity" : 33,
        "green" : 0,
        "scandirectionflag" : 0
}

我的索引:

> db.las_prod.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "lidar.las_prod"
        },
        {
                "v" : 1,
                "key" : {
                        "geometry" : "2dsphere"
                },
                "name" : "geometry_2dsphere",
                "ns" : "lidar.las_prod",
                "2dsphereIndexVersion" : 3
        }
]

总索引大小:

> db.las_prod.totalIndexSize()
15326851072

例如:

查询

db.las_prod.find({geometry:{$geoWithin:{$geometry:{type:"Polygon",coordinates:[[[-0.88,44.87],[-0.86,44.87],[-0.86,44.84],[-0.88,44.84],[-0.88,44.87]]]}}}}).explain("executionStats")

结果

{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "lidar.las_prod",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "geometry" : {
                                "$geoWithin" : {
                                        "$geometry" : {
                                                "type" : "Polygon",
                                                "coordinates" : [
                                                        [
                                                                [
                                                                        -0.88,
                                                                        44.87
                                                                ],
                                                                [
                                                                        -0.86,
                                                                        44.87
                                                                ],
                                                                [
                                                                        -0.86,
                                                                        44.84
                                                                ],
                                                                [
                                                                        -0.88,
                                                                        44.84
                                                                ],
                                                                [
                                                                        -0.88,
                                                                        44.87
                                                                ]
                                                        ]
                                                ]
                                        }
                                }
                        }
                },
                "winningPlan" : {
                        "stage" : "FETCH",
                        "filter" : {
                                "geometry" : {
                                        "$geoWithin" : {
                                                "$geometry" : {
                                                        "type" : "Polygon",
                                                        "coordinates" : [
                                                                [
                                                                        [
                                                                                -0.88,
                                                                                44.87
                                                                        ],
                                                                        [
                                                                                -0.86,
                                                                                44.87
                                                                        ],
                                                                        [
                                                                                -0.86,
                                                                                44.84
                                                                        ],
                                                                        [
                                                                                -0.88,
                                                                                44.84
                                                                        ],
                                                                        [
                                                                                -0.88,
                                                                                44.87
                                                                        ]
                                                                ]
                                                        ]
                                                }
                                        }
                                }
                        },
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                        "geometry" : "2dsphere"
                                },
                                "indexName" : "geometry_2dsphere",
                                "isMultiKey" : false,
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 1,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "geometry" : [
                                                "[936748722493063168, 936748722493063168]",
                                                "[954763121002545152, 954763121002545152]",
                                                "[959266720629915648, 959266720629915648]",
                                                "[960392620536758272, 960392620536758272]",
                                                "[960603726769291264, 960603726769291264]",
                                                "[960608124815802368, 960608124815802368]",
                                                "[960610495637749761, 960610529997488127]",
                                                "[960610529997488128, 960610529997488128]",
                                                "[960610598716964864, 960610598716964864]",
                                                "[960611423350685696, 960611423350685696]",
                                                "[960612247984406528, 960612247984406528]",
                                                "[960612316703883264, 960612316703883264]",
                                                "[960612316703883265, 960612351063621631]",
                                                "[960612351063621633, 960612359653556223]",
                                                "[960612359653556225, 960612368243490815]",
                                                "[960612368243490816, 960612368243490816]",
                                                "[960612797740220416, 960612797740220416]",
                                                "[960612806330155009, 960612814920089599]",
                                                "[960612814920089600, 960612814920089600]",
                                                "[960612866459697152, 960612866459697152]",
                                                "[960612917999304704, 960612917999304704]",
                                                "[960612917999304705, 960612926589239295]",
                                                "[960612926589239297, 960612935179173887]",
                                                "[960612935179173889, 960612969538912255]",
                                                "[960612969538912257, 960612978128846847]",
                                                "[960612986718781440, 960612986718781440]",
                                                "[960612995308716033, 960613003898650623]",
                                                "[960613003898650624, 960613003898650624]",
                                                "[960613003898650625, 960613012488585215]",
                                                "[960613021078519808, 960613021078519808]",
                                                "[960613029668454401, 960613038258388991]",
                                                "[960613038258388993, 960613072618127359]",
                                                "[960613072618127361, 960613106977865727]",
                                                "[960613106977865729, 960613141337604095]",
                                                "[960613141337604096, 960613141337604096]",
                                                "[960613347496034304, 960613347496034304]",
                                                "[960613553654464512, 960613553654464512]",
                                                "[960613562244399105, 960613570834333695]",
                                                "[960613570834333696, 960613570834333696]",
                                                "[960613570834333697, 960613579424268287]",
                                                "[960613622373941248, 960613622373941248]",
                                                "[960616920908824576, 960616920908824576]",
                                                "[960621318955335680, 960621318955335680]",
                                                "[960674095513468928, 960674095513468928]"
                                        ]
                                }
                        }
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 13776129,
                "executionTimeMillis" : 93540,
                "totalKeysExamined" : 16923284,
                "totalDocsExamined" : 16923267,
                "executionStages" : {
                        "stage" : "FETCH",
                        "filter" : {
                                "geometry" : {
                                        "$geoWithin" : {
                                                "$geometry" : {
                                                        "type" : "Polygon",
                                                        "coordinates" : [
                                                                [
                                                                        [
                                                                                -0.88,
                                                                                44.87
                                                                        ],
                                                                        [
                                                                                -0.86,
                                                                                44.87
                                                                        ],
                                                                        [
                                                                                -0.86,
                                                                                44.84
                                                                        ],
                                                                        [
                                                                                -0.88,
                                                                                44.84
                                                                        ],
                                                                        [
                                                                                -0.88,
                                                                                44.87
                                                                        ]
                                                                ]
                                                        ]
                                                }
                                        }
                                }
                        },
                        "nReturned" : 13776129,
                        "executionTimeMillisEstimate" : 89200,
                        "works" : 16923285,
                        "advanced" : 13776129,
                        "needTime" : 3147155,
                        "needYield" : 0,
                        "saveState" : 132218,
                        "restoreState" : 132218,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "docsExamined" : 16923267,
                        "alreadyHasObj" : 0,
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "nReturned" : 16923267,
                                "executionTimeMillisEstimate" : 16230,
                                "works" : 16923285,
                                "advanced" : 16923267,
                                "needTime" : 17,
                                "needYield" : 0,
                                "saveState" : 132218,
                                "restoreState" : 132218,
                                "isEOF" : 1,
                                "invalidates" : 0,
                                "keyPattern" : {
                                        "geometry" : "2dsphere"
                                },
                                "indexName" : "geometry_2dsphere",
                                "isMultiKey" : false,
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 1,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "geometry" : [
                                                "[936748722493063168, 936748722493063168]",
                                                "[954763121002545152, 954763121002545152]",
                                                "[959266720629915648, 959266720629915648]",
                                                "[960392620536758272, 960392620536758272]",
                                                "[960603726769291264, 960603726769291264]",
                                                "[960608124815802368, 960608124815802368]",
                                                "[960610495637749761, 960610529997488127]",
                                                "[960610529997488128, 960610529997488128]",
                                                "[960610598716964864, 960610598716964864]",
                                                "[960611423350685696, 960611423350685696]",
                                                "[960612247984406528, 960612247984406528]",
                                                "[960612316703883264, 960612316703883264]",
                                                "[960612316703883265, 960612351063621631]",
                                                "[960612351063621633, 960612359653556223]",
                                                "[960612359653556225, 960612368243490815]",
                                                "[960612368243490816, 960612368243490816]",
                                                "[960612797740220416, 960612797740220416]",
                                                "[960612806330155009, 960612814920089599]",
                                                "[960612814920089600, 960612814920089600]",
                                                "[960612866459697152, 960612866459697152]",
                                                "[960612917999304704, 960612917999304704]",
                                                "[960612917999304705, 960612926589239295]",
                                                "[960612926589239297, 960612935179173887]",
                                                "[960612935179173889, 960612969538912255]",
                                                "[960612969538912257, 960612978128846847]",
                                                "[960612986718781440, 960612986718781440]",
                                                "[960612995308716033, 960613003898650623]",
                                                "[960613003898650624, 960613003898650624]",
                                                "[960613003898650625, 960613012488585215]",
                                                "[960613021078519808, 960613021078519808]",
                                                "[960613029668454401, 960613038258388991]",
                                                "[960613038258388993, 960613072618127359]",
                                                "[960613072618127361, 960613106977865727]",
                                                "[960613106977865729, 960613141337604095]",
                                                "[960613141337604096, 960613141337604096]",
                                                "[960613347496034304, 960613347496034304]",
                                                "[960613553654464512, 960613553654464512]",
                                                "[960613562244399105, 960613570834333695]",
                                                "[960613570834333696, 960613570834333696]",
                                                "[960613570834333697, 960613579424268287]",
                                                "[960613622373941248, 960613622373941248]",
                                                "[960616920908824576, 960616920908824576]",
                                                "[960621318955335680, 960621318955335680]",
                                                "[960674095513468928, 960674095513468928]"
                                        ]
                                },
                                "keysExamined" : 16923284,
                                "dupsTested" : 0,
                                "dupsDropped" : 0,
                                "seenInvalidated" : 0
                        }
                }
        },
        "serverInfo" : {
                "host" : "MDB01",
                "port" : 27017,
                "version" : "3.2.1",
                "gitVersion" : "a14d55980c2cdc565d4704a7e3ad37e4e535c1b2"
        },
        "ok" : 1
}

我不明白为什么在FETCH和IXSCAN执行时之间有72970 ms的差异和总共93540 ms!

我的方法存在一些问题?或者MongoDB对地理空间索引效率不高?为什么需要这么长时间?

0 个答案:

没有答案