Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by postgresql.org (Postfix) with ESMTP id A6E44475982 for ; Wed, 10 Apr 2002 11:18:10 -0400 (EDT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.11.4/8.11.4) with ESMTP id g3AFI2F05497; Wed, 10 Apr 2002 11:18:02 -0400 (EDT) To: Jean-Christophe ARNU (JX) Cc: pgsql-admin@postgresql.org Subject: Re: Timestamps and performances problems In-reply-to: <20020410045517.57c89e27.jc.arnu@free.fr> References: <20020410045517.57c89e27.jc.arnu@free.fr> Comments: In-reply-to Jean-Christophe ARNU (JX) message dated "Wed, 10 Apr 2002 04:55:17 -0400" Date: Wed, 10 Apr 2002 11:18:02 -0400 Message-ID: <5494.1018451882@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200204/86 X-Sequence-Number: 3974 Jean-Christophe ARNU (JX) 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() - '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