sql - Improve Function performance in Postgresql -


following function creating temp table , populating data in it.

that temp table supposed displayed on click of button on website.

steps tried improve performance: 1. modified postgresql.conf file. 2. added index in temp table

create or replace function lastonemonth()   returns void $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 customernotorderdb_temp month in ( to_char( now() - interval ''2 month'', ''yyyymm'') , to_char( now() - interval ''3 month'', ''yyyymm''))except select distinct dealer customernotorderdb_temp month in ( to_char( now() - interval ''1 month'', ''yyyymm''), to_char(now(), ''yyyymm'') )';   var_loop1 in execute (query1)   loop       dealername:= var_loop1.dealer;       insert lom(dealer_name) values(dealername);       execute 'select month customernotorderdb_temp dealer ''%'||dealername||'%'' , month in(to_char( now() - interval ''2 month'', ''yyyymm''))' cur_minus_2_month;        --raise notice 'cur_minus_2_month( % )', cur_minus_2_month;       if cur_minus_2_month not null                  execute 'select sum(saleqtypermt) customernotorderdb_temp dealer ''%'||dealername||'%'' , 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''))' var_sum_of_quantity;           execute 'select sum(basic_value-rate_diff) customernotorderdb_temp dealer ''%'||dealername||'%'' , 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''))' var_net_value;           execute 'select sum(avgpricensr) customernotorderdb_temp dealer ''%'||dealername||'%'' , 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''))' 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            dealer_name=var_loop1.dealer;        else           execute 'select sum(saleqtypermt) customernotorderdb_temp dealer ''%'||dealername||'%'' , 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''))' var_sum_of_quantity;           execute 'select sum(basic_value-rate_diff) customernotorderdb_temp dealer ''%'||dealername||'%'' , 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''))' var_net_value;           execute 'select sum(avgpricensr) customernotorderdb_temp dealer ''%'||dealername||'%'' , 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''))' 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 dealer_name=var_loop1.dealer;       end if;           end loop; end; $body$   language plpgsql volatile  

first of all, looks you're storing months strings , manually doing date comparisons strings. that'll slow down. postgres date types or integers instead?

for other things, can't tell without knowing database layout , statistics, here's basic method optimizing queries:

  1. open new connection using psql or something.
  2. set enable_seqscan = off, disables seq scan in query planner (unless necessary) session.
  3. run explain before queries see ones being forced scan large tables. add indexes if see seq scans.

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -