1

我一直在尝试创建一个视图,其中列 pending_amount 之一由于存储过程的执行而获得其值。

存储过程是pending_stock(int,int)并返回一个整数。该视图已成功创建,但是当我尝试在此视图上执行任何查询(如选择)时,它需要返回一个值。

CREATE OR REPLACE VIEW view_production_parts AS 
SELECT p.part_id, p.min_amount, gp.part_num, gp.description
     , p.quantity_available
     , p.quantity_total - p.quantity_available AS quantity_alloc
     , p.quantity_total
     , (SELECT pending_stock(p.part_id, 0) AS pending_stock) AS pending_amount
     , p.production_run
     , CASE
           WHEN ppur.purchased_part_id IS NOT NULL THEN true
           ELSE false
       END AS is_purchased_part, ppur.purchased_part_id, p.store_move_type_id
     , gp.part_status_id, p.default_location
     , COALESCE(pwoh.part_work_order_hold_id, 0) AS part_work_order_hold_id
   FROM general_part gp
   JOIN part p ON gp.part_id = p.part_id
   LEFT JOIN purchased_part ppur ON ppur.part_id = p.part_id
   LEFT JOIN part_work_order_hold pwoh ON pwoh.part_id = p.part_id
  ORDER BY gp.part_num;

可以在视图中使用存储过程吗?如果使用,我的声明是否正确?

在 explain.depesz.com 上查找此查询的结果:

EXPLAIN ANALYZE SELECT count(*) FROM view_production_parts

我正在使用 Postgres 8.4。
的函数定义pending_stock(int,int)

CREATE OR REPLACE FUNCTION pending_stock(var_part_id integer
                                       , var_pattern_id integer)
  RETURNS integer AS
$BODY$
declare
    r record;
    var_qty_expected integer;
    var_qty_moved_to_stock integer;
    var_total_stock_moved_out integer;
    var_actual_qty integer;

begin

var_total_stock_moved_out := 0;
var_qty_expected := 0;

   for r in
      select work_order_id,quantity_expected
      from view_work_orders
      where part_id = var_part_id and open = 'TRUE'
      and quantity_allocated is null and quantity_expected >= quantity_actual

   loop
      var_qty_expected = var_qty_expected + r.quantity_expected;

      select sum(quantity) from view_work_order_move_parts_details
      where source_work_order_id = r.work_order_id
      and part_id = var_part_id into var_qty_moved_to_stock;

      if var_qty_moved_to_stock is null then
         var_qty_moved_to_stock = 0;
      end if;

      var_total_stock_moved_out = var_total_stock_moved_out
                                + var_qty_moved_to_stock;
   end loop;

   var_actual_qty := var_qty_expected - var_total_stock_moved_out;

   if var_actual_qty > 0 then
      return var_actual_qty;
   else
      return 0;
   end if;
end;
$BODY$
LANGUAGE 'plpgsql' VOLATILE STRICT
COST 100;
ALTER FUNCTION pending_stock(integer, integer) OWNER TO postgres;
4

1 回答 1

2

看法

您不需要函数调用的子查询。您还可以简化其他一些小细节:

CREATE OR REPLACE VIEW view_production_parts AS 
SELECT  p.part_id, p.min_amount
      , gp.part_num, gp.description, p.quantity_available
      , p.quantity_total - p.quantity_available AS quantity_alloc
      , p.quantity_total
      , pending_stock(gp.part_id, 0) AS pending_amount
      , p.production_run
      ,(ppur.purchased_part_id IS NOT NULL) AS is_purchased_part
      , ppur.purchased_part_id, p.store_move_type_id, gp.part_status_id
      , p.default_location
      , COALESCE(pwoh.part_work_order_hold_id, 0) AS part_work_order_hold_id
FROM    general_part              gp
JOIN    part                      p    USING (part_id)
LEFT    JOIN purchased_part       ppur USING (part_id)
LEFT    JOIN part_work_order_hold pwoh USING (part_id)
ORDER   BY gp.part_num;

除此之外,VIEW 定义看起来不错。

功能

可以大大简化:

CREATE OR REPLACE FUNCTION pending_stock(var_part_id integer
                                       , var_pattern_id integer)
  RETURNS integer AS
