日期:2014-05-16  浏览次数:20497 次

MONGODB地理位置索引的问题

? ? 最近公司一个项目,要做一个现在比较流行的附近的人的系统, 我在测试mongodb性能的时候发现了一个奇怪的问题。

同样的数据量,同样的索引,不同的经纬度,查询到的时间差别相差却很大。

?

mongo数据量大概在(70W):

?> db.userLastLocation.count();

774632

执行计划如下:

1)中等

> db.userLastLocation.find({lonlat:{$near:[120,46.6236409]}}).limit(100).explain();?

{

? ? ? ? "cursor" : "GeoSearchCursor",

? ? ? ? "nscanned" : 100,

? ? ? ? "nscannedObjects" : 100,

? ? ? ? "n" : 100,

? ? ? ? "millis" : 102,

? ? ? ? "nYields" : 0,

? ? ? ? "nChunkSkips" : 0,

? ? ? ? "isMultiKey" : false,

? ? ? ? "indexOnly" : false,

? ? ? ? "indexBounds" : {

?

? ? ? ? }

}

2)慢的

> db.userLastLocation.find({lonlat:{$near:[104,46.6236409]}}).limit(100).explain();

{

? ? ? ? "cursor" : "GeoSearchCursor",

? ? ? ? "nscanned" : 100,

? ? ? ? "nscannedObjects" : 100,

? ? ? ? "n" : 100,

? ? ? ? "millis" : 2563,

? ? ? ? "nYields" : 0,

? ? ? ? "nChunkSkips" : 0,

? ? ? ? "isMultiKey" : false,

? ? ? ? "indexOnly" : false,

? ? ? ? "indexBounds" : {

?

? ? ? ? }

}

3)最快的

> db.userLastLocation.find({lonlat:{$near:[120,30]}}).limit(100).explain(); ? ? ? ??

{

? ? ? ? "cursor" : "GeoSearchCursor",

? ? ? ? "nscanned" : 100,

? ? ? ? "nscannedObjects" : 100,

? ? ? ? "n" : 100,

? ? ? ? "millis" : 4,

? ? ? ? "nYields" : 0,

? ? ? ? "nChunkSkips" : 0,

? ? ? ? "isMultiKey" : false,

? ? ? ? "indexOnly" : false,

? ? ? ? "indexBounds" : {

?

? ? ? ? }

}

最快的和最慢的性能差距600多倍。索引都是一样的,用到了lonlat" : "2d"这个索引

> db.system.indexes.find();

{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "lbs.userLastLocation", "name" : "_id_" }

{ "v" : 1, "key" : { "createTime" : -1, "appid" : 1 }, "ns" : "lbs.userLastLocation", "name" : "createTime_appid_idex", "dropDups" : false, "sparse" : false }

{ "v" : 1, "key" : { "lonlat" : "2d" }, "ns" : "lbs.userLastLocation", "name" : "lonlat", "min" : -180, "max" : 180 }

?

求指导,求原因

?