Re: Optimization with dates

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Jean-Christophe Boggio <cat(at)thefreecat(dot)org>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Optimization with dates
Date: 2001-11-14 02:43:52
Message-ID: 4596.1005705832@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Jean-Christophe Boggio <cat(at)thefreecat(dot)org> writes:
> I have tried :
> where dategain>=(select now()-30);
> where dategain+30>='now'
> where date(dategain)>=date('now')-30
> and many other, syntactically absurd :-)

Try
dategain >= ago(30)

where "ago" is a function that computes "date(now()) - n" and is
marked "iscachable". This is a cheat but it keeps the planner from
being distracted by the noncachable nature of "now()". You can find
past discussions of this if you search the archives for "iscachable",
eg
http://fts.postgresql.org/db/mw/msg.html?mid=1037521
http://fts.postgresql.org/db/mw/msg.html?mid=60584
http://fts.postgresql.org/db/mw/msg.html?mid=97823

As several other people pointed out, there's also a question of whether
the system *should* use the index --- you haven't told us how many
rows you expect the query to visit. But until you express the WHERE
condition in the form "column >= something-that-can-be-reduced-to-
a-constant", you're unlikely to get the system to even try.

regards, tom lane

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tille, Andreas 2001-11-14 08:12:34 Re: Design Tool for postgresql
Previous Message Llew Goodstadt 2001-11-14 02:43:50 Re: how do I update or insert efficently in postgres