<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">I'm doing some left outer spatial joins using the && operator, which should be relatively quick. <br><br>When I join across 5 tables, the result is fast enough, & explain shows index scans are being applied to the spatial tables. When I add another table, explain shows a seq scan is used, despite the table having a suitable index, and given 90,000,000 records, it is more than somewhat sluggish.<br><br>Any advice??<br><br><br>The query I'm running (with the extra table  & the explain results are:<br><br>explain select s.ogc_fid,<br>       s.trip::integer as trip,<br>       s.station::varchar(20) as station,<br>       avg(d.depth)::decimal(6,2) as avg_depth,<br>       avg(t.botemp)::decimal(8,6) as
 avg_botemp,<br>       avg(a.aou)::decimal(8,6) as avg_aou,<br>       avg(o.boto2sat)::decimal(8,6) as avg_o2<br>--       avg(p.botpho)::decimal(8,6) as avg_pho<br>from allstations s<br>left outer join depths d on d.geom && buffer(s.geom2,125)<br>left outer join botemp t on t.geom && buffer(s.geom2,125)<br>left outer join aou a on a.geom && buffer(s.geom2,125)<br>left outer join boto2sat o on o.geom && buffer(s.geom2,125)<br>--left outer join botpho p on p.geom && buffer(s.geom2,125)<br>where s.ogc_fid =1 <br>group by s.ogc_fid,<br>         s.trip,<br>         s.station<br>order by s.ogc_fid;<br><br><br>Query plan 1
 <br>--------------------------------------------------------------------------------------------------------------------<br> HashAggregate  (cost=210102370480.36..210102370480.40 rows=1 width=293)<br>   ->  Nested Loop Left Join  (cost=38.17..209249565267.93 rows=48731726425 width=293)<br>         ->  Nested Loop Left Join  (cost=0.75..452592989.59 rows=103730722 width=371)<br>               ->  Nested Loop Left Join  (cost=0.50..969144.98 rows=220802 width=365)<br>                     ->  Nested Loop Left Join  (cost=0.25..2066.85 rows=470
 width=357)<br>                           ->  Index Scan using allstations_pk on allstations s  (cost=0.00..8.28 rows=1 width=349)<br>                                 Index Cond: (ogc_fid = 1)<br>                           ->  Index Scan using aou_point_idx on aou a  (cost=0.25..1935.19 rows=470 width=92)<br>                                 Index Cond: (geom
 && buffer(s.geom2, 125::double precision))<br>                     ->  Index Scan using botemp_point_idx on botemp t  (cost=0.25..1934.24 rows=470 width=92)<br>                           Index Cond: (geom && buffer(s.geom2, 125::double precision))<br>               ->  Index Scan using boto2sat_point_idx on boto2sat o  (cost=0.25..1922.00 rows=470 width=90)<br>                     Index Cond: (geom && buffer(s.geom2, 125::double precision))<br>         ->  Bitmap
 Heap Scan on depths d  (cost=37.42..1889.50 rows=470 width=90)<br>               Recheck Cond: (geom && buffer(s.geom2, 125::double precision))<br>               ->  Bitmap Index Scan on depth_point_idx  (cost=0.00..37.30 rows=470 width=0)<br>                     Index Cond: (geom && buffer(s.geom2, 125::double precision))<br>(17 rows)<br><br><br>Query plan 2 (extra table causes Seq scan)<br>--------------------------------------------------------------------------------------------------------------------------<br> HashAggregate  (cost=98761396253872.30..98761396253872.34 rows=1 width=301)<br>   ->  Nested Loop Left Join 
 (cost=38.17..98303522035976.80 rows=22893710894775 width=301)<br>         ->  Nested Loop Left Join  (cost=0.75..212650447941.07 rows=48731726425 width=379)<br>               ->  Nested Loop Left Join  (cost=0.50..481774313.60 rows=103730722 width=373)<br>                     ->  Nested Loop Left Join  (cost=0.25..27449239.14 rows=220802 width=365)<br>                           ->  Nested Loop Left Join  (cost=0.00..26481714.34 rows=470
 width=357)<br>                                 Join Filter: (p.geom && buffer(s.geom2, 125::double precision))<br>                                 ->  Index Scan using allstations_pk on allstations s  (cost=0.00..8.28 rows=1 width=349)<br>                                       Index Cond: (ogc_fid =
 1)<br>                                 ->  Seq Scan on botpho p  (cost=0.00..1817695.36 rows=93958136 width=92)<br>                           ->  Index Scan using aou_point_idx on aou a  (cost=0.25..1935.19 rows=470 width=92)<br>                                 Index Cond: (geom && buffer(s.geom2, 125::double precision))<br>                     ->  Index Scan using
 botemp_point_idx on botemp t  (cost=0.25..1934.24 rows=470 width=92)<br>                           Index Cond: (geom && buffer(s.geom2, 125::double precision))<br>               ->  Index Scan using boto2sat_point_idx on boto2sat o  (cost=0.25..1922.00 rows=470 width=90)<br>                     Index Cond: (geom && buffer(s.geom2, 125::double precision))<br>         ->  Bitmap Heap Scan on depths d  (cost=37.42..1889.50 rows=470 width=90)<br>               Recheck Cond: (geom && buffer(s.geom2,
 125::double precision))<br>               ->  Bitmap Index Scan on depth_point_idx  (cost=0.00..37.30 rows=470 width=0)<br>                     Index Cond: (geom && buffer(s.geom2, 125::double precision))<br>(20 rows)<br><br></td></tr></table>