pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Itai Zukerman <zukerman@math-hat.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Indices and time spans
Date: Mon, 31 Jul 2000 10:49:20 -0400
Message-ID: <9449.965054960@sss.pgh.pa.us> (raw)
In-Reply-To: <87g0oqbej1.fsf@matt.w80.math-hat.com>
References: <87g0oqbej1.fsf@matt.w80.math-hat.com>

Itai Zukerman <zukerman@math-hat.com> writes:
> I have this:
>       SELECT ...
>       FROM trade, entry
>       WHERE AGE( CURRENT_TIMESTAMP, posted ) <= '5 minutes'::TIMESPAN
>         AND trade.entryId = entry.entryId

> That is, fetch all trades executed in the last 5 minutes.

> This query seems to run pretty slowly when trade is filled.  Putting
> an index on trade ( posted ) doesn't seem to help any

No, it wouldn't, since the indexscan machinery can only deal with
WHERE clauses that look like "indexed_column relational_op constant".
You need to recast the clause as something like

      WHERE posted >= (CURRENT_TIMESTAMP - '5 minutes'::TIMESPAN)

Then you still have the problem of persuading Postgres that it should
treat the right side of this as a constant and not something to
re-evaluate at each row.  In 7.0 and later it's possible to do that
with creative use of a user-defined function marked "iscachable"
(for details see the archives for the last time this question came up,
a few months back).  But the lowest-tech solution may be to calculate
the cutoff time on the application side, so you can just send it as a
constant to begin with.

			regards, tom lane



view thread (3+ messages)  latest in thread

Message-ID: <9449.965054960@sss.pgh.pa.us>
Permalink:  ../9449.965054960@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/9449.965054960@sss.pgh.pa.us

 · 

reply

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-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, zukerman@math-hat.com
  Subject: Re: Indices and time spans
  In-Reply-To: <9449.965054960@sss.pgh.pa.us>
  References: <87g0oqbej1.fsf@matt.w80.math-hat.com> <9449.965054960@sss.pgh.pa.us>

* 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