【发布时间】:2018-01-23 10:17:20
【问题描述】:
SELECT st.id
FROM
station st, strike s
WHERE s.srikedatetime > (current_timestamp - interval '20 seconds') and srikedatetime < current_timestamp
AND ST_Distance_Sphere(st.Geometry, s.strikepoint)/1000 <= st.radius
这个想法是当罢工发生时,它可能在基于站点监控半径的多个站点的边界内。我需要选择在过去 20 秒内受任何罢工影响的所有电台。在 20 秒内我可以有数千次罢工,当每 20 秒执行一次此查询时,CPU 会变高,查询需要几分钟才能完成。当 CPU 不高时,它以毫秒为单位运行。
查询计划:
"Nested Loop (cost=0.42..505110.20 rows=21 width=7)"
" Join Filter: ((_st_distance(geography(a.Geometry), geography(s.strikepoint), 0::double precision, false) / 1000::double precision) <= (a.radius)::double precision)"
" -> Index Only Scan using dup_strike_constraint on strike s (cost=0.42..8.45 rows=1 width=36)"
" Index Cond: ((srikedatetime > (now() - '00:00:20'::interval)) AND (srikedatetime < now()))"
" -> Seq Scan on station st (cost=0.00..505084.86 rows=62 width=549)"
我尝试过内部连接,类似这样的
Inner JOIN strike s ON ST_Distance(a.Geometry, s.strikepoint) < 1
并且还在 where 子句和分组中尝试了 ST_DWithin 仍然很慢。
ST_DWithin(s.strikepoint,a.Geometry, a.radius)
请问有什么想法吗?我有关于罢工和车站表的索引。
st.strikepoint 和 a.geomerty 列的数据类型是几何。 坐标系为 4326。 谢谢!
【问题讨论】:
-
请使用这些列的数据类型以及使用的坐标系更新问题。
-
更新了问题,st.strikepoint 和 a.geomerty 列的数据类型是几何。坐标系为 4326。
标签: sql postgresql gis postgis postgresql-9.4