﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28146	PostGIS: Django 1.11 adds two extra queries: SELECT postgis_lib_version() and SELECT version()	George Tantiras	nobody	"* Python 3.4.2
* PostgreSQL(9.4.10) 
* Database Extensions:
{{{
                                     List of installed extensions
  Name   | Version |   Schema   |                             Description                             
---------+---------+------------+---------------------------------------------------------------------
 plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language
 postgis | 2.1.4   | public     | PostGIS geometry, geography, and raster spatial types and functions
(2 rows)
}}}

models.py
{{{
from django.contrib.gis.db import models

class TheModel(models.Model):
    polygon_area = models.PolygonField()
    centroid = models.PointField(srid=4326)
}}}

The code needed to fetch the results:
{{{
>>> from my.models import TheModel
>>> from django.contrib.gis.geos import Point
>>> from django.contrib.gis.measure import D
>>> from django.contrib.gis.db.models.functions import Distance

>>> db.reset_queries()
>>> TheModel.objects.filter(centroid__distance_lte=(Point(23.12, 37.9), D(m=3000))).annotate(distance=Distance('centroid', Point(23.12, 37.9))).order_by('polygon_area'))
 ... <QuerySet []>
>>> print(db.connection.queries)
}}}
Django-1.11 result(4 Queries, total time 0.025):
{{{
{'time': '0.003', 'sql': 'SELECT ""spatial_ref_sys"".""srid"", ""spatial_ref_sys"".""auth_name"", ""spatial_ref_sys"".""auth_srid"", ""spatial_ref_sys"".""srtext"", ""spatial_ref_sys"".""proj4text"" FROM ""spatial_ref_sys"" WHERE ""spatial_ref_sys"".""srid"" = 4326'}
{'time': '0.001', 'sql': 'SELECT version()'}
{'time': '0.014', 'sql': 'SELECT postgis_lib_version()'}
{'time': '0.007', 'sql': 'SELECT ""my_themodel"".""id"", ""my_themodel"".""polygon_area"", ""my_themodel"".""centroid"", ST_distance_sphere(""my_themodel"".""centroid"", ST_GeomFromEWKB(\'\\x0101000020e61000001f85eb51b81e37403333333333f34240\'::bytea)) AS ""distance"" FROM ""my_themodel"" WHERE ST_distance_sphere(""my_themodel"".""centroid"", ST_GeomFromEWKB(\'\\x0101000020e61000001f85eb51b81e37403333333333f34240\'::bytea)) <= 3000.0 ORDER BY ""my_themodel"".""polygon_area"" ASC LIMIT 21'}
}}}

Django-1.10.7 result (2 queries, total time 0.01):
{{{
{'time': '0.003', 'sql': 'SELECT ""spatial_ref_sys"".""srid"", ""spatial_ref_sys"".""auth_name"", ""spatial_ref_sys"".""auth_srid"", ""spatial_ref_sys"".""srtext"", ""spatial_ref_sys"".""proj4text"" FROM ""spatial_ref_sys"" WHERE ""spatial_ref_sys"".""srid"" = 4326'}
{'time': '0.007', 'sql': 'SELECT ""my_themodel"".""id"", ""my_themodel"".""polygon_area"", ""my_themodel"".""centroid"", ST_distance_sphere(""my_themodel"".""centroid"", ST_GeomFromEWKB(\'\\x0101000020e61000001f85eb51b81e37403333333333f34240\'::bytea)) AS ""distance"" FROM ""my_themodel"" WHERE ST_distance_sphere(""my_themodel"".""centroid"", ST_GeomFromEWKB(\'\\x0101000020e61000001f85eb51b81e37403333333333f34240\'::bytea)) <= 3000.0 ORDER BY ""my_themodel"".""polygon_area"" ASC LIMIT 21'}
}}}

After upgrading to Django 1.11, Google spends around 600ms to download the page which was downloaded in 350ms using Django 1.10.7, as illustrated in the image below:

[[Image(https://yadi.sk/i/wWAup3MU3HRx9N)]]"	Cleanup/optimization	closed	GIS	1.11	Normal	invalid		Simon Charette	Unreviewed	0	0	0	0	0	0
