From: Andre Schubert <andre.schubert@km3.de>
To: pgsql-sql@postgresql.org
Subject: Indexing timestamps
Date: Thu, 6 Jun 2002 10:45:21 +0200
Message-ID: <20020606104521.3b3f1b06.andre.schubert@km3.de> (raw)
Hi all,
i have some questions on indexing a timestamp column of a table.
i have the following structure:
create table test_table( time_stamp datetime, id int8);
create index test_index on test_table using btree(time_stamp);
select count(*) from tbl_traffic;
count
--------
116894
I have questions on the following explains:
explain select * from test_table where time_stamp = datetime('2002-01-01');
NOTICE: QUERY PLAN:
Index Scan using test_index on test_table (cost=0.00..1651.78 rows=584 width=16)
explain select * from test_table where time_stamp = datetime(now());
NOTICE: QUERY PLAN:
Seq Scan on test_table (cost=0.00..2733.64 rows=584 width=16)
Why is the index test_index not used with the now() function?
If it is possible, how should i create i index that is used with now()?
explain select * from test_table where date_trunc('month',time_stamp) = date_trunc('month',datetime('2002-01-01'));
NOTICE: QUERY PLAN:
Seq Scan on test_table (cost=0.00..2441.41 rows=584 width=16)
explain select * from test_table where date_trunc('month',time_stamp) = date_trunc('month',datetime(now()));
NOTICE: QUERY PLAN:
Seq Scan on test_table (cost=0.00..3318.12 rows=584 width=16)
I need to select all data from this table monthly by select data of the last month.
Can anyone explain me why the second Seq scan takes longer than the first one and
is there i whay to define a index that is used if i do such a select, or better
is there a better select statement that uses some indexes??
Thanks in advance, hope someone could answer my questions :)
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: andre.schubert@km3.de
Subject: Re: Indexing timestamps
In-Reply-To: <20020606104521.3b3f1b06.andre.schubert@km3.de>
* 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