Why is PostGIS query not using spatial index?

Your first query is using an index just not the spatial one. See the Index Scan using geoplanet_place_pkey. So it's more efficient for it to use the id key since you are doing an ORDER by the column and your spatial filter covers the whole table.

The spatial index is not used because your ST_Expand is too big. You have a geometry but its in long lat. The expand units are therefore in long lat. My god I can't fathom how big 16093.44 degrees is. that would cover your whole table and then some so therefore the index is useless and Postgres is smart enough to realize that.

If you are going to do this use geography data type and then your 16093.44 would be in meters and valid and you wouldn't need that ST_Spheriod call.

since the ST_Dwithin geograpy call would take care of everything.

e.g.


CREATE TABLE geoplanet_place
(
  woeid integer NOT NULL,
  "name" character varying(300) NOT NULL,
  admin_1 character varying(300),
  coords geography(POINT,4326) NOT NULL,
  bbox geography(POLYGON,4326) NOT NULL
)

and then instead of ST_GeomFromText use ST_GeogFromText


I had a similar problem, but with a different solution: I simply needed to ANALYZE the table in question. This was following a fresh restore of the table on a new database, so the query planner did not have statistics and so it didn't know it should use the index and defaulted to a sequential scan instead.