【问题标题】:Low performance on PostGIS Spatial QueryPostGIS 空间查询性能低下
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

    • 我会试试这个,我会通知你的。谢谢你的回答
    • 现在响应时间好像变小了。表格700秒现在是500了。没有别的事可做吗?我看到 postgis(我测试了很多查询)在使用索引时几乎比 mongodb 快 4 倍。为什么此查询中出现此行为?你知道吗?
    【解决方案2】:

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

    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 秒。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-03
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      相关资源
      最近更新 更多