From: hubert depesz lubaczewski <depesz@depesz.com>
To: Paul van der Linden <paul.doskabouter@gmail.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: Slow index creation
Date: Wed, 17 Feb 2021 19:48:08 +0100
Message-ID: <20210217184808.GA14032@depesz.com> (raw)
In-Reply-To: <CAEC-EqBdBNeBd+ssUZbuKPyd4gTE2sjMFEmioSCa=2bsHtJvMA@mail.gmail.com>
References: <CAEC-EqBdBNeBd+ssUZbuKPyd4gTE2sjMFEmioSCa=2bsHtJvMA@mail.gmail.com>
On Tue, Feb 16, 2021 at 07:30:23PM +0100, Paul van der Linden wrote:
> Hi,
> I have 2 functions:
> CREATE FUNCTION foo(a text, b text, c text) RETURNS text AS
> $func$
> DECLARE
> retVal text;
> BEGIN
> SELECT
> CASE
> WHEN a='v1' AND b='b1' THEN 'r1'
> WHEN a='v1' THEN 'r2'
> ... snip long list containing various tests on a,b and c
> WHEN a='v50' THEN 'r50'
> END INTO retval;
> RETURN retVal;
> END
> $func$ LANGUAGE PLPGSQL IMMUTABLE;
If this function was converted to SQL function it could be faster, as it
could be inlined.
> CREATE FUNCTION bar(r text, geom geometry) RETURNS int AS
> $func$
> DECLARE
> retVal int;
> BEGIN
> SELECT
> CASE
> WHEN r='r1' AND st_area(geom)>100 THEN 1
> WHEN r='r1' THEN 2
> ... snip long list containing various tests on r and st_area(geom)
> WHEN r='r50' THEN 25
> END INTO retval;
> RETURN retVal;
> END
> $func$ LANGUAGE PLPGSQL IMMUTABLE;
First thing that I notice is that it seems (from the code and comment)
that you run st_area(geom) multiple times.
If that's really the case - why don't you cache it in some variable?
declare
v_area float := st_area( geom );
begin
...
and then use v_area instead of st_area(geom)
depesz
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-general@postgresql.org
Cc: depesz@depesz.com, paul.doskabouter@gmail.com
Subject: Re: Slow index creation
In-Reply-To: <20210217184808.GA14032@depesz.com>
References: <CAEC-EqBdBNeBd+ssUZbuKPyd4gTE2sjMFEmioSCa=2bsHtJvMA@mail.gmail.com> <20210217184808.GA14032@depesz.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