假设我有以下的数据模型如何高效是它在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
之间的距离?
有人可以建议如何正确地返回按距离排序附近的用户以有效的方式?