Indexing timestamps

From: Andre Schubert <andre(dot)schubert(at)km3(dot)de>
To: pgsql-sql(at)postgresql(dot)org
Subject: Indexing timestamps
Date: 2002-06-06 08:45:21
Message-ID: 20020606104521.3b3f1b06.andre.schubert@km3.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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 :)

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message D'Arcy J.M. Cain 2002-06-06 10:34:35 Re: PostgreSQL on AIX
Previous Message Ludwig Lim 2002-06-06 03:33:41 Re: simple select statement inquiry