python - PostGIS 空间查询性能低下

标签 python postgresql postgis psycopg2

我有一个空间查询,可以像这样找到多边形内的所有点:

startpolygon = time.time()
pointInpolygonV2 = """SELECT col.vessel_hash,ST_X(col.the_geom) AS long, ST_Y(col.the_geom) AS lat, ST_Contains(ST_GeomFromEWKT('SRID=4326; POLYGON((-28.828125 65.0721301,-28.125 64.7741253,-139.5703125 -47.0401821, 127.265625 -44.5904672,90 71.1877539,-28.828125 65.0721301))'),ST_GeomFromEWKT(col.the_geom))  FROM samplecol As col;"""
cursor.execute(pointInpolygonV2)
pointsINpol = cursor.fetchall()
endpolygon = time.time()
print (CGREYTIME+ "Time to fetch all points inside a polygon: "+CENDTIME), endpolygon - startpolygon

表格的形式是:

vessel_hash  | status | station | speed |  latitude   |  longitude  | course | heading |        timestamp         |                      the_geom                      
--------------+--------+---------+-------+-------------+-------------+--------+---------+--------------------------+----------------------------------------------------
‎103079215239 | 99     | 841     | 55    | 36.‎14622100 | -5.‎44244810 | 6      | 511     | 2016-07-28T05:55:31.000Z | 0101000020E610000098B55E1D11C515C0847EA65EB7124240

‎103079215239 | 99     | 841     | 45    | 36.‎14238000 | -5.‎44235280 | 355    | 511     | 2016-07-28T05:52:32.000Z | 0101000020E6100000162DE521F8C415C060CD018239124240   

此外,我在 the_geom 字段上还有一个索引:

CREATE INDEX samplecol_the_geom_gist ON samplecol USING gist (the_geom );  

创建表是:

CREATE TABLE samplecol
(
vessel_hash serial NOT NULL,
status character varying(50),
station character varying(50),
speed character varying(10),
latitude numeric(12,8),
longitude numeric(12,8),
course character varying(50),
heading character varying(50),
timestamp character varying(50),
the_geom geometry,
CONSTRAINT enforce_dims_the_geom CHECK (st_ndims(the_geom) = 2),
CONSTRAINT enforce_geotype_geom CHECK (geometrytype(the_geom) = 
'POINT'::text OR the_geom IS NULL),
CONSTRAINT enforce_srid_the_geom CHECK (st_srid(the_geom) = 4326) );

问题是查询太慢了。获取 745.902 点的响应时间为 700 秒。我观察到 postgis 使用索引比 mongo 快得多。在 mongo 中,同一查询的响应时间为 90 秒。有谁知道如何改进此查询或我做错了什么?

最佳答案

查询可能没有使用索引,因为您使用的不是几何列,而是它的派生列。

要使用索引,不要使用ST_GeomFromEWKT(col.the_geom)而是直接使用col.the_geom

SELECT col.vessel_hash,
       ST_X(col.the_geom) AS long, 
       ST_Y(col.the_geom) AS lat, 
       ST_Contains(ST_GeomFromEWKT('SRID=4326; POLYGON((-28.828125 65.0721301,-28.125 64.7741253,-139.5703125 -47.0401821, 127.265625 -44.5904672,90 71.1877539,-28.828125 65.0721301))'),
          col.the_geom) 
FROM samplecol As col;

关于python - PostGIS 空间查询性能低下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50800795/

相关文章:

postgresql - 如何将几何数据从一个表中取出到另一个表中?

python - 在 Haskell 中调用 Python 代码

Django - 使用 PostgreSQL 和 Elasticsearch 进行全文搜索

python - SQLAlchemy (w/Postgres) - 如何将全文搜索限制为多个索引的一列?

ruby-on-rails - 无法在 Heroku 上迁移

postgresql - 我如何获得围绕点的边界多边形?

python - 递归解决基本乘法

python - MNIST:试图获得高精度

python - 二维数组内的填充列表

database - PostgreSQL 的可扩展性