2012-06-23 9 views
5

が、私はこのサブクエリを実行すると、このサブクエリを結合として最適化するにはどうすればよいですか?

選択していることに気付きました ST_Areaは(にST_Union(ST_Transform(ST_Intersection((ポリ1からpoly1.the_geomを選択WHERE poly1.polygon_type = 'P')、poly2.the_geom)、3857) area_of_P AS))

ポリ1 FROM、ポリ2

が、これは

に参加し実行するよりも大幅に遅いです

SELECT ST_Areaは(にST_Union(ST_Transform(ST_Intersection(poly1.the_geom、poly2.the_geom)、3857)))

AS area_of_poly

POLY2 FROM

LEFTはST_Intersectsは上POLY1をJOIN(poly1.the_geom poly2.polygon_type = 'P'

は、しかし、私はこの第二のジョー時に拡張する必要があり、poly2.the_geom)

INEDバージョン、すなわち

ST_Areaは(にST_Union(ST_Transform(ST_Intersectionは((POLY1からpoly1.the_geomを選択]を選択し、計算され、所与の多角形タイプの面積と、それぞれ複数の列を返す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 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

申し訳ありませんが、私はこれが明確になされている必要があります。私は2つの列を返したい。 1つはポリゴンタイプ 'P'のエリアで、もう1つはポリゴンタイプ 'S'のエリアです。 – John

+0

更新された回答を参照してください。 –

+0

私が必要とする通りに正確に動作します。 Brettに感謝します。 – John

関連する問題