0
我正在使用的PostGIS以下近鄰查詢與燒瓶的SQLAlchemy:如何在給定距離內優化postgis中的最近鄰居查詢?
def get_neighbor():
pt = WKTElement('POINT(%s %s)' % (longitude, latitude), srid=srid)
# Get total count of neighbor with distance less than 1000m for example.
total_count = db.session.query(User, func.ST_Distance(User.geom, pt)). \
filter(func.ST_DWith(User.geom, pt, 1000).count()
if total_count > 0:
result_list = db.session.query(User, func_ST_Distance(User.geom, pt)). \
filter(func.ST_DWithin(User.geom, pt, 1000).all()
return jsonify({'total_count': total_count, 'result_list':result_list})
else
return jsonify({'total_count': total_count})
它的工作原理其實很好,但我查詢TOTAL_COUNT和result_list時注意到了同樣的查詢語句將被執行兩次。
- 如何優化重複查詢問題TOTAL_COUNT和result_list?
- 當用戶表包含數千行時,查詢會將用戶表中的每一行與給定點進行比較嗎?如果是這樣,這將是緩慢的表現,我該如何解決這個問題?
任何意見將不勝感激!
謝謝!
謝謝麥克,我會試試看。 :) – Raindrop