Skip site navigation (1) Skip section navigation (2)

Performance of SQL Function versus View

From: "Igor Schtein" <ischtein(at)gmail(dot)com>
To: <pgsql-performance(at)postgresql(dot)org>
Subject: Performance of SQL Function versus View
Date: 2012-02-29 20:37:56
Message-ID: 021a01ccf722$059a2a80$10ce7f80$@gmail.com (view raw or flat)
Thread:
Lists: pgsql-performance
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;

 

Thank you, 

Igor

Responses

pgsql-performance by date

Next:From: Marc MaminDate: 2012-02-29 20:40:22
Subject: Re: text search: tablescan cost for a tsvector
Previous:From: Robert HaasDate: 2012-02-29 18:45:25
Subject: Re: Vacuuming problems on TOAST table

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group