【发布时间】:2018-11-20 21:14:22
【问题描述】:
我有一个空间查询,可以像这样找到多边形内的所有点:
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 秒。有谁知道如何改进这个查询或者我做错了什么?
【问题讨论】:
标签: python postgresql postgis psycopg2