我的问题是一般性的,因为我正在寻找有关在 PostgreSQL 上使用具有以下特征的查询的最佳方式的建议:
- 客户端间隔 5 秒
- 每个查询结果超过 5000 条记录
- 结果集是四个连接表
- 结果集将显示在谷歌地图上
与使用简单的 SQL 或存储过程相比,视图很可能会更好。目前我正在使用一个常见的 AJAX 请求,我的 AJAX 代码是:
def request_positions(request):
try:
vehicles = Vehicle.objects.filter(registration__in = simplejson.loads(request.GET['vehicles']))
except MultiValueDictKeyError:
vehicles = Vehicle.objects.all()
points = []
for vehicle in vehicles:
point = {
'vehicle' : vehicle.registration,
'imei': None,
'latitude': None,
'longitude':None,
'course': None,
'speed': None,
'mileage': None,
'gps_signal': None,
'gsm_signal': None,
'alarm_status': None,
'gps_satus': None,
'vehicle_status': None,
'alarm_over_speed': None,
'other': None,
'date_time_process': None,
}
try:
vehicledevice = vehicle.vehicledevice_set.get(is_joined__exact = True)
try:
track = vehicledevice.device.devicetrack_set.exclude(date_time_process__isnull = True).latest('date_time_process')
point['imei'] = vehicledevice.device.imei
point['latitude'] = track.latitude
point['longitude'] =track.longitude
point['course'] = track.course
point['speed'] = track.speed
point['mileage'] = track.mileage
point['gps_signal'] = track.gps_signal
point['gsm_signal'] = track.gsm_signal
point['alarm_status'] = track.alarm_status
point['gps_status'] = track.gps_status
point['vehicle_status'] = track.vehicle_status
point['other'] = track.other
point['date_time_process'] = track.date_time_process.astimezone(tz.gettz()).strftime("%Y-%m-%d %H:%M:%S")
points.append(point)
except ObjectDoesNotExist:
pass
except ObjectDoesNotExist:
pass
json = simplejson.dumps(points)
return HttpResponse(json, mimetype='application/json')
我的应用程序是一个跟踪器,它每 10 秒存储一次数据,大约 5000 个跟踪器使用一个扭曲的守护进程。然后,我需要一种在客户端无需额外加载的情况下快速获得答案的方法,如您所见,我正在使用常见的 django 定义,但我也希望尝试优化此查询以减少请求和回答时间.另外,网络和服务器的费用,我的意思是,为了获得最佳过程的最佳方式。