My question is something in general as I am looking for a suggestion on the best way to work with a query on PostgreSQL with the characteristics:
- Interval from client side of 5 seconds
- More than 5000 records on each query result
- Result set is four joined tables
- Result set will be displayed on google map
Most likely a view could be better instead of using simple SQL or maybe a store procedure. At the moment I am using a common AJAX request, and my AJAX code is:
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')
My app is for a tracker which stores data each 10 seconds, and about 5000 trackers using a twisted daemon. Then, I need a way to get fast answers with no extra loading on the client side, as you could see, I am using common django definitions, but I am also looking to try and optimize this query in order to reduce request and answer time.Also, a charge of network and server, I mean, the best way in order to get the best process.