function calls in WHERE clause

From: "Lee Harr" <missive(at)hotmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: function calls in WHERE clause
Date: 2004-09-08 21:13:05
Message-ID: BAY2-F19gcTEGi9R6K7000091a0@hotmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have a view like this:

CREATE or replace VIEW program_member_this_year AS
SELECT DISTINCT person.person_id
FROM unit.person, unit.visit
WHERE
unit.person.person_id = unit.visit.person_id
AND
visit.program_id = this_program()
AND
visit.time_stamp > shared.last_september();

and I would like to speed it up ...

I notice that if I write it like this:

CREATE or replace VIEW program_member_this_year AS
SELECT DISTINCT person.person_id
FROM unit.person, unit.visit
WHERE
unit.person.person_id = unit.visit.person_id
AND
visit.program_id = 3
AND
visit.time_stamp > '2004-9-1';

It takes about 1/4 the time as with the function calls.

Both of those function calls will always return the exact
same thing for any given query.

My approach now is to make a Pl/PgSQL function which
will build up the correct query with the function calls
replaced by their constant results .... but is there a
better way?

I should note that this_program() is defined as VOLATILE.

It was a while ago, but I believe this is because I have
a this_program() defined in each schema and they
return a different value depending on which schema
you are in when you call the function...

Thanks for any insight.

_________________________________________________________________
MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*.
http://join.msn.com/?page=features/virus

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2004-09-08 21:42:12 Re: function calls in WHERE clause
Previous Message Karl O. Pinc 2004-09-08 21:10:45 Re: Returning multiple values (but one row) in plpgsql