X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id C25333A4353 for ; Thu, 20 Jan 2005 09:34:09 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 61015-09 for ; Thu, 20 Jan 2005 09:34:02 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id 979E93A44B7 for ; Thu, 20 Jan 2005 09:34:02 +0000 (GMT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.1/8.13.1) with ESMTP id j0K9XWZY006176 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 20 Jan 2005 02:33:34 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.1/8.13.1) with ESMTP id j0K9XVIQ037715; Thu, 20 Jan 2005 02:33:32 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.1/8.13.1/Submit) id j0K9XVkf037714; Thu, 20 Jan 2005 02:33:31 -0700 (MST) (envelope-from mfuhr) Date: Thu, 20 Jan 2005 02:33:31 -0700 From: Michael Fuhr To: Andrei Bintintan Cc: pgsql-sql@postgresql.org Subject: Re: SQL timestamp to date cast Message-ID: <20050120093331.GA37672@winnie.fuhr.org> References: <006b01c4fecd$6d37c9e0$0b00a8c0@forge> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <006b01c4fecd$6d37c9e0$0b00a8c0@forge> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.003 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200501/158 X-Sequence-Number: 19946 On Thu, Jan 20, 2005 at 10:52:50AM +0200, Andrei Bintintan wrote: > SELECT DISTINCT(nummer) > FROM user_action > WHERE datetime::date='11/11/2004' AND id_action IN (5,6,9) > > Now, datetime has the type timestamp. How can I make an index or write > different this query so that it runs faster? You could create an index on datetime and rewrite your queries: CREATE INDEX user_action_datetime_idx ON user_action (datetime); SELECT DISTINCT(nummer) FROM user_action WHERE datetime >= '11/11/2004' AND datetime < '11/12/2004' AND id_action IN (5,6,9); Another possibility would be to create a functional index on datetime: CREATE INDEX user_action_date_idx ON user_action (date(datetime)); SELECT DISTINCT(nummer) FROM user_action WHERE date(datetime) = '11/11/2004' AND id_action IN (5,6,9); -- Michael Fuhr http://www.fuhr.org/~mfuhr/