改进Postgresql中的函数性能

时间:2016-06-10 23:57:10

标签: sql performance postgresql function

以下函数正在创建临时表,然后在其中填充数据。

该临时表应该在点击网站上的按钮时显示。

我尝试提高性能的步骤: 1.修改了postgresql.conf文件。 2.在临时表中添加了索引

CREATE OR REPLACE FUNCTION lastonemonth()
  RETURNS void AS
$BODY$
  DECLARE
  query1 text;
  query2 text;
  var_loop1 RECORD;
  dealerName text;
  cur_minus_2_month text; 
  var_sum_of_quantity numeric;
  var_net_value numeric;
  var_average_price_nsr numeric; 
  BEGIN
  EXECUTE 'DROP TABLE IF EXISTS LOM CASCADE';
  EXECUTE 'CREATE TEMP TABLE LOM ( dealer_name text PRIMARY KEY , sum_of_quantity numeric, net_value numeric, average_price_nsr numeric)';
  EXECUTE 'CREATE UNIQUE INDEX dealer_name_idx ON LOM (dealer_name)';

  query1:= 'SELECT DISTINCT dealer FROM customernotorderdb_temp WHERE month IN ( to_char( now() - interval ''2 month'', ''YYYYMM'') , to_char( now() - interval ''3 month'', ''YYYYMM''))EXCEPT SELECT DISTINCT dealer FROM customernotorderdb_temp WHERE month IN ( to_char( now() - interval ''1 month'', ''YYYYMM''), to_char(now(), ''YYYYMM'') )';
  FOR var_loop1 IN EXECUTE (query1)
  LOOP
      dealerName:= var_loop1.dealer;
      INSERT INTO LOM(dealer_name) VALUES(dealerName);
      EXECUTE 'SELECT month FROM customernotorderdb_temp WHERE dealer LIKE ''%'||dealerName||'%'' AND month IN(to_char( now() - interval ''2 month'', ''YYYYMM''))' INTO cur_minus_2_month;

      --RAISE NOTICE 'cur_minus_2_month( % )', cur_minus_2_month;
      IF cur_minus_2_month IS NOT NULL 
      THEN
          EXECUTE 'SELECT SUM(saleqtypermt) FROM customernotorderdb_temp WHERE dealer LIKE ''%'||dealerName||'%'' AND month IN (to_char( now() - interval ''2 month'', ''YYYYMM''),to_char( now() - interval ''3 month'', ''YYYYMM''),to_char( now() - interval ''4 month'', ''YYYYMM''),to_char( now() - interval ''5 month'', ''YYYYMM''),to_char( now() - interval ''6 month'', ''YYYYMM''),to_char( now() - interval ''7 month'', ''YYYYMM''),to_char( now() -interval ''8 month'', ''YYYYMM''),to_char( now() - interval ''9 month'', ''YYYYMM''),to_char( now() - interval ''10 month'', ''YYYYMM''),to_char( now() - interval ''11 month'', ''YYYYMM''),to_char( now() - interval ''12 month'', ''YYYYMM''),to_char( now() - interval ''13 month'', ''YYYYMM''))' INTO var_sum_of_quantity;
          EXECUTE 'SELECT SUM(basic_value-rate_diff) FROM customernotorderdb_temp WHERE dealer LIKE ''%'||dealerName||'%'' AND month IN (to_char( now() - interval ''2 month'', ''YYYYMM''),to_char( now() - interval ''3 month'', ''YYYYMM''),to_char( now() - interval ''4 month'', ''YYYYMM''),to_char( now() - interval ''5 month'', ''YYYYMM''),to_char( now() - interval ''6 month'', ''YYYYMM''),to_char( now() - interval ''7 month'', ''YYYYMM''),to_char( now() -interval ''8 month'', ''YYYYMM''),to_char( now() - interval ''9 month'', ''YYYYMM''),to_char( now() - interval ''10 month'', ''YYYYMM''),to_char( now() - interval ''11 month'', ''YYYYMM''),to_char( now() - interval ''12 month'', ''YYYYMM''),to_char( now() - interval ''13 month'', ''YYYYMM''))' INTO var_net_value;
          EXECUTE 'SELECT SUM(avgpricensr) FROM customernotorderdb_temp WHERE dealer LIKE ''%'||dealerName||'%'' AND month IN (to_char( now() - interval ''2 month'', ''YYYYMM''),to_char( now() - interval ''3 month'', ''YYYYMM''),to_char( now() - interval ''4 month'', ''YYYYMM''),to_char( now() - interval ''5 month'', ''YYYYMM''),to_char( now() - interval ''6 month'', ''YYYYMM''),to_char( now() - interval ''7 month'', ''YYYYMM''),to_char( now() -interval ''8 month'', ''YYYYMM''),to_char( now() - interval ''9 month'', ''YYYYMM''),to_char( now() - interval ''10 month'', ''YYYYMM''),to_char( now() - interval ''11 month'', ''YYYYMM''),to_char( now() - interval ''12 month'', ''YYYYMM''),to_char( now() - interval ''13 month'', ''YYYYMM''))' INTO var_average_price_nsr;
          --RAISE NOTICE 'A [ %   % ]', dealername,var_net_value;
          UPDATE LOM SET sum_of_quantity=var_sum_of_quantity,
                         net_value=var_net_value,
                         average_price_nsr=var_average_price_nsr 
          WHERE dealer_name=var_loop1.dealer;

      ELSE
          EXECUTE 'SELECT SUM(saleqtypermt) FROM customernotorderdb_temp WHERE dealer LIKE ''%'||dealerName||'%'' AND month IN (to_char( now() - interval ''3 month'', ''YYYYMM''),to_char( now() - interval ''4 month'', ''YYYYMM''),to_char( now() - interval ''5 month'', ''YYYYMM''),to_char( now() - interval ''6 month'', ''YYYYMM''),to_char( now() - interval ''7 month'', ''YYYYMM''),to_char( now() -interval ''8 month'', ''YYYYMM''),to_char( now() - interval ''9 month'', ''YYYYMM''),to_char( now() - interval ''10 month'', ''YYYYMM''),to_char( now() - interval ''11 month'', ''YYYYMM''),to_char( now() - interval ''12 month'', ''YYYYMM''),to_char( now() - interval ''13 month'', ''YYYYMM''),to_char( now() - interval ''14 month'', ''YYYYMM''))' INTO var_sum_of_quantity;
          EXECUTE 'SELECT SUM(basic_value-rate_diff) FROM customernotorderdb_temp WHERE dealer LIKE ''%'||dealerName||'%'' AND month IN (to_char( now() - interval ''3 month'', ''YYYYMM''),to_char( now() - interval ''4 month'', ''YYYYMM''),to_char( now() - interval ''5 month'', ''YYYYMM''),to_char( now() - interval ''6 month'', ''YYYYMM''),to_char( now() - interval ''7 month'', ''YYYYMM''),to_char( now() -interval ''8 month'', ''YYYYMM''),to_char( now() - interval ''9 month'', ''YYYYMM''),to_char( now() - interval ''10 month'', ''YYYYMM''),to_char( now() - interval ''11 month'', ''YYYYMM''),to_char( now() - interval ''12 month'', ''YYYYMM''),to_char( now() - interval ''13 month'', ''YYYYMM''),to_char( now() - interval ''14 month'', ''YYYYMM''))' INTO var_net_value;
          EXECUTE 'SELECT SUM(avgpricensr) FROM customernotorderdb_temp WHERE dealer LIKE ''%'||dealerName||'%'' AND month IN (to_char( now() - interval ''3 month'', ''YYYYMM''),to_char( now() - interval ''4 month'', ''YYYYMM''),to_char( now() - interval ''5 month'', ''YYYYMM''),to_char( now() - interval ''6 month'', ''YYYYMM''),to_char( now() - interval ''7 month'', ''YYYYMM''),to_char( now() -interval ''8 month'', ''YYYYMM''),to_char( now() - interval ''9 month'', ''YYYYMM''),to_char( now() - interval ''10 month'', ''YYYYMM''),to_char( now() - interval ''11 month'', ''YYYYMM''),to_char( now() - interval ''12 month'', ''YYYYMM''),to_char( now() - interval ''13 month'', ''YYYYMM''),to_char( now() - interval ''14 month'', ''YYYYMM''))' INTO var_average_price_nsr;
          --RAISE NOTICE 'B [ %  % ]', dealername,var_net_value;
          UPDATE LOM SET sum_of_quantity=var_sum_of_quantity,net_value=var_net_value,average_price_nsr=var_average_price_nsr WHERE dealer_name=var_loop1.dealer;
      END IF;         
 END LOOP;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE 

1 个答案:

答案 0 :(得分:0)

首先,看起来您将月份存储为字符串并手动与字符串进行日期比较。这会让它变慢。你可以用Postgres日期类型或整数代替吗?

对于其他事情,我不知道你不知道你的数据库布局和统计数据,但这是优化查询的基本方法:

  1. 使用psql或其他东西打开一个新连接。
  2. set enable_seqscan = off,仅在此会话中禁用查询计划程序中的seq扫描(除非完全必要)。
  3. 在查询之前运行EXPLAIN,以查看哪些人被迫扫描大表。如果看到任何SEQ SCAN s。
  4. ,请添加索引