Mongodb 2.4 2dsphere查询非常慢(使用$ geoIntersects)?

时间:2013-08-03 10:49:48

标签: mongodb indexing geospatial

mongod.log显示:

 {deliver_area: { $geoIntersects:
     { $geometry: { 
         type: "Point", 
         coordinates: [ 116.3426399230957, 39.95959281921387 ] 
     } } 
 } }

 ntoreturn:0 
 ntoskip:0
 nscanned:2965
 keyUpdates:0
 numYields: 2 locks(micros)
 r:136723
 nreturned:52
 reslen:23453
 103ms

该集合有大约10k条记录,其中deliver_area是多边形(GeoJSON)并且2dsphere索引

的字段之一

这是我的疑问:

db.area_coll.find( { 
    id: 59, 
    deliver_area: { 
        $geoIntersects: { 
            $geometry: { 
                type: "Point", 
                coordinates: [ 116.3175773620605, 39.97607231140137 ] 
            } 
        } 
    } 
})

解释结果:

{
    "cursor" : "S2Cursor",
    "isMultiKey" : true,
    "n" : 0,
    "nscannedObjects" : 0,
    "nscanned" : 3887,
    "nscannedObjectsAllPlans" : 0,
    "nscannedAllPlans" : 3887,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 5,
    "indexBounds" : {
    },
    "nscanned" : 3887,
    "matchTested" : NumberLong(666),
    "geoTested" : NumberLong(0),
    "cellsInCover" : NumberLong(1),
    "server" : "testing:27017"
}

1 个答案:

答案 0 :(得分:1)

日志中的查询与您运行的查询不匹配,位置不同:

[ 116.3426399230957, 39.95959281921387 ]
[ 116.3175773620605, 39.97607231140137 ]

我也认为您没有复制整个日志行,因为它只提到area而不是deliver_area

但是,它们并非非常慢。在第一种情况下,花了103毫秒,在某些情况下可能会发生,因为您的服务器正在执行其他IO。 explain()输出告诉您,第二个查询花了5毫秒。

但最引人注目的是你的主要标准是id: 59。我不知道你的_id字段是什么,但是如果你在id设置一个索引,那么根本不需要使用2dsphere索引 - 除非你当然有许多文件id=59。在这种情况下,使用{ id: 1, deliver_area: '2dsphere' }上的复合键可能会更好。

相关问题