X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 9CFE9474E4F for ; Sat, 22 Mar 2003 03:02:30 -0500 (EST) Received: from trade-india.com (ns5.trade-india.com [66.234.10.13]) by postgresql.org (Postfix) with SMTP id 9F48E474E44 for ; Sat, 22 Mar 2003 03:02:29 -0500 (EST) Received: (qmail 15091 invoked by uid 505); 22 Mar 2003 08:02:14 -0000 Received: from mallah@trade-india.com by ns5.trade-india.com by uid 502 with qmail-scanner-1.15 (uvscan: v4.1.60/v4171. Clear:. Processed in 2.900109 secs); 22 Mar 2003 08:02:14 -0000 Received: from mail-relay.trade-india.com (HELO system67.trade-india-local.com) (203.196.129.235) by ns5.trade-india.com with SMTP; 22 Mar 2003 08:02:11 -0000 From: Rajesh Kumar Mallah Organization: Infocom Network Limited To: pgsql-sql@postgresql.org Subject: SQL help for efficient time handling.. Date: Sat, 22 Mar 2003 13:32:33 +0530 User-Agent: KMail/1.5 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200303221332.33121.mallah@trade-india.com> X-Spam-Status: No, hits=0.0 required=5.0 tests=USER_AGENT version=2.50-cvs X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50-cvs (1.172-2003-02-14-exp) X-Archive-Number: 200303/518 X-Sequence-Number: 12557 Hi everyone, I have a table time_dimension loaded with 1440 records i record for each min in 24 hrs CREATE TABLE general.time_dimension ( time_id integer not null , sql_time time(0) without time zone not null, PRIMARY KEY (time_id), UNIQUE (sql_time) ); sample data is like, tradein_clients=# SELECT * from time_dimension limit 10; +---------+----------+ | time_id | sql_time | +---------+----------+ | 1 | 00:00:00 | | 2 | 00:01:00 | | 3 | 00:02:00 | | 4 | 00:03:00 | | 5 | 00:04:00 | | 6 | 00:05:00 | | 7 | 00:06:00 | | 8 | 00:07:00 | | 9 | 00:08:00 | | 10 | 00:09:00 | +---------+----------+ (10 rows) To get current_time_id i use a query like SELECT time_id from time_dimension where sql_time=date_trunc('minute' , cast(now() as time without time zone) ); it works but uses seq_scan Seq Scan on time_dimension (cost=0.00..35.00 rows=5 width=4) (actual time=4.75..8.16 rows=1 loops=1) Filter: ((sql_time)::interval = date_trunc('minute'::text, ((now())::time without time zone)::interval)) Total runtime: 8.20 msec whereas a query like explain analyze SELECT time_id from time_dimension where sql_time='13:13:13'; +------------------------------------------------------------------------------------------------------------------------------------------+ | QUERY PLAN | +------------------------------------------------------------------------------------------------------------------------------------------+ | Index Scan using time_dimension_sql_time_key on time_dimension (cost=0.00..4.82 rows=1 width=4) (actual time=0.03..0.03 rows=0 loops=1) | | Index Cond: (sql_time = '13:13:13'::time without time zone) | | Total runtime: 0.06 msec -------------------------------------------------------------------------------------------------------------------------------------------- take much less. can anyone tell be the best way to get time_id from time_dimension for current_time. any help is appreciated. Regds Mallah. -- Rajesh Kumar Mallah, Project Manager (Development) Infocom Network Limited, New Delhi phone: +91(11)6152172 (221) (L) ,9811255597 (M) Visit http://www.trade-india.com , India's Leading B2B eMarketplace.