【问题标题】:Slow output of pymongo.cursor for geosearch用于地理搜索的 pymongo.cursor 输出缓慢
【发布时间】:2020-03-17 15:13:58
【问题描述】:

我对一个充满多边形的集合执行地理搜索。查询本身非常快,但需要 3 多分钟才能得到结果。我需要一个解决方案来加快多边形名称的输出。

我尝试只包含名称,排除几何形状,根本没有指定任何内容,但效果相同。

输出:

0:00:00.000061
Result of Query
<class 'pymongo.cursor.Cursor'>
{'_id': ObjectId('5e70ab3b160bdb5ebde5da7f'), 'name': 'Hessen'}
{'_id': ObjectId('5e70ab65160bdb5ebde5e0d4'), 'name': 'Hessen/Rheingau'}
{'_id': ObjectId('5e70ab7a160bdb5ebde5e38e'), 'name': 'Hessen'}
{'_id': ObjectId('5e70b707faa4a158f866f6fb'), 'name': 'Frankfurt am Main'}
0:03:11.020437
from pymongo import MongoClient
import json
from datetime import datetime

client = MongoClient()
client = MongoClient('localhost', 27017)
db = client['GEO']
geo_collection = db['polygons']

starttime = datetime.now()
search_results = geo_collection.find({"geometry":
                 {"$geoIntersects":
                     {"$geometry":{ "type" : "Point",
                          "coordinates" :  [8.636274, 50.145938]}
                      }
                  }
             },{"geometry":0})

endtime = datetime.now()
print(endtime-starttime)

print("Result of Query")
print(type(search_results))
for result in search_results:
    print(result)

endtime = datetime.now()
print(endtime-starttime)
> db.polygons.explain().find({"geometry":{"$geoIntersects":{"$geometry":{ "type" : "Point","coordinates":[8.636274, 50.145938]}}}},{"name":1})
{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "GEO.polygons",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "geometry" : {
                "$geoIntersects" : {
                    "$geometry" : {
                        "type" : "Point",
                        "coordinates" : [
                            8.636274,
                            50.145938
                        ]
                    }
                }
            }
        },
        "queryHash" : "D3B4DDC2",
        "planCacheKey" : "45C3B08C",
        "winningPlan" : {
            "stage" : "PROJECTION_SIMPLE",
            "transformBy" : {
                "name" : 1
            },
            "inputStage" : {
                "stage" : "COLLSCAN",
                "filter" : {
                    "geometry" : {
                        "$geoIntersects" : {
                            "$geometry" : {
                                "type" : "Point",
                                "coordinates" : [
                                    8.636274,
                                    50.145938
                                ]
                            }
                        }
                    }
                },
                "direction" : "forward"
            }
        },
        "rejectedPlans" : [ ]
    },
    "serverInfo" : {
        "host" : "DTO-MB-007.local",
        "port" : 27017,
        "version" : "4.2.3",
        "gitVersion" : "6874650b362138df74be53d366bbefc321ea32d4"
    },
    "ok" : 1
}

【问题讨论】:

  • 你有关于该数据的二维索引吗?此查询是集合扫描,因此集合中的每个文档都从磁盘加载并检查。您首先报告的时间就是设置光标所需的时间,并且可能获得第一批。驱动程序会根据需要自动进行尽可能多的调用以获取更多批次。
  • mogo 中的数据如下所示: { "_id" : ObjectId("5e70ab35160bdb5ebde5d554"), "name" : "Monaco", "geometry" : { "type" : "Polygon", "坐标”:[ [ [ 7.414266, 43.727287 ], [ 7.417669, 43.724243 ],... ] ] } } 我添加了索引: db.polygons.createIndex({"geometry.coordinates":"2d"}) 但是它不会加快查询速度

标签: python python-3.x mongodb polygon


【解决方案1】:

“2d”索引支持传统的长/纬度坐标对,而不是像 Polygon 这样的 GeoJSON 对象。

2dsphere index 确实支持这些。 geometry 字段似乎是格式正确的 GeoJSON 对象,因此您应该能够创建如下索引:

db.polygons.createIndex({"geometry":"2dsphere"})

该索引应支持所注明的查询。

【讨论】:

  • 索引不起作用,知道吗? > db.polygons.createIndex({geometry:"2dsphere"}) { "ok" : 0, "errmsg" : "无法提取地理键: { _id: ObjectId('5e70ab77160bdb5ebde5e2b1'), name: \"Emirat Abu Dhabi\",几何:{ 类型:\"MultiPolygon\",坐标:[ [ [ [ 54.478299693, 24.411246705 ],
猜你喜欢
  • 2012-08-06
  • 2012-02-02
  • 1970-01-01
  • 2011-07-05
  • 2014-06-10
  • 1970-01-01
  • 1970-01-01
  • 2015-01-12
  • 1970-01-01
相关资源
最近更新 更多