Reputation: 215
I need to optimize this query, 'coz execution time is too long. I've built gist indexes on geom(point), but they are not used unfortunaly.
select *, cast(st_distance_sphere(city_gis.point, tmp1.pnt) as int) as distance
from tmp1
inner join city_gis
on cast(st_distance_sphere(city_gis.point,tmp1.pnt) as int) between 0 and 60000
Here my query and execution plan
Upvotes: 0
Views: 913
Reputation: 9131
ST_DISTANCE
functions do not use a geometry index. You should use e.g. ST_DWITHIN
so your query would be something like
select *, cast(st_distance_sphere(city_gis.point, tmp1.pnt) as int) as distance
from tmp1
inner join city_gis on st_dwithin(city_gis.point,tmp1.pnt,60000)
Here is the documentation of this function.
https://postgis.net/docs/ST_DWithin.html
Upvotes: 1
Reputation: 43632
If you have a table called city_gis
, I'm going to assume that this is a table of GIS data for a city. Is it one city, or more? If it is geographically isolated, make life much easier by using a projected coordinate system for the region. You can always transform coordinates back to lat/long for other uses. By using a projected coordinate system, any distance-related functions will use the projected units. For example, when using a UTM coordinate system, the distance units are always in metres. Obviously, this wouldn't work if your table indeed contains data from different cities across the world, or even in one continent.
Upvotes: 0