1

I have three tables containing increasingly-complex geometry. I'm clipping Tables 2 and 3 to the first, least-complex table (with 700 multipolygons). The other two tables contain spatial attributes I'm after (t2: 300 000 records, t3: millions).

Table 3 - most complex - has less than 100% areal coverage, so I need to "see through" the holes to the geometries of Table 2.

With SO help sorted a couple intersect queries, read docs explaining indexing, vacuum / explain, and came back to speed these up:

SELECT ST_Intersection(t1.geom, t2.geom)
FROM t1 
JOIN t2 ON ST_Intersects(t1.geom, t2.geom)

... when I found this thread from 6 years back, a massive help, taking initial queries from 30 minutes to under 4: Acquiring ArcGIS-like speed in Postgis

Using the ST_CoveredBy function and CASE flow we have two nearly-identical queries relating t3 with t1, then t2 with t1:

SELECT t1.location, t3.attributecode, 
  CASE
  WHEN ST_CoveredBy(t3.geom, t1.geom)
  THEN t3.geom
ELSE
  ST_Multi(
      ST_Intersection(t3.geom, t1.geom)
      ) END AS geom
FROM table3 AS t3
    INNER JOIN table1 AS t1
    ON ST_Intersects(t3.geom, t1.geom)
    );

I've made tables from these, here's t2 cut to t1 boundaries:

t2 cut to t1

With t3 overlying this (transparency added to make it clear they're two geom tables):

t3 cut to t1

Next is aggregating t2 and t3, using ST_Union perhaps, which is slow even on smaller test datasets.

What are some ways of speeding this up?

Vince
  • 20,017
  • 15
  • 45
  • 64
Jeff
  • 73
  • 1
  • 8
  • Have you looked into using ST_Collect instead of union? Significant performance boost if I recall – Liam G Jun 13 '19 at 23:02

0 Answers0