Pergunta

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

Foi útil?

Solução

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

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top