如何改进这个 SELECT 查询链?
How to improve this chain of SELECT queries?
我有这个功能查询,它在与主 table 中的多边形相交的参考 table 中构建多边形的加权平均值。
是否有比一组嵌套选择更好的构造查询的方法?无论是在性能还是可读性方面。我考虑过使用一系列临时 tables 但这似乎更复杂而不是更少。
UPDATE tmp_master_geom AS master
SET weighted_value = sub_query.weighted_average
FROM
-- calc weighted average column
(SELECT
the_id,
(weighted_sum / total_area) AS weighted_average
-- the_whole_intersection AS geom
FROM
-- group intersection pieces by original id
(SELECT
the_id,
st_union(the_intersection) AS the_whole_intersection,
SUM(the_area) AS total_area,
SUM(the_area * ref_value) AS weighted_sum
FROM
-- get all intersections between master and reference
(SELECT
tmg.ID AS the_id,
st_astext(trgl.geom) AS the_original,
st_astext(st_intersection(tmg.geom, trgl.geom)) AS the_intersection,
st_area(st_intersection(tmg.geom, trgl.geom)) AS the_area,
trgl.ref_value AS ref_value
FROM
tmp_master_geom tmg,
tmp_ref_geoms_larger trgl
where st_intersects(tmg.geom, trgl.geom)
) AS intersection_table
GROUP BY
the_id
) AS sum_table
) AS sub_query
WHERE
master.id = sub_query.the_id
;
我的下一步是将其从查询转换为可重用函数,因此特别感谢考虑到这一点的建议。
Is there a better way to structure the query than a set of nested selects? Both in terms of performance and readability.
如果您担心代码的可读性,我想说您不必担心。但是,如果您主要关心的是性能,您可能需要 EXPLAIN
您的查询以确定可能的瓶颈。
I considered using a series of temporary tables but that seems like it would be more complicated rather than less.
根据结果集的大小,临时 table 可能会提高您的代码的性能!如果您要处理非常大的 table,创建一个临时的 table 并为您正在搜索的列编制索引可能比通过子选择或 CTE 查询更快。
My next step is to convert this from a query into a reusable function so suggestions with that in mind are especially appreciated.
如果你打算将整个东西打包成一个函数,你可以使用这个结构:
CREATE OR REPLACE FUNCTION gimme_the_geom()
RETURNS GEOMETRY AS $BODY$
DECLARE geom GEOMETRY;
BEGIN
--your huge query goes here + INTO geom;
SELECT 'POINT(1 2)'::GEOMETRY INTO geom;
RETURN geom;
END;
$BODY$
LANGUAGE plpgsql;
用法
SELECT gimme_the_geom();
gimme_the_geom
--------------------------------------------
0101000000000000000000F03F0000000000000040
(1 Zeile)
我有这个功能查询,它在与主 table 中的多边形相交的参考 table 中构建多边形的加权平均值。
是否有比一组嵌套选择更好的构造查询的方法?无论是在性能还是可读性方面。我考虑过使用一系列临时 tables 但这似乎更复杂而不是更少。
UPDATE tmp_master_geom AS master
SET weighted_value = sub_query.weighted_average
FROM
-- calc weighted average column
(SELECT
the_id,
(weighted_sum / total_area) AS weighted_average
-- the_whole_intersection AS geom
FROM
-- group intersection pieces by original id
(SELECT
the_id,
st_union(the_intersection) AS the_whole_intersection,
SUM(the_area) AS total_area,
SUM(the_area * ref_value) AS weighted_sum
FROM
-- get all intersections between master and reference
(SELECT
tmg.ID AS the_id,
st_astext(trgl.geom) AS the_original,
st_astext(st_intersection(tmg.geom, trgl.geom)) AS the_intersection,
st_area(st_intersection(tmg.geom, trgl.geom)) AS the_area,
trgl.ref_value AS ref_value
FROM
tmp_master_geom tmg,
tmp_ref_geoms_larger trgl
where st_intersects(tmg.geom, trgl.geom)
) AS intersection_table
GROUP BY
the_id
) AS sum_table
) AS sub_query
WHERE
master.id = sub_query.the_id
;
我的下一步是将其从查询转换为可重用函数,因此特别感谢考虑到这一点的建议。
Is there a better way to structure the query than a set of nested selects? Both in terms of performance and readability.
如果您担心代码的可读性,我想说您不必担心。但是,如果您主要关心的是性能,您可能需要 EXPLAIN
您的查询以确定可能的瓶颈。
I considered using a series of temporary tables but that seems like it would be more complicated rather than less.
根据结果集的大小,临时 table 可能会提高您的代码的性能!如果您要处理非常大的 table,创建一个临时的 table 并为您正在搜索的列编制索引可能比通过子选择或 CTE 查询更快。
My next step is to convert this from a query into a reusable function so suggestions with that in mind are especially appreciated.
如果你打算将整个东西打包成一个函数,你可以使用这个结构:
CREATE OR REPLACE FUNCTION gimme_the_geom()
RETURNS GEOMETRY AS $BODY$
DECLARE geom GEOMETRY;
BEGIN
--your huge query goes here + INTO geom;
SELECT 'POINT(1 2)'::GEOMETRY INTO geom;
RETURN geom;
END;
$BODY$
LANGUAGE plpgsql;
用法
SELECT gimme_the_geom();
gimme_the_geom
--------------------------------------------
0101000000000000000000F03F0000000000000040
(1 Zeile)