2

我是 PostgreSQL / PostGIS 的新手。我正在评估它以解决一个简单的算法:尝试找到半径(米)内的所有点。这是我的桌子:

=> \d+ theuser;
                         Table "public.theuser"
  Column  |          Type          | Modifiers | Storage  | Description 
----------+------------------------+-----------+----------+-------------
 id       | bigint                 | not null  | plain    | 
 point    | geometry               |           | main     | 
Indexes:
    "theuser_pkey" PRIMARY KEY, btree (id)
    "point_index" gist (point)
Referenced by:
    ...
Has OIDs: no

我在列中添加了一个要点索引point,我不知道它是否是正确的设计。插入的所有“点”都带有SRID=4326.

似乎有两种方法可以获得附近的点:

ST_DistanceST_Distance_Sphere。以2为例:

select * from theuser where
ST_distance_sphere(point , ST_GeomFromText('POINT(120.9982 24.788)',4326)) < 100;

我想知道哪个算法使用了“ point_index”?如果有几百万个点,两者都执行得很快吗?

另一个问题,我如何查询一个单元格的 SRID(我搜索了没有找到答案)?我所能做的就是通过 hibernate-spatial-postgis ,获取 " com.vividsolutions.jts.geom.Point" ,然后从返回的点获取 SRID。如何在 SQL 中查询它?谢谢。

环境 :

=> select version();
                                                  version                                                  
-----------------------------------------------------------------------------------------------------------
 PostgreSQL 8.4.9 on i486-pc-linux-gnu, compiled by GCC gcc-4.4.real (Ubuntu 4.4.3-4ubuntu5) 4.4.3, 32-bit

=> SELECT postgis_lib_version();
 postgis_lib_version 
---------------------
 1.4.0

- - 更新 - -

谢谢@filiprem,我试过这个:

=> explain select * from theuser where
ST_distance_sphere(point , ST_GeomFromText('POINT(120.9982 24.788)',4326)) < 100;
                                                          QUERY PLAN                                                           
-------------------------------------------------------------------------------------------------------------------------------
 Seq Scan on theuser  (cost=0.00..1.15 rows=3 width=2644)
   Filter: (st_distance_sphere(point, '0101000020E610000080B74082E23F5E407D3F355EBAC93840'::geometry) < 100::double precision)
(2 rows)

我怎么知道它是否使用了"point_index" gist (point)? 它在高数据量搜索下还能生存吗?

4

1 回答 1

2

我曾经听说 ST_DWithin 是最快的,实际上在文档中他们说在较新的版本中 ST_DWithin 已被调整。

在 1.3 之前,ST_Expand 通常与 && 和 ST_Distance 结合使用以实现相同的效果,而在 1.3.4 之前,此函数基本上是该构造的简写。从 1.3.4 开始,ST_DWithin 使用更短的距离函数,这应该使它比以前的版本更有效,用于更大的缓冲区。

它还使用边界框比较和索引:

此函数调用将自动包含一个边界框比较,该比较将利用几何上可用的任何索引。

于 2011-12-09T00:08:43.673 回答