如何优化空间查询?

时间:2014-03-12 13:55:34

标签: postgresql postgis spatial-query

我需要优化此查询,&#co;执行时间过长。我已经在geom(point)上构建了gist索引,但它们并没有被不幸地使用。

    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

2 个答案:

答案 0 :(得分:0)

ST_DISTANCE函数不使用几何索引。你应该使用例如ST_DWITHIN所以您的查询将类似于

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)

以下是此功能的文档。

http://postgis.org/docs/ST_DWithin.html

答案 1 :(得分:0)

如果您有一个名为city_gis的表,我将假设这是一个城市的GIS数据表。它是一个城市还是更多?如果它在地理上是孤立的,那么通过使用该区域的投影坐标系可以使生活更加轻松。您始终可以将坐标转换回lat / long以用于其他用途。通过使用投影坐标系,任何与距离相关的函数都将使用投影单位。例如,使用UTM坐标系时,距离单位始终以米为单位。显然,如果您的表格确实包含来自世界各地不同城市甚至是一个大洲的数据,这将无效。