pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Timestamps and performances problems
8+ messages / 5 participants
[nested] [flat]

* Timestamps and performances problems
@ 2002-04-10 08:22 Jean-Christophe ARNU <arnu@paratronic.fr>
  0 siblings, 0 replies; 8+ messages in thread

From: Jean-Christophe ARNU (JX) @ 2002-04-10 08:22 UTC (permalink / raw)
  To: pgsql-admin

Hello all.
 I've a performance problem on specific requests :

 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?
 A paradigm seems that now() and (now() - '1hour'::interval) is evaluated for
 each row comparison... Am I right? Thus is there a way to make SQL
interpreter evaluate this by rewriting them before launching any comparisons?

 Or do I have to rewrite all my application queries and calculate each time
 now() and interval predicates?

Thanks by advance


-- 
Jean-Christophe ARNU
s/w developer 
Paratronic France
> Notre devoir, pour leur bien et pour le bien de Linux, est de te
> flinguer avant que tu ne les sacrifies (fût-ce avec les meilleures
> intentions du monde).
-+- TP In: Guide du linuxien pervers : "De la pédagogie par l'Exemple"



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Timestamps and performances problems
@ 2002-04-10 08:55 Jean-Christophe ARNU <jc.arnu@free.fr>
  2002-04-10 09:51 ` Re: Timestamps and performances problems Gaetano Mendola <mendola@bigfoot.com>
  2002-04-10 15:18 ` Re: Timestamps and performances problems Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 8+ messages in thread

From: Jean-Christophe ARNU (JX) @ 2002-04-10 08:55 UTC (permalink / raw)
  To: pgsql-admin

Hello all.
  I've a performance problem on specific requests :

  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?
  A paradigm seems that now() and (now() - '1hour'::interval) is evaluated for
  each row comparison... Am I right? Thus is there a way to make SQL
 interpreter evaluate this by rewriting them before launching any comparisons?

  Or do I have to rewrite all my application queries and calculate each time
  now() and interval predicates?

 Thanks by advance

-- 
Jean-Christophe ARNU
s/w developer 
Paratronic France
MR: J'ai beaucoup entendu parler de fcol, mais je n'y suis jamais alle
MR: jeter un oeil....  c'est quoi l'adresse ?
CL: Tu viens d'y poster :)
-+- in Guide de linuxien pervers : "Termes abscons..." -+-



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Timestamps and performances problems
  2002-04-10 08:55 Timestamps and performances problems Jean-Christophe ARNU <jc.arnu@free.fr>
@ 2002-04-10 09:51 ` Gaetano Mendola <mendola@bigfoot.com>
  2002-04-10 13:06   ` Re: Timestamps and performances problems Dan Langille <dan@langille.org>
  1 sibling, 1 reply; 8+ messages in thread

From: Gaetano Mendola @ 2002-04-10 09:51 UTC (permalink / raw)
  To: Jean-Christophe ARNU (JX) <jc.arnu@free.fr>; pgsql-admin

"Jean-Christophe ARNU (JX)" <jc.arnu@free.fr> wrote:
> Hello all.
>   I've a performance problem on specific requests :
>
>   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?
>   A paradigm seems that now() and (now() - '1hour'::interval) is evaluated
for
>   each row comparison... Am I right? Thus is there a way to make SQL
>  interpreter evaluate this by rewriting them before launching any
comparisons?
>
>   Or do I have to rewrite all my application queries and calculate each
time
>   now() and interval predicates?

I have the same problem, but in my case I use this query in a view so I
can't store the value now()
in a variable temp, I hope that this problem have another solution.

May be I can create a function that return a data set and I do the select
inside
with a temp variable for store now() but I don't know if is just a
quick & dirty solution.

Ciao
Gaetano.




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Timestamps and performances problems
  2002-04-10 08:55 Timestamps and performances problems Jean-Christophe ARNU <jc.arnu@free.fr>
  2002-04-10 09:51 ` Re: Timestamps and performances problems Gaetano Mendola <mendola@bigfoot.com>
@ 2002-04-10 13:06   ` Dan Langille <dan@langille.org>
  2002-04-10 14:44     ` Re: Timestamps and performances problems Jean-Christophe ARNU <jc.arnu@free.fr>
  2002-04-10 14:55     ` Re: Timestamps and performances problems Dan Langille <dan@langille.org>
  0 siblings, 2 replies; 8+ messages in thread

