PostgreSQL函数需要很长时间才能完成执行

时间:2016-11-25 06:21:16

标签: java postgresql jdbc plpgsql

我有一个plpgsql函数,它在循环中调用另一个postgres函数,而执行内部循环的main函数调用9个函数来处理一些数据。

当我从JDBC调用此函数时,执行时间为60-80分钟。但是,如果我通过pgAdmin(PostgreSQL数据库客户端)调用它,它仅在10-12分钟内执行。

任何线索为什么表现得太慢?

我正在使用:

数据库:“PostgreSQL 9.3.4,由Visual C ++ build 1600,64位编译”

服务器:“Windows Server 2008 R2”

驱动程序: postgresql 9.3-1100.jdbc4.jar

功能代码如下:

主要功能

CREATE OR REPLACE FUNCTION intermediatedata.sp_calculate_ix_cell_level()
  RETURNS void AS
$BODY$
declare  


 --Local Variables 
   temp1 character varying;
   temp3 timestamp without time zone; 
   temp4 timestamp without time zone; 
   temp5 timestamp without time zone;  
   startt timestamp without time zone;
   endt timestamp without time zone;
   edt timestamp without time zone;
   currhour double precision;
   enddt date;
   counter integer;
   i integer;
 -------------------------------------
begin
         --Set counter value here
          counter:=3;

          i:=1; 
          startt:=null;
      select current_date into enddt;

     select extract(hour from current_time - interval '1 hour' ) into currhour;  

     endt:=enddt||' '||currhour||':00';

     --Retriving lasr processing time              
        select intermediatedata.ix_cell_level_processing.end_time into startt from 
                   intermediatedata.ix_cell_level_processing order by intermediatedata.ix_cell_level_processing.id desc limit 1; 

          if startt is null
            then
              startt=enddt - interval '24 hour';
            end if;
             temp5=startt;        

    LOOP
        temp4=startt + interval '1 hour';  

        EXECUTE 'SELECT intermediatedata.sp_ix_cell_level(
                        '''||startt||''',
                        '''||temp4||'''      
                        )' into temp1;

        startt=startt + interval '1 hour';

        if startt > endt or i>=counter
        then
            EXIT ;
        end if; 

        raise notice '%',i;

        i=i+1;
     END LOOP;   


end;
 $BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION intermediatedata.sp_calculate_ix_cell_level()
  OWNER TO postgres;

循环执行的功能

CREATE OR REPLACE FUNCTION intermediatedata.sp_ix_cell_level(startt timestamp without time zone, endt timestamp without time zone)
  RETURNS void AS
$BODY$
declare  


 --Local Variables   
   temp1 character varying; 
   temp3 timestamp without time zone;  
  -- sdt timestamp without time zone;
   edt timestamp without time zone;
 -------------------------------------
begin      
          --Here inserting data in intermediatedata.ix_cell_level table  column 9,10,11,12
    EXECUTE 'SELECT intermediatedata.sp_ix_cell_level_column3_4_5_6_7_8_9_10_11_41_42_43_44_45_46(
        '''||startt||''',
        '''||endt||'''
        )' into temp3;

        --Here inserting data in intermediatedata.ix_cell_level table  column 3,4,5
    EXECUTE 'SELECT intermediatedata.sp_ix_cell_level_column19_20_21_22_23_24(
        '''||temp3||''',
        '''||endt||'''
        )' into temp1;

        --Here inserting data in intermediatedata.ix_cell_level table  column 14,15,16,17
    EXECUTE 'SELECT intermediatedata.sp_ix_cell_level_column12_13_14_15_16_17_18(
        '''||temp3||''',
        '''||endt||'''
        )' into temp1;


        --Here inserting data in intermediatedata.ix_cell_level table  column 9,10,11,12
    EXECUTE 'SELECT intermediatedata.sp_ix_cell_level_column30_31_32_33_34(
        '''||temp3||''',
        '''||endt||'''
        )' into temp1;


     --Here inserting data in intermediatedata.ix_cell_level table  column 9,10,11,12
    EXECUTE 'SELECT intermediatedata.sp_ix_cell_level_column35_36_37_38_39_40(
        '''||temp3||''',
        '''||endt||'''
        )' into temp1;

     --Here inserting data in intermediatedata.ix_cell_level table  column 18,19,20,21,22,23,24
    EXECUTE 'SELECT intermediatedata.sp_ix_cell_level_column25_26_27_28_29(
        '''||temp3||''',
        '''||endt||'''
        )' into temp1;

     --Here inserting data in intermediatedata.ix_cell_level table  column 47
    EXECUTE 'SELECT intermediatedata.sp_ix_cell_level_column47(
        '''||temp3||''',
        '''||endt||'''
        )' into temp1;


     --Here inserting data in intermediatedata.ix_cell_level table  column 47
    EXECUTE 'SELECT intermediatedata.sp_ix_cell_level_column48(
        '''||temp3||''',
        '''||endt||'''
        )' into temp1;

     --Here inserting data in intermediatedata.ix_cell_level table  column 47
    EXECUTE 'SELECT intermediatedata.sp_ix_cell_level_column51(
        '''||temp3||''',
        '''||endt||'''
        )' into temp1;          


        select max(start_time)+ interval '1 hour' into edt from intermediatedata.ix_cell_level;




        if edt is not null 
         then                         
        INSERT INTO intermediatedata.ix_cell_level_processing(start_time, end_time, status)
                                                         VALUES ( temp3, edt, 'ok');
         else
                INSERT INTO intermediatedata.ix_cell_level_processing(start_time, end_time, status)
                                                         VALUES ( temp3, temp3, 'Raw Data Not Found');                                                    
        end if; 




end
 $BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION intermediatedata.sp_ix_cell_level(timestamp without time zone, timestamp without time zone)
  OWNER TO postgres;

0 个答案:

没有答案