Re: Which is faster SQL or PL/PGSQL

From: Richard Huxton <dev(at)archonet(dot)com>
To: Michael Pohl <pgsql(at)newtopia(dot)com>, pgsql-sql(at)postgresql(dot)org
Cc: "George A(dot)J" <jinujosein(at)yahoo(dot)com>
Subject: Re: Which is faster SQL or PL/PGSQL
Date: 2003-10-20 17:09:51
Message-ID: 200310201809.51830.dev@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Monday 20 October 2003 16:36, Michael Pohl wrote:
> On Sun, 19 Oct 2003, Christopher Browne wrote:
> > The world rejoiced as jinujosein(at)yahoo(dot)com ("George A.J") wrote:
> > > i am converting an MSSQL database to Postgres. there is a lot of
> > > procedures to convert.
> > >
> > > which language is best for functions, SQL or plpgsql.
> > >
> > > which is faster . i am using postgres 7.3.2
> >
> > Hmm? This doesn't seem to make much more sense than the question of
> > what colour a database should be ("Mauve has more RAM...").
>
> Transact-SQL stored procedures pseudo-compile their execution plans the
> first time they are run, which can result in faster subsequent executions.
> I'm guessing the poster was wondering if plpgsql functions offered similar
> performance benefits vs. equivalent SQL.

To which the answer would have to be "yes and no".

A plpgsql function is compiled on its first call, so any queries will have
their plans built then, and you will gain on subsequent calls.

However, since the plan will have to be built without knowledge of the actual
values involved, you might not get as good a plan as you could with the
actual values. For example:
SELECT * FROM uk_towns WHERE town_name LIKE 'T%';
SELECT * FROM uk_towns WHERE town_name LIKE 'X%';
There's a good chance a seq-scan of the table is the best plan for the first
query, but if you have an index on town_name you probably want to use it in
the second case.

So - gain by not re-planning on every call, but maybe lose because your plan
is not so precise.

Of course, any queries you build dynamically and run via EXECUTE will have to
be planned each time.

--
Richard Huxton
Archonet Ltd

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Huxton 2003-10-20 17:12:49 Re: query or design question
Previous Message Tom Lane 2003-10-20 16:49:06 Re: Unable to user pg_restore