Re: Query organization question

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Query organization question
Date: 2009-04-28 08:42:24
Message-ID: 20090428084224.GC12225@frubble.xen.chris-lamb.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Apr 27, 2009 at 02:15:05PM -0700, Gauthier, Dave wrote:
> The stored procedure calls another recursive stored procedure that
> can take a long time to run, usually about 3-4 seconds. Not bad for
> a handful of records, but it is now operating on a table with over
> 40,000 records.

The most general solution I can think of would be to set the "cost" of
a function when creating it, have a look at the docs for create[1] or
alter[2] function.

The answer to your other question is that the inner select statement is
normally called a "sub-select". The way to force the sub-select to be
evaluated first is to put an OFFSET 0 at the end of it, i.e.:

SELECT a, b
FROM (
SELECT a.i AS a, b.i AS b
FROM foo a, foo b
OFFSET 0) x
WHERE a < b;

will force PG to create the complete cross product of "foo" with itself
before trying to apply the outer WHERE clause. I'd try changing the
cost of the function first as it should cause PG to do the "right thing"
when you use the function in other queries.

--
Sam http://samason.me.uk/

[1] http://www.postgresql.org/docs/current/static/sql-createfunction.html
[2] http://www.postgresql.org/docs/current/static/sql-alterfunction.html

In response to

Browse pgsql-general by date

  From Date Subject
Next Message itishree sukla 2009-04-28 09:14:30 Issue with POSTGIS
Previous Message David 2009-04-28 07:36:14 Re: Sequence Incrementing by 2 insted of 1