2012-06-23 8 views
5

Ich habe bemerkt, dass diese Unterabfrage ausgeführtWie kann ich diese Unterabfrage als Join optimieren?

SELECT ST_Area (ST_Union (ST_Transform (ST_Intersection ((SELECT poly1.the_geom von poly1 WHERE poly1.polygon_type = 'P'), poly2.the_geom), 3857)))

AS area_of_P VON poly1, poly2

ist deutlich langsamer als das

beitreten läuft

SELECT ST_Area (ST_Union (ST_Transform (ST_Intersection (poly1.the_geom, poly2.the_geom), 3857)))

AS area_of_poly

FROM poly2

LEFT JOIN poly1 auf ST_Intersects (poly1.the_geom , poly2.the_geom)

WHERE poly2.polygon_type = 'P'

Allerdings habe ich auf dieser zweiten jo erweitern müssen ined Version mehr Spalten zurückzukehren, berechnet die jeweils mit dem Bereich eines gegebenen Polygontypen, dh

SELECT ST_Area (ST_Union (ST_Transform (ST_Intersection ((poly1.the_geom von poly1 SELECT WHERE poly1.polygon_type = 'P'), poly2.the_geom), 3857))) AS area_of_P,

ST_Area (ST_Union (ST_Transform (ST_Intersection ((SELECT WHERE poly1.the_geom aus poly1 poly1.polygon_type = 'S'), poly2.the_geom), 3857))) AS Bereich_des_S

VON poly1, poly2

Antwort

6

Versuchen Sie dies.

SELECT ST_AREA(ST_Union(ST_Transform(ST_Intersection(poly1.the_geom,poly2.the_geom),3857))) 

AS area_of_poly 

FROM poly2 

LEFT JOIN poly1 on st_intersects(poly1.the_geom,poly2.the_geom) 

WHERE poly2.polygon_type IN ('P', 'S') 

Edit:

SELECT ST_AREA(ST_Union(ST_Transform(ST_Intersection(ps.the_geom,poly2.the_geom),3857))) AS area_of_P, 
     ST_AREA(ST_Union(ST_Transform(ST_Intersection(ss.the_geom,poly2.the_geom),3857))) AS area_of_S 
FROM poly2 
JOIN poly1 ps ON poly2.polygon_type = 'P' AND st_intersects(ps.the_geom,poly2.the_geom) 
JOIN poly1 ss ON poly2.polygon_type = 'S' AND st_intersects(ss.the_geom,poly2.the_geom) 
+0

Sorry, sollte ich dies deutlicher gemacht haben. Ich möchte zwei Spalten zurückgeben. Einer ist der Bereich des Polygontyps "P", der andere ist der Bereich des Polygontyps "S". – John

+0

Siehe die aktualisierte Antwort. –

+0

Funktioniert genau so, wie ich es brauche. Danke, Brett. – John

Verwandte Themen