Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id AE2BF475926 for ; Thu, 6 Jun 2002 04:39:57 -0400 (EDT) Received: from server.beierfeld.kabeljournal.de (pD9508741.dip.t-dialin.net [217.80.135.65]) by postgresql.org (Postfix) with SMTP id A85D3475905 for ; Thu, 6 Jun 2002 04:39:55 -0400 (EDT) Received: (qmail 3332 invoked from network); 6 Jun 2002 08:39:56 -0000 Received: from station158.beierfeld.kabeljournal.de (HELO station158) (192.168.128.158) by server.beierfeld.kabeljournal.de with SMTP; 6 Jun 2002 08:39:56 -0000 Date: Thu, 6 Jun 2002 10:45:21 +0200 From: Andre Schubert To: pgsql-sql@postgresql.org Subject: Indexing timestamps Message-Id: <20020606104521.3b3f1b06.andre.schubert@km3.de> Organization: km3 teledienst GmbH X-Mailer: Sylpheed version 0.7.5 (GTK+ 1.2.6; i586-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Archive-Number: 200206/47 X-Sequence-Number: 8012 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 :)