From: Dan Langille @ 2002-04-10 13:06 UTC (permalink / raw)
  To: Gaetano Mendola <mendola@bigfoot.com>; +Cc: pgsql-admin

On 10 Apr 2002 at 11:51, Gaetano Mendola wrote:

> "Jean-Christophe ARNU (JX)" <jc.arnu@free.fr> wrote:
> > Hello all.
> >   I've a performance problem on specific requests :
> >
> >   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)

Try where timestamp<now() and timestamp>(now() - '1 
hour'::interval)::timestemp.

-- 
Dan Langille
The FreeBSD Diary - http://freebsddiary.org/ - practical examples




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Timestamps and performances problems
  2002-04-10 08:55 Timestamps and performances problems Jean-Christophe ARNU <jc.arnu@free.fr>
  2002-04-10 09:51 ` Re: Timestamps and performances problems Gaetano Mendola <mendola@bigfoot.com>
  2002-04-10 13:06   ` Re: Timestamps and performances problems Dan Langille <dan@langille.org>
@ 2002-04-10 14:44     ` Jean-Christophe ARNU <jc.arnu@free.fr>
  2002-04-10 15:00       ` Re: Timestamps and performances problems Dan Langille <dan@langille.org>
  1 sibling, 1 reply; 8+ messages in thread

From: Jean-Christophe ARNU (JX) @ 2002-04-10 14:44 UTC (permalink / raw)
  To: dan@langille.org; +Cc: pgsql-admin

Le Wed, 10 Apr 2002 09:27:09 -0400
"Dan Langille" <dan@langille.org> me disait que :

> On 10 Apr 2002 at 9:13, JX wrote:
> 
> > Le Wed, 10 Apr 2002 09:06:55 -0400
> > "Dan Langille" <dan@langille.org> me disait que :
> > 
> > > On 10 Apr 2002 at 11:51, Gaetano Mendola wrote:
> > > 
> > > > "Jean-Christophe ARNU (JX)" <jc.arnu@free.fr> wrote:
> > > > > Hello all.
> > > > >   I've a performance problem on specific requests :
> > > > >
> > > > >   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)
> > > 
> > > Try where timestamp<now() and timestamp>(now() - '1 
> > > hour'::interval)::timestemp.
> > 
> > 	What's the difference with the syntax above? It takes he same time
> > 	than the query above. Bounded timestamps with "real" ISO timestamps
> > 	strings are always up to about 200 times faster (with extensive test
> > 	proof).
> 
> It casts the value to a timestamp.  I would prefer to discuss this on-
> list.
	Okaye, but what's the incidence on preformance issues? 
	Casting should only insure that given string is to be taken as a timestamp
isn't it? Does it make an "instanciation" of the timestamp to be that would be
applied for comparision clauses?

	Thanks

-- 
Jean-Christophe ARNU
s/w developer 
Paratronic France
«Dès que je clique sur "mount", il ne fait rien et le cdrom
 reste "unmount".
 Quel est le pb ?»
-+- Popol in Guide du linuxien pervers : "De l'avantage des interfaces..." -+-



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Timestamps and performances problems
  2002-04-10 08:55 Timestamps and performances problems Jean-Christophe ARNU <jc.arnu@free.fr>
  2002-04-10 09:51 ` Re: Timestamps and performances problems Gaetano Mendola <mendola@bigfoot.com>
  2002-04-10 13:06   ` Re: Timestamps and performances problems Dan Langille <dan@langille.org>
  2002-04-10 14:44     ` Re: Timestamps and performances problems Jean-Christophe ARNU <jc.arnu@free.fr>
@ 2002-04-10 15:00       ` Dan Langille <dan@langille.org>
  0 siblings, 0 replies; 8+ messages in thread

From: Dan Langille @ 2002-04-10 15:00 UTC (permalink / raw)
  To: pgsql-admin

On 10 Apr 2002 at 10:55, Dan Langille wrote:

> If there is an index on that field, casting to a timestamp may help the 
> optimization.  Therefore I suggested that it be tried.

And using "explain" will show the plan for that command.  I recommend 
using that too.
-- 
Dan Langille
The FreeBSD Diary - http://freebsddiary.org/ - practical examples




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Timestamps and performances problems
  2002-04-10 08:55 Timestamps and performances problems Jean-Christophe ARNU <jc.arnu@free.fr>
  2002-04-10 09:51 ` Re: Timestamps and performances problems Gaetano Mendola <mendola@bigfoot.com>
  2002-04-10 13:06   ` Re: Timestamps and performances problems Dan Langille <dan@langille.org>
@ 2002-04-10 14:55     ` Dan Langille <dan@langille.org>
  1 sibling, 0 replies; 8+ messages in thread

From: Dan Langille @ 2002-04-10 14:55 UTC (permalink / raw)
  To: Jean-Christophe ARNU (JX) <jc.arnu@free.fr>; +Cc: pgsql-admin

On 10 Apr 2002 at 10:44, JX wrote:

> Le Wed, 10 Apr 2002 09:27:09 -0400
> "Dan Langille" <dan@langille.org> me disait que :
> 
> > On 10 Apr 2002 at 9:13, JX wrote:
> > 
> > > Le Wed, 10 Apr 2002 09:06:55 -0400
> > > "Dan Langille" <dan@langille.org> me disait que :
> > > 
> > > > On 10 Apr 2002 at 11:51, Gaetano Mendola wrote:
> > > > 
> > > > > "Jean-Christophe ARNU (JX)" <jc.arnu@free.fr> wrote:
> > > > > > Hello all.
> > > > > >   I've a performance problem on specific requests :
> > > > > >
> > > > > >   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)
> > > > 
> > > > Try where timestamp<now() and timestamp>(now() - '1 
> > > > hour'::interval)::timestemp.
> > > 
> > > 	What's the difference with the syntax above? It takes he same time
> > > 	than the query above. Bounded timestamps with "real" ISO timestamps
> > > 	strings are always up to about 200 times faster (with extensive test
> > > 	proof).
> > 
> > It casts the value to a timestamp.  I would prefer to discuss this on-
> > list.
> 	Okaye, but what's the incidence on preformance issues? 
> 	Casting should only insure that given string is to be taken as a timestamp
> isn't it? Does it make an "instanciation" of the timestamp to be that would be
> applied for comparision clauses?

If there is an index on that field, casting to a timestamp may help the 
optimization.  Therefore I suggested that it be tried.
-- 
Dan Langille
The FreeBSD Diary - http://freebsddiary.org/ - practical examples




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Timestamps and performances problems
  2002-04-10 08:55 Timestamps and performances problems Jean-Christophe ARNU <jc.arnu@free.fr>
@ 2002-04-10 15:18 ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 8+ messages in thread

From: Tom Lane @ 2002-04-10 15:18 UTC (permalink / raw)
  To: Jean-Christophe ARNU (JX) <jc.arnu@free.fr>; +Cc: pgsql-admin

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



^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2002-04-10 15:18 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-04-10 08:22 Timestamps and performances problems Jean-Christophe ARNU <arnu@paratronic.fr>
2002-04-10 08:55 Timestamps and performances problems Jean-Christophe ARNU <jc.arnu@free.fr>
2002-04-10 09:51 ` Gaetano Mendola <mendola@bigfoot.com>
2002-04-10 13:06   ` Dan Langille <dan@langille.org>
2002-04-10 14:44     ` Jean-Christophe ARNU <jc.arnu@free.fr>
2002-04-10 15:00       ` Dan Langille <dan@langille.org>
2002-04-10 14:55     ` Dan Langille <dan@langille.org>
2002-04-10 15:18 ` Tom Lane <tgl@sss.pgh.pa.us>

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