$func$
DECLARE
    r record;
    var_qty_expected            integer   := 0;
    var_total_stock_moved_out   integer   := 0;
BEGIN
   FOR r IN
      SELECT work_order_id, quantity_expected
      FROM   view_work_orders
      WHERE  part_id = var_part_id
      AND    open = 'TRUE'  -- A string instead of a boolean?
      AND    quantity_allocated IS NULL
      AND    quantity_expected >= quantity_actual
   LOOP
      var_qty_expected := var_qty_expected + r.quantity_expected;

      SELECT var_total_stock_moved_out + COALESCE(sum(quantity), 0)
      FROM   view_work_order_move_parts_details
      WHERE  source_work_order_id = r.work_order_id
      AND    part_id = var_part_id
      INTO   var_total_stock_moved_out;
   END LOOP;

   RETURN GREATEST(var_qty_expected - var_total_stock_moved_out, 0);
END
$func$ LANGUAGE plpgsql

要点

  • 一般来说,在 plpgsql 中赋值是比较昂贵的。每个赋值都是在内部使用一个(非常简单和快速的)SELECT语句执行的。尽量少用它们。

  • 您可以在声明时初始化变量。无需另作声明。

  • plpgsql 中的赋值运算符是:=. =有效,但没有记录。

  • 用于COALESCE()捕获NULL值。

  • 函数参数var_pattern_id从不使用。这可能不是完整的函数定义。

  • 整个最后部分可以用单个语句替换GREATEST

高级查询

现在,这个清理后的功能会快一点,但不会太多。您重复循环的整个设计效率极低。它导致相关子查询再次循环通过相关子查询。性能噩梦。

将问题重铸为基于集合的操作以使其更快。嗯,快了很多。

SELECT e.part_id
      ,GREATEST(COALESCE(sum(e.quantity_expected), 0)
              - COALESCE(sum(m.total_stock_moved_out), 0), 0)
FROM   view_work_orders e
LEFT   JOIN (
   SELECT source_work_order_id       AS work_order_id
         ,COALESCE(sum(quantity), 0) AS total_stock_moved_out
   FROM   view_work_order_move_parts_details
   WHERE  part_id = var_part_id
   GROUP  BY 1
   ) m USING (work_order_id)
WHERE  e.part_id            =  var_part_id
AND    e.open               =  'TRUE'
AND    e.quantity_allocated IS NULL
AND    e.quantity_expected  >= e.quantity_actual
GROUP  BY 1;

优越的视野

将此集成到原始查询/视图中:

CREATE OR REPLACE VIEW view_production_parts AS 
SELECT p.part_id, p.min_amount
      ,gp.part_num, gp.description, p.quantity_available
      ,p.quantity_total - p.quantity_available AS quantity_alloc
      ,p.quantity_total
      ,x.pending_amount
      ,p.production_run
      ,(ppur.purchased_part_id IS NOT NULL) AS is_purchased_part
      ,ppur.purchased_part_id, p.store_move_type_id, gp.part_status_id
      ,p.default_location
      ,COALESCE(pwoh.part_work_order_hold_id, 0) AS part_work_order_hold_id
FROM   general_part              gp
JOIN   part                      p    USING (part_id)
LEFT   JOIN purchased_part       ppur USING (part_id)
LEFT   JOIN part_work_order_hold pwoh USING (part_id)
LEFT   JOIN (
   SELECT e.part_id
         ,GREATEST(COALESCE(sum(e.quantity_expected), 0)
                 - COALESCE(sum(m.total_stock_moved_out), 0)
                   , 0) AS pending_amount
   FROM   view_work_orders e
   LEFT   JOIN (
      SELECT source_work_order_id AS work_order_id
            ,sum(quantity)        AS total_stock_moved_out
      FROM   view_work_order_move_parts_details
      WHERE  part_id = var_part_id
      GROUP  BY 1
      ) m USING (work_order_id)
   WHERE  e.part_id            =  var_part_id
   AND    e.open               =  'TRUE'
   AND    e.quantity_allocated IS NULL
   AND    e.quantity_expected  >= e.quantity_actual
   GROUP  BY 1
   ) x USING (part_id)
ORDER  BY gp.part_num;

显然,未经测试。

于 2013-10-17T16:45:51.937 回答