Re: how to avoid repeating expensive computation in select

From: Orhan Kavrakoglu <orhan(at)tart(dot)com(dot)tr>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: how to avoid repeating expensive computation in select
Date: 2011-03-01 08:51:01
Message-ID: 4D6CB375.6080407@tart.com.tr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 2011-02-03 18:07, Bob Price wrote:
> I would like to know if there is a way in PostgreSQL to avoid repeating an expensive computation in a SELECT where the result is needed both as a returned value and as an expression in the WHERE clause.

I think I've seen it said here that PG avoids redundant multiple
calculations of an expression.

Even so, have you thought about using subqueries?

> SELECT id, expensivefunc(value) AS score FROM mytable
> WHERE id LIKE '%z%' AND expensivefunc(value)> 0.5;

SELECT id, expensivefunc(value) FROM (
(SELECT id, value FROM mytable WHERE id LIKE '%z%')
) WHERE expensivefunc(value) > 0.5;

or even

SELECT id, score FROM (
SELECT id, expensivefunc(value) AS score FROM (
(SELECT id, value FROM mytable WHERE id LIKE '%z%')
)
) WHERE score > 0.5

--
Orhan Kavrakoğlu
orhan(at)tart(dot)com(dot)tr

Tart New Media
w : http://www.tart.com.tr
t : +90 212 263 0 666 / ext: 142
f : TBA
a : TBA

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Szymon Guz 2011-03-01 09:35:40 Re: pg_catalog.pg_stat_activity and current_query
Previous Message Alex 2011-03-01 08:36:48 pg_catalog.pg_stat_activity and current_query