<div dir="ltr">Hi,<div><br></div><div>I have 2 tables, indexed and analyzed.</div><div>One big (batiment) with 45M records, one small (tmp.tp1), with 83k records (UNLOGGED table)</div><div><br></div><div>When performing a spatial join, the planner chooses the smallest table for seq scan only if I add a && condition with st_intersects:</div><div><br></div><div><div><font face="monospace, monospace">EXPLAIN SELECT <a href="http://p.id">p.id</a>, <a href="http://b.id">b.id</a></font></div><div><font face="monospace, monospace">FROM tmp.tp1 p join ref.batiment b on st_intersects(b.geom, p.geom);</font></div></div><div><font face="monospace, monospace"><br></font></div><div><div><font face="monospace, monospace">QUERY PLAN</font></div><div><font face="monospace, monospace">Nested Loop  (cost=0.15..21819347.50 rows=50019 width=35)</font></div><div><font face="monospace, monospace">  ->  Seq Scan on batiment b  (cost=0.00..2238826.04 rows=45794904 width=193)</font></div><div><font face="monospace, monospace">  ->  Index Scan using tp1_geom_gist on tp1 p  (cost=0.15..0.42 rows=1 width=276)</font></div><div><font face="monospace, monospace">        Index Cond: (b.geom && geom)</font></div><div><font face="monospace, monospace">        Filter: _st_intersects(b.geom, geom)</font></div></div><div><br></div><div>vs</div><div><br></div><div><div><font face="monospace, monospace">EXPLAIN SELECT <a href="http://p.id">p.id</a>, <a href="http://b.id">b.id</a></font></div><div><font face="monospace, monospace">FROM tmp.tp1 p join ref.batiment b on p.geom && b.geom and st_intersects(b.geom, p.geom);</font></div></div><div><font face="monospace, monospace"><br></font></div><div><div><font face="monospace, monospace">QUERY PLAN</font></div><div><font face="monospace, monospace">Nested Loop  (cost=0.42..118443.25 rows=1 width=35)</font></div><div><font face="monospace, monospace">  ->  Seq Scan on tp1 p  (cost=0.00..842.75 rows=13675 width=276)</font></div><div><font face="monospace, monospace">  ->  Index Scan using batiment_geom_gist on batiment b  (cost=0.42..8.59 rows=1 width=193)</font></div><div><font face="monospace, monospace">        Index Cond: ((p.geom && geom) AND (geom && p.geom))</font></div><div><font face="monospace, monospace">        Filter: _st_intersects(geom, p.geom)</font></div></div><div><br></div><div>PG: </div><div>PostgreSQL 9.6.6 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609, 64-bit<br></div><div><br></div><div>Postgis:</div><div><div>POSTGIS="2.3.3 r15473" GEOS="3.5.1-CAPI-1.9.1 r4246" PROJ="Rel. 4.9.2, 08 September 2015" GDAL="GDAL 1.11.3, released 2015/09/16" LIBXML="2.9.3" LIBJSON="0.11.99" TOPOLOGY (topology procs from "2.3.2 r15302" need upgrade) RASTER</div><div><br></div></div><div>Thanks for any hint.</div><div><br></div><div>Nicolas</div></div>