Using multipolygon or polygon features for large intersect operations?
You are right that dumping the multipolygons to polygons makes the index more efficient, for the reason you mention.
So if the size of the dataset makes performance an issue you should:
- dump the multipolygons into a new table.
- create a spatial index on the polygons
- get the intersecting part and restrict the query to polygons theat intersect.
Something like:
/*Create polygon table from multipolygons*/
Create table forest_polygons as select (st_dump(geom)).geom geom from forest_table;
/*create the spatial index*/
create index idx_forest_geom on forest_polygons using gist(geom);
/*tell the planner about the index*/
analyze forest_polygons;
/*Make a selection of forest in the municipality, or more correct:
Asign municipalitiy id to the forests*/
Select st_intersection(gem), municipalities_id as forest_in_municipality from
forest_polygons inner join municipalities
on st_intersects(forest_polygons.geom, municipalities.geom);