我有一个存储半径22km的ST_Buffer的表(SRID = 4326)。我正在尝试以下查询:
line = "ST_GeometryFromText('SRID=4326;LINESTRING(%s)')" % (line)
cur.execute("SELECT ST_AsText(ST_Difference(line,(SELECT ST_Union(buffers.the_geog::geometry)
FROM buffers WHERE ST_Intersects( line ,buffers.the_geog::geometry )))) FROM %s as line"%(line))
EXPlAIN ANALYZE
查询给出了以下结果:
[('Function Scan on line (cost=0.00..2741841.15 rows=1 width=32) (actual time=10345.785..10345.789 rows=1 loops=1)',),
(' SubPlan 1',), (' -> Aggregate (cost=2741841.12..2741841.13 rows=1 width=576) (actual time=10345.434..10345.435 rows=1 loops=1)',),
(' -> Seq Scan on buffers (cost=0.00..2741839.82 rows=261 width=576) (actual time=920.965..10051.716 rows=841 loops=1)',),
(' Filter: ((line.line && (the_geog)::geometry) AND _st_intersects(line.line, (the_geog)::geometry))',),
(' Rows Removed by Filter: 7814377',), ('Total runtime: 10346.496 ms',)]
正如您所看到的,ST_Intersects
未使用GIST
索引。如何强制查询使用GIST
索引?
我的表结构是:
cur.execute("CREATE TABLE buffers(gid serial PRIMARY KEY, the_geog geography(POLYGON,4326) );")
索引是:
CREATE INDEX tindex ON buffer USING GIST ( the_geog );
答案 0 :(得分:0)
假设您确实运行了分析,可能是因为子查询。在没有子查询的情况下编写查询时,它将使用空间索引。像这样:
select ST_AsText(ST_Difference(line.line, ST_Union(buffers.the_geom::geometry)))
FROM line,buffers WHERE
ST_Intersects( line.line ,buffers.the_geom::geometry )
group by line.line;