sql - Postgres query running very slow -


select st.id        station st, strike s s.srikedatetime > (current_timestamp - interval '20 seconds')  , srikedatetime < current_timestamp     , st_distance_sphere(st.geometry, s.strikepoint)/1000 <= st.radius 

the idea when strike hits, in boundary of multiple stations based on station monitoring radius. need select stations affected strikes in last 20 seconds. in 20 seconds can have thousands of strikes , when executing query every 20 seconds, cpu goes high , query takes minutes complete. when cpu not high, runs in milliseconds.

query plan:

"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 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)) , (srikedatetime < now()))" "  ->  seq scan on station  st  (cost=0.00..505084.86 rows=62 width=549)" 

i have tried inner join, this

inner join strike s on st_distance(a.geometry, s.strikepoint) < 1 

and tried st_dwithin in clause , grouping still slow.

st_dwithin(s.strikepoint,a.geometry, a.radius) 

any thoughts, please? have indexes on strike , station tables.

the data types st.strikepoint , a.geomerty columns geometry. coordinate system 4326. thank you!

you create lazy materialized view stock data need. it's lot faster if query 1 table instead of joining 'station' , 'strike' tables.

obviously, lazy table should indexed properly, especialy if don't plan 'clean' data periodicaly.

other option using temp table handle data, if it's not necessary store historically , handle data in 1 session. temp tables visible 1 session , 'die' when session ends. can create them , destroy them @ point, in case should pay attention memory usage.

hope helps.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -