Re: On using "date 'XXX' + interval 'XXX'" vs "date 'XXX'"

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Jon Lapham <lapham(at)extracta(dot)com(dot)br>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: On using "date 'XXX' + interval 'XXX'" vs "date 'XXX'"
Date: 2002-05-20 15:23:19
Message-ID: 22761.1021908199@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Jon Lapham <lapham(at)extracta(dot)com(dot)br> writes:
> In rewriting some queries I noticed a huge performance penalty when
> using a "date + interval" summation in the SELECT statement, versus a
> single simple "date". It is almost as though the "date + interval" is
> being calculated for each row...

Try coercing the sum result back to a date.

It's a little easier to see what's happening in current sources:

regression=# create table sample_tracker (initdate date primary key);

regression=# set enable_seqscan TO 0;
SET
regression=# explain select count(*) from sample_tracker where
regression-# initdate>=date '2002-02-01';
QUERY PLAN
---------------------------------------------------------------------------------------------------
Aggregate (cost=46.33..46.33 rows=1 width=0)
-> Index Scan using sample_tracker_pkey on sample_tracker (cost=0.00..45.50 rows=333 width=0)
Index Cond: (initdate >= '2002-02-01'::date)
(3 rows)

regression=# explain select count(*) from sample_tracker where
regression-# initdate>=date '2002-01-01' + interval '1 month';
QUERY PLAN
-----------------------------------------------------------------------------------------------
Aggregate (cost=100000025.83..100000025.83 rows=1 width=0)
-> Seq Scan on sample_tracker (cost=100000000.00..100000025.00 rows=333 width=0)
Filter: ("timestamp"(initdate) >= '2002-02-01 00:00:00'::timestamp without time zone)
(3 rows)

regression=#

Writing date(date '2002-01-01' + interval '1 month') gets me back to
the first plan.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Denis Perchine 2002-05-20 15:28:22 Re: PostgreSQL 7.1 forces sequence scan when there is no reason
Previous Message Tom Lane 2002-05-20 15:12:58 Re: Further thoughts on Referential Integrity