I'm trying to use ST_difference and ST_intersection with two layers of multipolygons.
I want something like:
With the input on the left and the result on the right.
MY DATA:
t1.geom contains 1000 differents polygons.
t2.geom contains 1000 differents polygons.
THE QUERIES:
I try to create (in a naive way) two queries to perform these operations
st_intersection(t1.geom, t2.geom)
from t1,t2
where st_intersects(t1.geom,t2.geom);
Unfortunately it doesn't work, or takes forever to run.
I would like a query similar to the tools erase and intersect from ArcGIS.
QUESTION:
How to perform those operations efficiently ?

st_difference(t1.geom, t2.geom) from t1,t2 where st_intersects(t1.geom,t2.geom);– user30184 Mar 31 '16 at 09:17t1.geom - t2.geom, in fact i don't understand the result, i just want to erase t2.geom that overlaps t1.geom – obchardon Mar 31 '16 at 09:29st_difference(t1.geom, t2.geom) from t1,t2 where st_intersects(t1.geom,t2.geom);does not give the expected results, as @user30184 has suggested. – John Powell Mar 31 '16 at 09:47select ST_AsText(ST_Difference(ST_GeomFromText('POLYGON (( 207 373, 207 719, 526 719, 526 373, 207 373 ))'),ST_GeomFromText('POLYGON (( 354 470, 354 628, 787 628, 787 470, 354 470 ))')))Result isPOLYGON (( 207 373, 207 719, 526 719, 526 628, 354 628, 354 470, 526 470, 526 373, 207 373 ))that is, part of geom1 that is not covered by geom2. Your problem is probably in what you get as geom1/geom2 pair when you process the whole layer. – user30184 Mar 31 '16 at 10:06SELECT ST_Difference(ST_Union(t1.geom), ST_Union(t2.geom)) FROM t1, t2though you might need to use ST_Dump to get polygons back or ST_CollectionExtract. For a small dataset, this should be fine, performance wise. I ran some tests and ST_Overlaps took about 1.5x longer than ST_Intersects, but this will depend so much on polygon complexity, index use, and any other functions being used. – John Powell Mar 31 '16 at 12:07