From: Tom Lane <tgl@sss.pgh.pa.us>
To: Jean-Christophe ARNU (JX) <jc.arnu@free.fr>
Cc: pgsql-admin@postgresql.org
Subject: Re: Timestamps and performances problems
Date: Wed, 10 Apr 2002 11:18:02 -0400
Message-ID: <5494.1018451882@sss.pgh.pa.us> (raw)
In-Reply-To: <20020410045517.57c89e27.jc.arnu@free.fr>
References: <20020410045517.57c89e27.jc.arnu@free.fr>
Jean-Christophe ARNU (JX) <jc.arnu@free.fr> writes:
> When I use timestamps + interval in where clauses, query performance is
> slowed down by a factor of 20 or 30!!!! For exemple :
> select timestamp,value
> from measure
> where timestamp<now() and timestamp>(now() - '1 hour'::interval)
> is 20 to 30 times longer than
> select timestamp,value
> from measure
> where timestamp<'2002-04-10 10:00' and timestamp>='2002-04-10 9:00';
> So where is the bottleneck?
Did you compare EXPLAIN output? I suspect that the second query is
using an index on the timestamp column and the first isn't.
The reason it isn't is that now() isn't a constant, and the system is
not smart enough to realize that it's safe to optimize the query into
an indexscan anyway.
For 7.3 we've fixed this by introducing a new concept of "constant within
a query", which now() does satisfy. In the meantime you could hack
around it by writing a user-defined function that calls now() and is
marked isCachable --- which is a lie, but you can get away with it in
interactive queries. (But don't try calling such a function in views,
or queries in plpgsql, 'cause you'll get burnt.)
regards, tom lane
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-admin@postgresql.org
Cc: tgl@sss.pgh.pa.us, jc.arnu@free.fr
Subject: Re: Timestamps and performances problems
In-Reply-To: <5494.1018451882@sss.pgh.pa.us>
References: <20020410045517.57c89e27.jc.arnu@free.fr> <5494.1018451882@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