简体   繁体   English

PostgreSQL查询优化索引未使用

[英]Postgresql query optimization Index not used

I'm having some trouble in optimizing some posgresql queries. 我在优化某些posgresql查询时遇到了一些麻烦。 For example, this one seams to be the slowest: 例如,这个接缝是最慢的:

select  st_astext(geom), 'all' as type
from (
   select ST_Simplify(ST_Intersection(ST_MakePolygon(ST_GeomFromText(? ,4326)), st_transform(way, 4326)), ?) as geom
   from planet_osm_polygon 
   where (sT_Intersects(ST_MakePolygon(ST_GeomFromText(?,4326)), st_transform(way,4326))=true)
   and   ('natural' IN ('water', 'pond') OR waterway IN ('basin', 'canal', 'mill_pond', 'pond', 'riverbank', 'stream'))
) AS subquery";

First thing i did was editing the conf file and changing buffer sizes, and i got few percent less in total time spent. 我要做的第一件事是编辑conf文件和更改缓冲区大小,总花费的时间减少了百分之几。

Next thing I did was creating new tables from existing tables and breaking multipolygons into polygons. 我要做的下一件事是从现有表中创建新表,并将多面多边形分解为多边形。 That speeded up most of the querys from 70-90%. 这使大多数查询从70-90%加快了速度。 However, the slowest 3 queries didnt speed up more than a few percent. 但是,最慢的3个查询的速度没有超过百分之几。

By examining the searches using EXPLAIN and ANALYZE, i realized that indexes are not used. 通过使用EXPLAIN和ANALYZE检查搜索,我意识到未使用索引。 What is used is seqscan even if i disable it. 即使我禁用它,也使用seqscan。 As far as i know this means that i should make a new index. 据我所知,这意味着我应该做一个新索引。

In the table planet_osm_polygon i have two indexes: 在表planet_osm_polygon中,我有两个索引:

CREATE INDEX planet_osm_polygon_index_poly
  ON planet_osm_polygon_poly
  USING gist (way);

CREATE INDEX planet_osm_polygon_poly_pkey
  ON planet_osm_polygon_poly
  USING btree (osm_id);

Any ideas how to speed up this query and why aren't the indexes being used? 有什么想法可以加快查询速度,为什么不使用索引?

This is very new to me, and if something i wrote doesn't make sense, just ignore it :) 这对我来说是很新的,如果我写的东西没有意义,那就忽略它吧:)

Indexes could potentially be used with ST_Intersects in the WHERE part, except they are not because a function is used on the data, namely st_transform(way,4326) . 索引可能与WHERE部分中的ST_Intersects一起使用,除非不是因为在数据上使用了一个函数,即st_transform(way,4326) ,否则它们没有st_transform(way,4326) Your options are to avoid the function (perform an interesction query within the native projection, which will yield different answers), or to add an index using the function (although I'm not 100% certain that this would work with ST_Intersects). 您的选择是避免使用该函数(在本机投影中执行交叉查询,这将产生不同的答案),或者使用该函数添加索引(尽管我不是100%确信这可以与ST_Intersects一起使用)。

Lastly, two points. 最后,有两点。 SELECT 'natural' IN ('water', 'pond'); is always false. 永远是错误的。 And SELECT true=true is true, so any boolean operator like ST_Intersects(g1, g2)=true is logically valid, but is aesthetically redundant. 并且SELECT true=true为true,因此任何布尔运算符(如ST_Intersects(g1, g2)=true在逻辑上都是有效的,但在美学上是多余的。 Just use ST_Intersects(g1, g2) without the "equals true" part. 只需使用ST_Intersects(g1, g2)而不使用“等于true”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM