2012-06-23 27 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