【发布时间】:2017-07-18 09:05:43
【问题描述】:
我有以下查询可以找到所有id insidePOLYGON(({points}))
SELECT id
FROM t
WHERE ST_DWithin('POLYGON(({points}))', ST_Point(latitude, longitude), 0);
查询很慢,因为它不使用(latitude, longitude) 索引,并且必须为每对可能的点计算公式。
如何更改我的查询以强制 Postgres 使用(latitude, longitude) 索引(我需要更改查询,因为我无法添加一些其他索引)?
我有以下索引:
"index_latitude_longitude" btree (latitude, longitude)
纬度、经度有double precision类型
我认为如果我们添加类似 latitude <= ... and longitude <= ... 的内容,Postgres 将使用索引,我们该怎么做?
【问题讨论】:
-
能否请您提供用于创建表和索引的 DDL?
-
@Arkhena 更新问题
-
我无法添加任何其他索引或更改某些内容,因为我对数据库没有任何写入权限
-
嗨@ipetr,我们假设(通过使用
ST_DWithin)您使用PostgreSQL的PostGIS扩展(请添加“postgis”标签)......所以,你不需要做纬度/经度分析,PostGIS 将为您完成。我还建议查看ST_DWithin(g1,g2,dist)使用,因为dist=0说“没有距离”(!)。是ST_Intersection?使用相关的dist参数测试您的查询。关于许可:对你的老板说你需要许可才能获得绩效(!!)。 -
关于“我认为Postgres会使用索引(...)经纬度”,你错了:要么放弃PostGIS,要么使用PostGIS。
标签: database postgresql performance postgis