假設我有以下的數據模型如何高效是它在GeoDjango內置距離(整個表)訂購
Person(models.Model):
id = models.BigAutoField(primary_key=True)
name = models.CharField(max_length=50)
location = models.PointField(srid=4326)
假設也說我有一個應用程序,使得查詢此Django的後端,而唯一這個應用程序的目的是從最近到最遠返回一個(分頁)註冊用戶列表。
目前,我有這個疑問在腦海:
# here we are obtaining all users in ordered form
current_location = me.location
people = Person.objects.distance(current_location).order_by('distance')
# here we are obtaining the first X through pagination
start_index = a
end_index = b
people = people[a:b]
雖然這個工作,它是不一樣快,因爲我想。
我對這個查詢的速度有一些擔憂。如果表是大(100萬+),那麼就不會在數據庫(SQL Postgres的W/PostGIS的)具有執行對隨後一百萬行的order_by
之前測量數據庫current_location
每location
之間的距離?
有人可以建議如何正確地返回按距離排序附近的用戶以有效的方式?