pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #14464: Problems about FUNCTIONS
2+ messages / 2 participants
[nested] [flat]

* BUG #14464: Problems about FUNCTIONS
@ 2016-12-13 16:42  syspegasus@gmail.com
  0 siblings, 1 reply; 2+ messages in thread

From: syspegasus@gmail.com @ 2016-12-13 16:42 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      14464
Logged by:          Sys Pegasus
Email address:      syspegasus@gmail.com
PostgreSQL version: 9.6.1
Operating system:   Windows 10 64bits
Description:        

Hi, I'm with problems about functions.
When I execute a select with Function is very slow, and when I execute
repeating the code of the existing function inside my SQL is faster. 
I need to centralize my code, because of that I'm using functions, but is
very very slow.. 
Please how can I use Function and become faster? I need to centralize the
SQL.

See my examples... 

MY FUNCTION:
CREATE OR REPLACE FUNCTION sp_valor (
  parcodfilial numeric,
  parcodpedido numeric,
  parcoditem numeric,
  parcodproduto numeric,
  partipodif char
)
RETURNS numeric AS
$body$
DECLARE    
    VALOR NUMERIC;
BEGIN  
  SELECT COALESCE(SUM(A.VLRUNIT),0) INTO VALOR
  FROM FAT0013 A
  WHERE A.CODFILIAL = PARCODFILIAL
    AND A.CODPEDIDO = PARCODPEDIDO
    AND A.CODITEM   = PARCODITEM
    AND A.CODPRODUTO = PARCODPRODUTO
    AND A.TIPODIF = PARTIPODIF;
  RETURN VALOR;
END;
$body$
LANGUAGE 'plpgsql'
VOLATILE;


SQL FAST (00:17):
SELECT A.CODFILIAL,
              A.CODPEDIDO,             
  	     (SELECT COALESCE(SUM(X.VLRUNIT),0) VALOR
              FROM FAT0013 X
             WHERE X.CODFILIAL = A.CODFILIAL
                  AND X.CODPEDIDO = A.CODPEDIDO
                  AND X.CODITEM   = A.CODITEM
                  AND X.CODPRODUTO = A.CODPRODUTO
                 AND X.TIPODIF = 'A')
FROM FAT0002 A


SQL SLOW (04:54):
SELECT A.CODFILIAL,
              A.CODPEDIDO,
              SP_VALOR(A.CODFILIAL, A.CODPEDIDO, A.CODITEM, A.CODPRODUTO,
'A')
FROM FAT0002 A


Thanks.


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs


^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: BUG #14464: Problems about FUNCTIONS
@ 2016-12-13 19:52  David G. Johnston <david.g.johnston@gmail.com>
  parent: syspegasus@gmail.com
  0 siblings, 0 replies; 2+ messages in thread

From: David G. Johnston @ 2016-12-13 19:52 UTC (permalink / raw)
  To: Ágile Sistema <syspegasus@gmail.com>; +Cc: pgsql-bugs

On Tue, Dec 13, 2016 at 9:42 AM, <syspegasus@gmail.com> wrote:

> The following bug has been logged on the website:
>
> Bug reference:      14464
>
> Hi, I'm with problems about functions.
> When I execute a select with Function is very slow, and when I execute
> repeating the code of the existing function inside my SQL is faster.
> I need to centralize my code, because of that I'm using functions, but is
> very very slow..
> Please how can I use Function and become faster? I need to centralize the
> SQL
>

This list is intended for reporting bugs in PostgreSQL.  This request for
help is not that.  Please choose a more appropriate list -
pgsql-general@postgresql.org suffices as a catch-all.

​That said, you'll likely find improvement by choosing the correct language
to write your function in.  Namely, choose SQL unless and until you are
forced to choose pl/pgsql.  As you are simply embedding a single SELECT
query you do not use any features that are only present in pl/pgsql.

David J.

^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2016-12-13 19:52 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-12-13 16:42 BUG #14464: Problems about FUNCTIONS syspegasus@gmail.com
2016-12-13 19:52 ` David G. Johnston <david.g.johnston@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox