PostGIS空间查询性能低下

时间:2018-06-11 15:04:24

标签: 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中,同一查询的repsonse时间为90秒。有谁知道如何改进这个查询或我做错了什么?

2 个答案:

答案 0 :(得分:2)

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

要使用索引,请不要使用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;

答案 1 :(得分:0)

@JGH似乎响应时间较短但这并不能解决我的问题。我发现如果我在gist中添加ST_Contains,则会使用索引where。所以我的查询现在是:

SELECT vessel_hash,ST_X(the_geom) AS long, ST_Y(the_geom) AS lat from samplecol where ST_Contains(ST_GeomFromEWKT('SRID=4326; POLYGON((17.2045898 37.3002753,17.2045898 37.2827946,17.0947266 35.5143431, 19.8413086 35.6215819,19.8193359 37.2827946,17.2045898 37.3002753))'),the_geom);

,响应时间约为20秒。