From: Stephan Szabo <sszabo@megazone23.bigpanda.com>
To: Jean-Christophe Boggio <cat@thefreecat.org>
Cc: pgsql-sql@postgresql.org
Subject: Re: Optimization with dates
Date: Tue, 13 Nov 2001 16:17:06 -0800 (PST)
Message-ID: <20011113161522.F89792-100000@megazone23.bigpanda.com> (raw)
In-Reply-To: <72319017913.20011114005656@thefreecat.org>
On Wed, 14 Nov 2001, Jean-Christophe Boggio wrote:
> Hello,
>
> I really have a problem dealing with dates :
>
> I have a big table (~10M rows) like :
>
> create table gains (
> dategain timestamp,
> otherfields
> );
>
> There's an index on dategain and I want to use it to get the last
> 30 days records :
>
> explain select count(*) from gains where dategain>=now()-30;
>
> Aggregate (cost=256546.78..256546.78 rows=1 width=0)
> -> Seq Scan on gains (cost=0.00..250627.68 rows=2367640 width=0)
>
> whereas :
>
> explain select count(*) from gains where dategain>='now';
>
> Aggregate (cost=27338.47..27338.47 rows=1 width=0)
> -> Index Scan using ix_gains_dategain on gains (cost=0.00..27320.71 rows=7103 width=0)
>
> I have tried :
> where dategain>=(select now()-30);
> where dategain+30>='now'
> where date(dategain)>=date('now')-30
> and many other, syntactically absurd :-)
>
> Anyone can help me use this index ?
Is 2367640 a reasonable estimate for the number of
rows that match the condition? Have you run vacuum
analyze?
If the estimate is right, you'll probably find that
the sequence scan is actually faster than an index
scan since about 1/4 of the table is being selected.
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: sszabo@megazone23.bigpanda.com, cat@thefreecat.org
Subject: Re: Optimization with dates
In-Reply-To: <20011113161522.F89792-100000@megazone23.bigpanda.com>
* 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