Dê uma olhada nisso, deve lhe ajudar:

http://www.postgresql.org/docs/8.4/static/queries-union.html

Dica, resolva o problema somente com SQL primeiro, gerando EXPLAIN
para as suas consultas e só depois que estiver satisfeito, passe para
o PL.

Espero ter ajudado.

[]s
Fábio Telles

Em 12 de fevereiro de 2010 11:27, Tiago Valério
<[email protected]> escreveu:
> Srs
>
> Estou com o seguinte problema , tenho a funcao abaixo que utilizo para
> manipular os registros da tabela_1 e tabela_2 , sendo que a tabela_1 possui
> aproximandamente 20 milhoes e a tabela 2 possui aproximadamente 10 milhoes
> de registros.
>
> Tentei descrever a funcao  de uma maneira sucinta,porem o objetivo da mesma
> sera manter dados historicos quando o registro da tabela_1 for diferente da
> tabela_2, irei fazer isto com as outras interacoes na function.
>
> Mas meu problema esta na performance da mesma, para fazer a leitura dos
> registros, ela demora mto tempo.Gostaria de alguma dica para tentar resolver
> este problema.
>
> Desde ja grato pela atencao.
>
>
>
>
>
>
>
>
>
> CREATE OR REPLACE FUNCTION teste_funcao (porcao integer)
>   RETURNS boolean AS
> $BODY$
> DECLARE
>
>
>  resultado boolean;
>  reg_proc record;
>  reg_rel record;
>  stringCnpjs text ='';
>  first boolean =true;
>
>
>
> BEGIN
>
>  resultado := true;
>
>
>
>  --BEGIN
>  for reg_proc in
>
>
>
>   select
>    col_1 ,
>    col_2 ,
>    col_4 ,
>    col_5 ,
>    col_6,
>    col_7,
>    col_8,
>    col_9,
>    col_10,
>    col_11,
>    col_12,
>    col_13,
>    col_14,
>    col_15,
>    col_16,
>    col_17,
>    col_18,
>    col_19,
>    col_20,
>    col_21,
>    col_22,
>    col_23,
>    col_24
>
>    from tabela_1
>     where  a.col_1=2  and col_2=0
>     limit porcao
>
> loop
>
>  select
>       col_1,
>       col_2,
>       col_3,
>       col_4,
>       col_5,
>       col_6,
>       col_7,
>       col_8,
>       col_9,
>       col_10,
>       col_11,
>       col_12,
>       col_13,
>       col_14,
>       col_15,
>       col_16,
>       col_17,
>       col_18,
>       col_19
>       into reg_rel ---armazena no cursor implicito da tabela de
> relacionamento
>   from tabela_2     rf
>    where  rf.col1=reg_proc.col2;
>
>
>
>
>
>
>
>  If found then
>
>
>
>    begin
>
>      update tabela_2
>       set col_1=reg_proc.col_1,
>        col_2=reg_proc.col_2
>      where  col1=reg_proc.col2;
>    end;
>
>
>  end loop;
>
>
>     return true;
> --EXCEPTION
>  --when others then return false;
> END;
> $BODY$
>   LANGUAGE 'plpgsql' VOLATILE
>   COST 10000;
>
> _______________________________________________
> pgbr-geral mailing list
> [email protected]
> https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral
>
>



-- 
blog: http://www.midstorm.org/~telles/
e-mail / jabber: [email protected]
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a