2012-06-23 16 views
5

me he dado cuenta de que la ejecución de este sub consulta¿Cómo puedo optimizar esta subconsulta como unirse?

SELECCIONAR ST_Area (ST_Union (ST_Transform (ST_Intersection ((SELECT poly1.the_geom de poly1 DONDE poly1.polygon_type = 'P'), poly2.the_geom), 3857)))

AS area_of_P dE poly1, poly2

es significativamente más lento que la ejecución de este unirse

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

AS area_of_poly

DE poly2

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

DONDE poly2.polygon_type = 'P'

Sin embargo, necesito de ampliar esta segunda jo versión ined para volver más columnas, cada una con el área de un tipo polígono dado calculado, es decir

SELECCIONAR ST_Area (ST_Union (ST_Transform (ST_Intersection ((SELECT poly1.the_geom de poly1 DONDE poly1.polygon_type = 'P'), poly2.the_geom), 3857))) AS area_of_P,

ST_Area (ST_Union (ST_Transform (ST_Intersection ((SELECT poly1.the_geom de poly1 DONDE poly1.polygon_type = 'S'), poly2.the_geom), 3857))) AS area_of_S

DESDE poly1, poly2

Respuesta

6

Pruebe esto.

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') 

Editar:

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

Lo sentimos, debería haber hecho esto más claro. Quiero devolver dos columnas. Una es el área del tipo de polígono 'P', la otra es el área del tipo de polígono 'S'. – John

+0

Ver la respuesta actualizada. –

+0

Funciona exactamente como lo necesito. Gracias Brett. – John

Cuestiones relacionadas