<div dir="ltr">Hi everyone,<div><br></div><div>I have GPS trajectory data in LineStringM format (GeoLife data formatted as described in Anita Graser's blog post). I am trying to find trajectories that are close to each other via ST_DWithin and ST_CPAWithin.</div><div><br></div><div>I have pre-filtered the trajectories with respect to time-ranges (tstzrange) and saved results in another table named `tf`. This table has two fields id1 and id2 corresponding to the pairs that satisfy the temporal filtering (overlap via &&) requirements.</div><div><br></div><div>Then I apply ST_DWithin to find trajectories within 0.1 degrees of each other. Ideally this would be the second pre-filtering step before finally refining with the spatiotemporal function CPAWithin. However, I cannot seem to get postgis to use the available indexes. I ran the following query:</div><div><br></div><div>EXPLAIN ANALYZE<br>SELECT <a href="http://t1.id">t1.id</a>, <a href="http://t2.id">t2.id</a><br>FROM tf<br>JOIN traj2 t1 ON <a href="http://t1.id">t1.id</a> = tf.id1<br>JOIN traj2 t2 ON <a href="http://t2.id">t2.id</a> = tf.id2<br>WHERE ST_DWithin(t1.track, t2.track,0.1)<br></div><div><br></div><div>I have the following indexes on the GeoLife trajectory table called `traj2`:<br>Indexes:<br>    "idx_traj2_ep_geog" gist (geography(end_point))<br>    "idx_traj2_ep_geom" gist (end_point)<br>    "idx_traj2_id" btree (id)<br>    "idx_traj2_sp_geog" gist (geography(start_point))<br>    "idx_traj2_sp_geom" gist (start_point)<br>    "idx_traj2_time_range" gist (time_range)<br>    "idx_traj2_time_track2d" gist (track)<br>    "idx_traj2_time_track2d_geog" gist (geography(track))<br>    "idx_traj2_time_track3d" gist (track gist_geometry_ops_nd)<br>    "idx_traj2_tracker" btree (tracker)<br>Access method: heap<br>Options: parallel_workers=8<br><br>and the following on `tf`:<br>Indexes:<br>    "idx_tf_id1" btree (id1)<br>    "idx_tf_id2" btree (id2)<br>Access method: heap<br>Options: parallel_workers=8<br></div><div><br></div><div>I have run VACUUM ANALYZE before running EXPLAIN ANALYZE and the results are as follows:<br><br>"Gather  (cost=38404.10..557039.81 rows=23555 width=8) (actual time=182.494..362202.326 rows=48253 loops=1)"<br>"  Workers Planned: 2"<br>"  Workers Launched: 2"<br>"  ->  Parallel Hash Join  (cost=37404.10..553684.31 rows=9815 width=8) (actual time=217.140..361763.181 rows=16084 loops=3)"<br>"        Hash Cond: (tf.id1 = <a href="http://t1.id">t1.id</a>)"<br>"        Join Filter: st_dwithin(t1.track, t2.track, '0.1'::double precision)"<br>"        Rows Removed by Join Filter: 4931"<br>"        ->  Parallel Hash Join  (cost=870.07..7265.31 rows=26270 width=32026) (actual time=3.649..10.452 rows=21016 loops=3)"<br>"              Hash Cond: (<a href="http://t2.id">t2.id</a> = tf.id2)"<br>"              ->  Parallel Seq Scan on traj2 t2  (cost=0.00..6003.79 rows=7779 width=32022) (actual time=0.003..2.695 rows=6223 loops=3)"<br>"              ->  Parallel Hash  (cost=541.70..541.70 rows=26270 width=8) (actual time=3.452..3.452 rows=21016 loops=3)"<br>"                    Buckets: 65536  Batches: 1  Memory Usage: 3008kB"<br>"                    ->  Parallel Seq Scan on tf  (cost=0.00..541.70 rows=26270 width=8) (actual time=0.008..0.930 rows=21016 loops=3)"<br>"        ->  Parallel Hash  (cost=6003.79..6003.79 rows=7779 width=32022) (actual time=132.408..132.408 rows=6223 loops=3)"<br>"              Buckets: 4096  Batches: 16  Memory Usage: 2688kB"<br>"              ->  Parallel Seq Scan on traj2 t1  (cost=0.00..6003.79 rows=7779 width=32022) (actual time=121.500..123.018 rows=6223 loops=3)"<br>"Planning Time: 3.335 ms"<br>"JIT:"<br>"  Functions: 60"<br>"  Options: Inlining true, Optimization true, Expressions true, Deforming true"<br>"  Timing: Generation 3.102 ms, Inlining 79.410 ms, Optimization 173.707 ms, Emission 110.965 ms, Total 367.184 ms"<br>"Execution Time: 362206.853 ms"<br></div><div><br></div><div>My current settings are </div><div>shared_buffers: 2GB, work_mem: 64MB, maintenance_work_mem: 512MB, effective_cache_size: 4GB.</div><div>(I am on a Ubuntu 18.04 laptop machine with 16 GB RAM and i7 2.30 GHz 16 core CPU) (I have the following postgis installation: POSTGIS="3.1.0 5e2af69" [EXTENSION] PGSQL="120" GEOS="3.7.1-CAPI-1.11.1 27a5e771" PROJ="Rel. 4.9.3, 15 August 2016" LIBXML="2.9.4" LIBJSON="0.12.1" LIBPROTOBUF="1.2.1" WAGYU="0.5.0 (Internal)" )</div><div><br></div><div>I have also tried setting STORAGE EXTERNAL as per Paul Ramsay's blog post on spatial joins to no avail.</div><div><br></div><div>As a final note, I have SET parallel_workers=8 but query planner seems to be stuck with 2 workers launched/planned.<br><br>I am fairly new to GIS and postgis altogether, so I might be missing something very simple and straightforward. I would be grateful if you guys can point me in the right direction.</div><div><br></div><div>Best regards,</div><div>Temir</div></div>