Skip to content

Commit

Permalink
Remove workaround for inefficient query planning
Browse files Browse the repository at this point in the history
We have migrated MapIt to use Postgres 9.6 and PostGIS 2.3 now; Matthew
Somerville investigated whether this sub-optimal query planning still
happens with those versions, but it doesn't seem to. The query time is
considerably faster as well. So, we believe that this workaround should
no longer be necessary, but this commit should revert cleanly if in
practice we discover the problem is still present with some other
variant of these queries or data.
  • Loading branch information
mhl committed Oct 26, 2017
1 parent 90206c7 commit 4210423
Showing 1 changed file with 6 additions and 24 deletions.
30 changes: 6 additions & 24 deletions mapit/views/areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from django.shortcuts import redirect, render
from django.views.decorators.csrf import csrf_exempt

from mapit.models import Area, Generation, Geometry, Code, Name
from mapit.models import Area, Generation, Code, Name
from mapit.shortcuts import output_json, output_html, output_polygon, get_object_or_404, set_timeout
from mapit.middleware import ViewException
from mapit.ratelimitcache import ratelimit
Expand Down Expand Up @@ -363,30 +363,12 @@ def areas_by_point(request, srid, x, y, bb=False, format='json'):
method = 'box' if bb and bb != 'polygon' else 'polygon'

args = query_args(request, format)
type = request.GET.get('type', '')
country = request.GET.get('country', '')

if (type or country) and method == 'polygon':
args = dict(("area__%s" % k, v) for k, v in args.items())
# So this is odd. It doesn't matter if you specify types, PostGIS will
# do the contains test on all the geometries matching the bounding-box
# index, even if it could be much quicker to filter some out first
# (ie. the EUR ones).
args['polygon__bbcontains'] = location
shapes = Geometry.objects.filter(**args).defer('polygon')
areas = []
for shape in shapes:
try:
areas.append(Area.objects.get(polygons__id=shape.id, polygons__polygon__contains=location))
except:
pass

if method == 'box':
args['polygons__polygon__bbcontains'] = location
else:
if method == 'box':
args['polygons__polygon__bbcontains'] = location
else:
geoms = list(Geometry.objects.filter(polygon__contains=location).defer('polygon'))
args['polygons__in'] = geoms
areas = Area.objects.filter(**args)
args['polygons__polygon__contains'] = location
areas = Area.objects.filter(**args)

return output_areas(
request, _('Areas covering the point ({0},{1})').format(x, y),
Expand Down

0 comments on commit 4210423

Please sign in to comment.