Skip to main content
Added where clause based on Thingamabub's comment and primary key assumption.
Source Link
Nate Wanner
  • 1.6k
  • 9
  • 16

Does it work any better with an inner join rather than a full outer join? I expect an inner join would make better use of the indexes, where the full outer join would assemble every possible combination and then evaluate the results.

(This was edited with a presumption that you have a key column called "id" with a single-field primary key or a unique index)

with pnt_sample as (
  select a.id, b.rate
  from points a
  JOIN polys b
  ON st_intersects(a.geom, b.geom)
)

update points p
set p.fert_rate = ps.rate
from pnt_sample ps
where p.id = ps.id;

Alternatively:

update points
set a.fert_rate = b.rate
from pnt_sample;points a JOIN polys b
ON st_intersects(a.geom, b.geom);

Related: Acquiring ArcGIS-like speed in Postgis and https://stackoverflow.com/questions/17946221/sql-join-and-different-types-of-joins

EDIT: Thingamabub makes a good point. I don't know which table fert_rate and rate are in to setup the filter, but putting the join in your first query may help. If polygons overlap, then choosing the correct value may require some grouping.

update points
set fert_rate = rate
from points a JOIN polys b
ON st_intersects(a.geom, b.geom);

Does it work any better with an inner join rather than a full outer join? I expect an inner join would make better use of the indexes, where the full outer join would assemble every possible combination and then evaluate the results.

with pnt_sample as (
  select rate
  from points a
  JOIN polys b
  ON st_intersects(a.geom, b.geom)
)

update points
set fert_rate = pnt_sample.rate
from pnt_sample;

Related: Acquiring ArcGIS-like speed in Postgis and https://stackoverflow.com/questions/17946221/sql-join-and-different-types-of-joins

EDIT: Thingamabub makes a good point. I don't know which table fert_rate and rate are in to setup the filter, but putting the join in your first query may help. If polygons overlap, then choosing the correct value may require some grouping.

update points
set fert_rate = rate
from points a JOIN polys b
ON st_intersects(a.geom, b.geom);

Does it work any better with an inner join rather than a full outer join? I expect an inner join would make better use of the indexes, where the full outer join would assemble every possible combination and then evaluate the results.

(This was edited with a presumption that you have a key column called "id" with a single-field primary key or a unique index)

with pnt_sample as (
  select a.id, b.rate
  from points a
  JOIN polys b
  ON st_intersects(a.geom, b.geom)
)

update points p
set p.fert_rate = ps.rate
from pnt_sample ps
where p.id = ps.id;

Alternatively:

update points
set a.fert_rate = b.rate
from points a JOIN polys b
ON st_intersects(a.geom, b.geom);

Related: Acquiring ArcGIS-like speed in Postgis and https://stackoverflow.com/questions/17946221/sql-join-and-different-types-of-joins

If polygons overlap, then choosing the correct value may require some grouping.

Discussed issues raised by Thingamabub
Source Link
Nate Wanner
  • 1.6k
  • 9
  • 16

Does it work any better with an inner join rather than a full outer join? I expect an inner join would make better use of the indexes, where the full outer join would assemble every possible combination and then evaluate the results.

with pnt_sample as (
  select rate
  from points a
  JOIN polys b
  ON st_intersects(a.geom, b.geom)
)

update points
set fert_rate = pnt_sample.rate
from pnt_sample;

Related: Acquiring ArcGIS-like speed in Postgis and https://stackoverflow.com/questions/17946221/sql-join-and-different-types-of-joins

EDIT: Thingamabub makes a good point. I don't know which table fert_rate and rate are in to setup the filter, but putting the join in your first query may help. If polygons overlap, then choosing the correct value may require some grouping.

update points
set fert_rate = rate
from points a JOIN polys b
ON st_intersects(a.geom, b.geom);

Does it work any better with an inner join rather than a full outer join? I expect an inner join would make better use of the indexes, where the full outer join would assemble every possible combination and then evaluate the results.

with pnt_sample as (
  select rate
  from points a
  JOIN polys b
  ON st_intersects(a.geom, b.geom)
)

update points
set fert_rate = pnt_sample.rate
from pnt_sample;

Related: Acquiring ArcGIS-like speed in Postgis and https://stackoverflow.com/questions/17946221/sql-join-and-different-types-of-joins

Does it work any better with an inner join rather than a full outer join? I expect an inner join would make better use of the indexes, where the full outer join would assemble every possible combination and then evaluate the results.

with pnt_sample as (
  select rate
  from points a
  JOIN polys b
  ON st_intersects(a.geom, b.geom)
)

update points
set fert_rate = pnt_sample.rate
from pnt_sample;

Related: Acquiring ArcGIS-like speed in Postgis and https://stackoverflow.com/questions/17946221/sql-join-and-different-types-of-joins

EDIT: Thingamabub makes a good point. I don't know which table fert_rate and rate are in to setup the filter, but putting the join in your first query may help. If polygons overlap, then choosing the correct value may require some grouping.

update points
set fert_rate = rate
from points a JOIN polys b
ON st_intersects(a.geom, b.geom);
Source Link
Nate Wanner
  • 1.6k
  • 9
  • 16

Does it work any better with an inner join rather than a full outer join? I expect an inner join would make better use of the indexes, where the full outer join would assemble every possible combination and then evaluate the results.

with pnt_sample as (
  select rate
  from points a
  JOIN polys b
  ON st_intersects(a.geom, b.geom)
)

update points
set fert_rate = pnt_sample.rate
from pnt_sample;

Related: Acquiring ArcGIS-like speed in Postgis and https://stackoverflow.com/questions/17946221/sql-join-and-different-types-of-joins