1

在 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 ;- 我希望在帖子中不会以这种方式出现。

4

2 回答 2

1

我不认为ST_GeomFromText()它特别昂贵,尽管过去我PostGIS通过创建函数、声明变量然后将结果分配给变量来优化查询ST_GeomFromText

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

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

于 2008-08-30T22:16:10.093 回答
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 个航点。所以我可能不应该担心应用于返回点的过滤器的额外成本。

于 2008-08-31T01:37:11.203 回答