2012-06-23 37 views
5

我注意到,運行此子查詢如何優化此子查詢作爲連接?

選擇 ST_Area(ST_Union(ST_Transform(ST_Intersection((選擇POLY1 poly1.the_geom WHERE poly1.polygon_type = 'P'),poly2.the_geom),3857) ))

AS area_of_P FROM POLY1,POLY2

比運行此連接顯著慢

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

AS area_of_poly

FROM POLY2

LEFT JOIN上st_intersects POLY1(poly1.the_geom ,poly2.the_geom)

WHERE poly2.polygon_type = 'P'

然而,我需要在這個第二裘擴大INED版本返回多個列,每個計算出的給定的多邊形類型的區域,即

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

ST_Area(ST_Union(ST_Transform(ST_Intersection((從POLY1 WHERE poly1.polygon_type = 'S'),poly2.the_geom),3857 SELECT poly1.the_geom ))) AS area_of_S

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

完全按照我需要的那樣工作。謝謝佈雷特。 – John