Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PostgreSQL函数比硬编码查询慢10倍_Postgresql - Fatal编程技术网

PostgreSQL函数比硬编码查询慢10倍

PostgreSQL函数比硬编码查询慢10倍,postgresql,Postgresql,如果我将下面的函数硬编码到查询中,它的处理速度将提高10倍。。。关于如何让函数运行得那么快有什么想法吗 我认为编写sql函数的一个优点是,与PL语言函数相比,查询计划器在此类函数上功能齐全 顺便说一下,我正在使用PostgreSQL 9.4 更新 我现在意识到,执行速度的差异并不是来自将查询放入函数中,而是来自如何调用函数 select*from spatical.aggregate\u graster\u stats\u by\u geom(155)>>1.5秒 select(space.a

如果我将下面的函数硬编码到查询中,它的处理速度将提高10倍。。。关于如何让函数运行得那么快有什么想法吗

我认为编写sql函数的一个优点是,与PL语言函数相比,查询计划器在此类函数上功能齐全

顺便说一下,我正在使用PostgreSQL 9.4


更新

我现在意识到,执行速度的差异并不是来自将查询放入函数中,而是来自如何调用函数


select*from spatical.aggregate\u graster\u stats\u by\u geom(155)>>1.5秒

select(space.aggregate\u graster\u stats\u by\u geom(155))*>>15秒



啊。你遗漏了重要的细节

在PostgreSQL(至少在9.5及更早版本中)中:

。。。为每个结果列运行一次
f

这基本上是一种宏观扩张


要解决此问题,请在另一层子查询中换行

@wildplasser 9.2及更新版本应在参数绑定时自动指定计划,因此此问题已基本解决。除非有什么原因,否则不能在这里执行。@matthew请显示独立运行的
explain(buffers,analyze)
。然后尝试通过SQL级别的
PREPARE
EXPLAIN-ANALYZE-EXECUTE
运行它。那么慢吗?如果是的话,把两个计划都公布出来。如果仍然很快,请启用
auto_explain
模块,并启用解释嵌套语句和分析模式,然后获取函数中嵌入的查询计划并发布。@CraigRinger感谢您的提示。我做了解释,但突然之间,这个函数和独立查询一样快。然后我意识到我调用了不同的函数。有关更多详细信息,请参见更新的问题。
select*from spatical.aggregate\u graster\u stats\u by\u geom(155)是调用返回表的函数的唯一正确方法。该函数返回表-应使用
select*from function()
anywayWow调用该函数。知道这件事真是太好了。这也意味着我的
(ST_SummaryStats(clip,band,TRUE)).
没有我希望的那么高效。相关链接:
CREATE OR REPLACE FUNCTION spatial.aggregate_raster_stats_by_geom(
  IN arg_rid INTEGER
) 
-- This function is called by the trigger that is fired whenever an entry is created in the raster catalog.
RETURNS TABLE(band INTEGER,gid INTEGER, rid INTEGER, product_id INTEGER,ref_datetime TIMESTAMP ,scale INTEGER, count BIGINT, sum FLOAT, mean FLOAT, stddev FLOAT, min FLOAT, max FLOAT) AS
$$
SELECT
    band,
    gid,
    arg_rid as rid,
    product_id,
    ref_datetime,
    scale,
    (ST_SummaryStats(clip,band,TRUE)).* -- compute summary statistics (min and max, etc are also in there). TRUE indicates that nodata should be ignored.
    FROM
      (SELECT
        gid,
        ST_Union(ST_Clip(rast, geom)) as clip -- assemble the raster tiles and clip them with the assembled polygons
      FROM 
        spatial.raster_tiles AS r
      JOIN 
        spatial.geom_catalog AS polygons
        ON ST_Intersects(rast,polygons.geom) -- only select raster tiles that touch the polygons. Spatial indexes should make this fast
      JOIN
        spatial.geom_metadata AS geometa
        ON geometa.product_id = polygons.product_id
      WHERE 
        geometa.aggregate_raster_auto = TRUE
        AND r.rid=$1

      GROUP by gid
      ) as foo
    cross join (
    -- Join bands to the selection
    -- this join has to be introduced AFTER the clipping. If done before, then clipping will be performed for each band.
        SELECT 
          generate_series(md.band_data_start,band_count) as band,
          data_scale_factor as scale,
          md.product_id,
          rid, 
          ref_datetime
        FROM spatial.raster_metadata md 
       JOIN spatial.raster_catalog rst ON rst.product_id = md.product_id
       WHERE rst.rid = $1) AS bar2
$$
  LANGUAGE sql IMMUTABLE;
SELECT ( f () ).*;