pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tony Capobianco <tcapobianco@prospectiv.com>
To: Pavel Stehule <pavel.stehule@gmail.com>
Cc: Andy Colson <andy@squeakycode.net>
Cc: pgsql-performance@postgresql.org
Subject: Re: Cursor fetch performance issue
Date: Tue, 24 Jan 2012 16:34:06 -0500
Message-ID: <1327440846.1968.20.camel@tony1.localdomain> (raw)
In-Reply-To: <CAFj8pRDyZRb1Q3574dZ2VfQnBRRPvnrwGuhLvh6qRK-ByHsk+A@mail.gmail.com>
References: <1327437700.1968.10.camel@tony1.localdomain>
	<CAFj8pRD3g3WESOxES27t_pBh_30Ko7JO7nKCf77oRsjU==tFwQ@mail.gmail.com>
	<1327438657.1968.15.camel@tony1.localdomain>
	<4F1F1E7A.4080403@squeakycode.net>
	<CAFj8pRDyZRb1Q3574dZ2VfQnBRRPvnrwGuhLvh6qRK-ByHsk+A@mail.gmail.com>

Here's the explain:

pg=# explain select getMemberAdminPrevious_sp(247815829, 1,'test.email@hotmail.com', 'Email', 'Test');
                QUERY PLAN                
------------------------------------------
 Result  (cost=0.00..0.26 rows=1 width=0)
(1 row)

Time: 1.167 ms

There was discussion of 'LIKE' v. '=' and wildcard characters are not
being entered into the $1 parameter.

This is not generating a sql string.  I feel it's something to do with
the fetch of the refcursor.  The cursor is a larger part of a function:

CREATE OR REPLACE FUNCTION PUBLIC.GETMEMBERADMINPREVIOUS_SP2 ( 
  p_memberid       IN numeric,
  p_websiteid      IN numeric,
  p_emailaddress   IN varchar,
  p_firstname      IN varchar,
  p_lastname       IN varchar)
RETURNS refcursor AS $$
DECLARE
  ref            refcursor;
  l_sysdateid    numeric;
BEGIN
  l_sysdateid := sysdateid();
  if (p_memberid != 0) then
    if (p_emailaddress IS NOT NULL) then
      OPEN ref FOR
        SELECT m.memberid, m.websiteid, m.emailaddress,
               m.firstname, m.lastname, m.regcomplete, m.emailok
        FROM   members m
        WHERE  m.emailaddress LIKE p_emailaddress
        AND    m.changedate_id < l_sysdateid ORDER BY m.emailaddress, 
m.websiteid;
    end if;
  end if;
  Return ref;
EXCEPTION
WHEN NO_DATA_FOUND THEN
   Return null;
END;
$$ LANGUAGE 'plpgsql';


On Tue, 2012-01-24 at 22:17 +0100, Pavel Stehule wrote:
> Hello
> 
> >
> > So, is getMemberAdminPrevious_sp2() preparing a statement with wildcards?
> >
> > SELECT m.memberid, m.websiteid, m.emailaddress,
> >       m.firstname, m.lastname, m.regcomplete, m.emailok
> >       FROM   members m
> >       WHERE  m.emailaddress LIKE $1
> >       AND    m.changedate_id < $2
> >      ORDER BY m.emailaddress, m.websiteid;
> >
> > Or is it creating the string and executing it:
> >
> > sql = 'SELECT m.memberid, m.websiteid, m.emailaddress, '
> >    ||  ' m.firstname, m.lastname, m.regcomplete, m.emailok '
> >    ||  ' FROM   members m
> >    ||  ' WHERE  m.emailaddress LIKE ' || arg1
> >    ||  ' AND    m.changedate_id < ' || arg2
> >    ||  ' ORDER BY m.emailaddress, m.websiteid ';
> > execute(sql);
> >
> > Maybe its the planner doesnt plan so well with $1 arguments vs actual
> > arguments thing.
> >
> 
> sure, it could be blind optimization problem in plpgsql. Maybe you
> have to use a dynamic SQL - OPEN FOR EXECUTE stmt probably
> 
> http://www.postgresql.org/docs/9.1/interactive/plpgsql-cursors.html
> 
> Regards
> 
> Pavel Stehule
> 
> > -Andy
> >
> >
> 





view thread (9+ messages)  latest in thread

Message-ID: <1327440846.1968.20.camel@tony1.localdomain>
Permalink:  ../1327440846.1968.20.camel@tony1.localdomain/
Also on:    postgresql.org/message-id/1327440846.1968.20.camel@tony1.localdomain

 · 

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: tcapobianco@prospectiv.com, pavel.stehule@gmail.com, andy@squeakycode.net
  Subject: Re: Cursor fetch performance issue
  In-Reply-To: <1327440846.1968.20.camel@tony1.localdomain>
  References: <1327437700.1968.10.camel@tony1.localdomain> <CAFj8pRD3g3WESOxES27t_pBh_30Ko7JO7nKCf77oRsjU==tFwQ@mail.gmail.com> <1327438657.1968.15.camel@tony1.localdomain> <4F1F1E7A.4080403@squeakycode.net> <CAFj8pRDyZRb1Q3574dZ2VfQnBRRPvnrwGuhLvh6qRK-ByHsk+A@mail.gmail.com> <1327440846.1968.20.camel@tony1.localdomain>

* 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