【发布时间】:2016-11-05 23:04:29
【问题描述】:
我的数据库中有大约 300 万份文档。我有这个查询来获取我的文档中包含的最小和最大纬度和经度,因为在我的应用程序中,我想放大一个正方形中包含的现有数据。 执行大约需要 16 秒:
正方形代表4个坐标。 tMin 和 tMax 是我的时间间隔(日期)。
cursor = db.collection.aggregate([
{
"$match":
{
"nodeLoc":{"$geoWithin":{"$geometry":square}}, "t": {"$gt": tMin, "$lt": tMax}
}
},
{
"$group":
{
"_id": {},
"minLat": {"$min": {"$arrayElemAt": [ "$nodeLoc.coordinates", 1]}},
"maxLat": {"$max": {"$arrayElemAt": [ "$nodeLoc.coordinates", 1]}},
"minLon": {"$min": {"$arrayElemAt": [ "$nodeLoc.coordinates", 0]}},
"maxLon": {"$max": {"$arrayElemAt": [ "$nodeLoc.coordinates", 0]}}
}
}
]
)
有没有办法优化 $group 或 $match 阶段? 我已经在 nodeLoc (2dsphere) 和 t 上创建了一个复合索引,但我没有看到任何改进。
编辑:
我删除了索引,但执行查询的时间保持不变。
【问题讨论】:
标签: mongodb optimization query-optimization pymongo