title: PostGis geospatial query slower than a customized python script
body: <p>I 'm pretty new in PostGis and I'm using it in order to perform geospatial query but it seems quite slow in returning the desired results.</p>
  
  <p>Before I used a python script that usually returns the results in  in more or less 5 seconds (searching over 1.2M of elements).</p>
  
  <p>In order to get this result in a faster way I moved the problem on postgis, but, as I just write before, it takes more then 20 seconds for the same job.</p>
  
  <p>More precisely each element is made by a point (lat lon) and a string (label for the point)</p>
  
  <p>I'm using a dockerized postgis (<a href="https://hub.docker.com/r/mdillon/postgis" rel="nofollow noreferrer">https://hub.docker.com/r/mdillon/postgis</a>) on my I7 16 gb ram (ubuntu 18.04)</p>
  
  <p>I created the db in the following way:</p>
  
  <pre><code>CREATE DATABASE demo;
  \c demo
  create extension postgis;
  CREATE TABLE mypoints ( id serial primary key, name varchar(50), the_geom geometry(POINT,4326) );
  </code></pre>
  
  <p>and inserted the points (1,2M), by means of a python script, in this way</p>
  
  <pre><code>INSERT INTO cities (the_geom, name) VALUES (ST_GeomFromText('POINT(-3.782 40.4351)',4326), 'point_label');
  </code></pre>
  
  <p>The query I used is:</p>
  
  <pre><code>select name from cities where ST_Distance_Sphere(the_geom,ST_GeomFromText('POINT(-3.713 40.4321)',4326))&lt;500;
  </code></pre>
  
  <p>Am I doing something wrong?
  How is it possible that my python code is faster than an query optimized on geospatial problem?</p>