pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Robert Haas <robertmhaas@gmail.com>
To: Igor Schtein <ischtein@gmail.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: Performance of SQL Function versus View
Date: Tue, 3 Apr 2012 10:21:10 -0400
Message-ID: <CA+TgmoaOv+CKkw9fKTid7Vca+abAHSakoG8cn7hb7O2mBmaW-g@mail.gmail.com> (raw)
In-Reply-To: <021a01ccf722$059a2a80$10ce7f80$@gmail.com>
References: <021a01ccf722$059a2a80$10ce7f80$@gmail.com>

On Wed, Feb 29, 2012 at 3:37 PM, Igor Schtein <ischtein@gmail.com> wrote:
> Do you see any performance difference between the following approaches? The
> assumption is that most of the rows in the query will be requested often
> enough.
>
>
>
> 1.       SQL function.
>
> CREATE OR REPLACE FUNCTION X(IN a_id uuid, IN b_id uuid)
>
>  RETURNS int
>
>   STABLE
>
> AS $$
>
>   SELECT count(1)
>
>      FROM A, B
>
>      WHERE a_join_id = b_join_id
>
>        AND A.a_id  = a_id
>
>        AND B.b_id = b_id;
>
> $$ LANGUAGE SQL;
>
>
>
> SELECT X(a_id, b_id);
>
>
>
> 2.       View.
>
> CREATE OR REPLACE VIEW X AS
>
>   SELECT a_id, b_id, count(1) cnt
>
>      FROM A, B
>
>      WHERE a_join_id = b_join_id
>
> GROUP BY (a_id, b_id)
>
>
>
> SELECT cnt FROM  X WHERE X.a_id = a_id and X.B_id = b_id;

You should probably test this in your environment, but I'd expect the
view to be better.  Wrapping logic inside PL/pgsql functions
needlessly rarely turn outs to be a win.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company



view thread (3+ messages)  latest in thread

Message-ID: <CA+TgmoaOv+CKkw9fKTid7Vca+abAHSakoG8cn7hb7O2mBmaW-g@mail.gmail.com>
Permalink:  ../CA+TgmoaOv+CKkw9fKTid7Vca+abAHSakoG8cn7hb7O2mBmaW-g@mail.gmail.com/
Also on:    postgresql.org/message-id/CA+TgmoaOv+CKkw9fKTid7Vca+abAHSakoG8cn7hb7O2mBmaW-g@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-performance@postgresql.org
  Cc: robertmhaas@gmail.com, ischtein@gmail.com
  Subject: Re: Performance of SQL Function versus View
  In-Reply-To: <CA+TgmoaOv+CKkw9fKTid7Vca+abAHSakoG8cn7hb7O2mBmaW-g@mail.gmail.com>
  References: <021a01ccf722$059a2a80$10ce7f80$@gmail.com> <CA+TgmoaOv+CKkw9fKTid7Vca+abAHSakoG8cn7hb7O2mBmaW-g@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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