ST_GeomFromText有多贵

时间:2008-08-30 17:25:41

标签: gis postgis

在postgis中,ST_GeomFromText电话是否非常昂贵?我主要是因为我有一个经常被调用的查询,它试图找到最接近另一个点的点,这个点与某个标准匹配,并且也在该点的某个距离内,以及我当前编写它的方式,它正在执行相同ST_GeomFromText两次:

 $findNearIDMatchStmt = $postconn->prepare(
    "SELECT     internalid " .
    "FROM       waypoint " .
    "WHERE      id = ? AND " .
    "           category = ? AND ".
    "           (b.category in (1, 3) OR type like ?) AND ".
    "           ST_DWithin(point, ST_GeomFromText(?," . SRID .
    "           ),".  SMALL_EPSILON . ") " .
    "           ORDER BY ST_Distance(point, ST_GeomFromText(?,", SRID .
    "           )) " .
    "           LIMIT 1");

有没有更好的方法来重写这个?

略微OT:在预览屏幕中,我的所有下划线都呈现为& # 9 5 ; - 我希望不会在帖子中以这种方式显示。

2 个答案:

答案 0 :(得分:1)

我不相信ST_GeomFromText()特别昂贵,虽然过去我通过创建函数优化PostGIS查询,声明变量然后分配ST_GeomFromText的结果到变量。

您是否尝试使用各种不同的参数检查执行计划,因为这可以让您明确了解查询的哪些位花费时间?

我猜测大部分执行时间都在ST_DWithin()ST_Distance()的调用中,尽管如果id和category列没有编入索引,那么它可能会进行一些有趣的表扫描

答案 1 :(得分:1)

@Ubiguch 似乎ST_DWithin使用空间索引,因此似乎可以很快减少要查询的点数。

 navaid=> explain select internalid from waypoint where id != 'KROC' AND ST_DWithin(point,                                                                  ST_GeomFromText('POINT(-77.6723888888889 43.1188611111111)',4326), 0.05) order by st_distance(point, st_geomfromtext('POINT(-77.6723888888889 43.1188611111111)',4326)) limit 1;
                                                                                                                                                                                                                                                      QUERY PLAN                                                                                                                                                                                                                                                       
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=8.37..8.38 rows=1 width=104)
   ->  Sort  (cost=8.37..8.38 rows=1 width=104)
         Sort Key: (st_distance(point, '0101000020E61000002FFE676B086B53C0847E44D7368F4540'::geometry))
         ->  Index Scan using waypoint_point_idx on waypoint  (cost=0.00..8.36 rows=1 width=104)
               Index Cond: (point && '0103000020E61000000100000005000000000000C03B6E53C000000060D0884540000000C03B6E53C0000000409D95454000000020D56753C0000000409D95454000000020D56753C000000060D0884540000000C03B6E53C000000060D0884540'::geometry)
               Filter: (((id)::text <> 'KROC'::text) AND (point && '0103000020E61000000100000005000000000000C03B6E53C000000060D0884540000000C03B6E53C0000000409D95454000000020D56753C0000000409D95454000000020D56753C000000060D0884540000000C03B6E53C000000060D0884540'::geometry) AND ('0101000020E61000002FFE676B086B53C0847E44D7368F4540'::geometry && st_expand(point, 0.05::double precision)) AND (st_distance(point, '0101000020E61000002FFE676B086B53C0847E44D7368F4540'::geometry) < 0.05::double precision))
(6 rows)

如果没有order bylimit,看起来典型的查询最多只能返回5-10个航点。所以我可能不应该担心应用于返回点的过滤器的额外成本。