简体   繁体   English

Mongodb 2.4 2dsphere查询非常慢

[英]Mongodb 2.4 2dsphere queries very slow

I have converted my old collection using mongodb "2d" index to a collection having geojson specification "2dsphere" index. 我已经将使用mongodb“ 2d”索引的旧集合转换为具有geojson规范“ 2dsphere”索引的集合。 The problem is that the query is taking about 11 second to execute on collection of about 2 lac objects. 问题是查询要花费大约11秒钟才能对大约2个lac对象的集合执行。 Previously is was taking about 100 ms for query. 以前大约要花100毫秒进行查询。 My document is as follow. 我的文件如下。

{ "_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 } } }

My explain query output is given below. 我的解释查询输出如下。

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" : "..."
}

Please let me know how can I correct the problem and reduce the query time. 请让我知道如何解决该问题并减少查询时间。

The $near command does not require $maxDistance argument for "2dsphere" databases as you suggest. 如您建议的那样,对于“ 2dsphere”数据库,$ near命令不需要$ maxDistance参数。 Adding $maxDistance just specified a range that reduced the number of query results to a manageable number. 添加$ maxDistance只是指定了一个范围,该范围将查询结果的数量减少为可管理的数量。 The reason for the difference in your experience changing from "2d" to "2dsphere" style indexes is that "2d" indexes impose a default limit of 100 if none is specified. 您的体验从“ 2d”样式索引更改为“ 2dsphere”样式索引的原因在于,如果未指定任何索引,则“ 2d”索引将默认限制为100。 As you can see, the default query plan for 2dsphere indexes does not impose such limit so the query is scanning the entire index ("nscannedObjects" : 172980). 如您所见,默认的2dsphere索引查询计划没有施加此限制,因此查询正在扫描整个索引(“ nscannedObjects”:172980)。 If you ran the same query on a "2d" index you would see "n" and "nscannedObjects" are only 100 which explains the cost discrepancy. 如果在“ 2d”索引上运行相同的查询,您将看到“ n”和“ nscannedObjects”仅为100,这说明了成本差异。

If all of your items were within the $maxDistance range (try it with $maxDistance 20M meters, for instance), you will see the query performance degrade back to where it was without it. 如果所有项目都在$ maxDistance范围内(例如,在$ maxDistance 20M米的情况下尝试使用),您将看到查询性能下降到没有它的情况。 In either case, it is very important to use limit() to tell the query plan to only scan the necessary results within the index to prevent runaways, especially with larger data sets. 无论哪种情况,使用limit()告诉查询计划仅扫描索引内的必要结果以防止失控(尤其是对于较大的数据集)而言,都是非常重要的。

I have solved the problem. 我已经解决了问题。 The $near command requires $maxDistance argument as specified here: http://docs.mongodb.org/manual/applications/2dsphere/ . $ near命令需要$ maxDistance参数,如此处指定: http : //docs.mongodb.org/manual/applications/2dsphere/ As soon as I supplied $maxDistance, the query time reduced to less than 100 ms. 一旦提供$ maxDistance,查询时间就会减少到不到100毫秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM