2012-06-23 3 views
5

나는이 하위 쿼리를 실행하는이 부속 조회를 결합으로 어떻게 최적화 할 수 있습니까?

SELECT 것으로 나타났습니다 에서 ST_Area (ST_Union은 (ST_Transform (ST_Intersection에 ((poly1의에서 poly1.the_geom을 선택 WHERE poly1.polygon_type = 'P'), poly2.the_geom), 3857)))

이 조인 실행보다 훨씬 느린 poly1의 FROM area_of_P , poly2 AS

,174,515 15,

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

AS area_of_poly

poly2 FROM

왼쪽 st_intersects에 poly1의 가입 (poly1.the_geom , poly2.the_geom) poly2.polygon_type = 'P'그러나

, 나는이 두 번째 조에 따라 확장 할 필요가

이네 버전 즉

에서 ST_Area (ST_Union은 (ST_Transform (ST_Intersection에은 ((poly1의으로부터 poly1.the_geom을 선택 SELECT 계산 주어진 다각형 형의 영역으로 각각 더 많은 열을 반환 WHERE poly1.polygon_type = 'P') poly2.the_geom), 3857))) area_of_P AS ,

에서 ST_Area (ST_Union은 (ST_Transform (ST_Intersection에 ((WHERE poly1.polygon_type = 'S') poly2.the_geom), 3857에서 poly1의 poly1.the_geom를 선택))) AS 면적

FROM poly1, poly2

답변

6

시도해보십시오.

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

편집 :

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

미안 해요, 난이 명확를 만든 것이다. 두 개의 기둥을 반환하고 싶습니다. 하나는 폴리곤 유형 'P'의 영역이고, 다른 하나는 폴리곤 유형 'S'의 영역입니다. – John

+0

업데이트 된 답변보기 –

+0

내가 필요한만큼 정확하게 작동합니다. Brett에게 감사드립니다. – John

관련 문제