2013-03-28 94 views
1

我已经使用mongodb“2d”索引将我的旧集合转换为具有geojson规范“2dsphere”索引的集合。问题在于查询大约需要11秒才能收集大约2个lac对象。以前是需要大约100毫秒的查询。我的文档如下。Mongodb 2.4 2dsphere查询很慢

{ "_id": ObjectId("4f9c2aa2d142b9882f02a3b3"), "geonameId": NumberInt(1106542), "name": "Chitungwiza", "feature code": "PPL", "country code": "ZW", "state": "Harare Province", "population": NumberInt(340360), "elevation": "", "timezone": "Africa\/Harare", "geolocation": { "type": "Point", "coordinates": { "0": 31.07555, "1": -18.01274 } } }

我解释查询的输出如下。

db.city_info.find({"geolocation":{'$near':{ '$geometry': { 'type':"Point",coordinates:[73,23] } }}}).explain() 

{ 
"cursor" : "S2NearCursor", 
"isMultiKey" : true, 
"n" : 172980, 
"nscannedObjects" : 172980, 
"nscanned" : 1121804, 
"nscannedObjectsAllPlans" : 172980, 
"nscannedAllPlans" : 1121804, 
"scanAndOrder" : false, 
"indexOnly" : false, 
"nYields" : 13, 
"nChunkSkips" : 0, 
"millis" : 13841, 
"indexBounds" : { 

}, 
"nscanned" : 1121804, 
"matchTested" : NumberLong(191431), 
"geoMatchTested" : NumberLong(191431), 
"numShells" : NumberLong(373), 
"keyGeoSkip" : NumberLong(930373), 
"returnSkip" : NumberLong(933610), 
"btreeDups" : NumberLong(0), 
"inAnnulusTested" : NumberLong(191431), 
"server" : "..." 
} 

请让我知道如何解决问题并缩短查询时间。

回答

0

我已经解决了这个问题。 $ near命令需要$ maxDistance参数:http://docs.mongodb.org/manual/applications/2dsphere/。只要我提供$ maxDistance,查询时间就会缩短到小于100毫秒。

+0

$ near不需要$ maxDistance。您执行的查询已成功并正确地返回了您要求的内容。 – 2013-07-08 17:02:21

2

根据您的建议,$ near命令不需要$ 2dsphere数据库的$ maxDistance参数。添加$ maxDistance只是指定一个范围,将查询结果的数量减少到可管理的数量。如果您的体验从“2d”变为“2dsphere”样式索引的原因有所不同,那么如果没有指定“2d”索引,则默认限制为100。如您所见,2dsphere索引的默认查询计划不会强制实施此限制,因此查询正在扫描整个索引(“nscannedObjects”:172980)。如果您在“2d”索引上运行相同的查询,您会看到“n”和“nscannedObjects”仅为100,这说明了成本差异。

如果所有的项目都在$ maxDistance范围内(以$ maxDistance 20M米,例如尝试),你会看到查询性能下降回到它是离不开它。在任何一种情况下,使用limit()来告诉查询计划只扫描索引内的必要结果以防止失控,尤其是对于较大的数据集时非常重要。

+2

这是一个可以接受的答案,对吧?你有可能为我做这件事吗? – 2013-07-19 03:10:54

+0

关注为什么你低估了这个答案?这是正确的。 – 2014-06-10 15:11:35

+0

我意识到这是非常旧的,但只是添加,我运行一个2dsphere索引$ 2近似查询和2d索引,数据是完全一样的。两者都限制为100,但二维索引查询返回0.2秒,2dsphere索引查询需要10s + ..所以我不认为使用限制是正确的答案 – 2015-03-06 15:15:34