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 D00123A432A for ; Thu, 20 Jan 2005 09:41:44 +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 63353-01 for ; Thu, 20 Jan 2005 09:41:30 +0000 (GMT) Received: from ar-sd.net (unknown [82.77.155.72]) by svr1.postgresql.org (Postfix) with ESMTP id C198D3A40AB for ; Thu, 20 Jan 2005 09:41:31 +0000 (GMT) Received: from localhost (localhost [127.0.0.1]) by ar-sd.net (Postfix) with ESMTP id AE5C31E9BC; Thu, 20 Jan 2005 11:37:07 +0200 (EET) Received: from ar-sd.net ([127.0.0.1]) by localhost (linz [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 02819-09; Thu, 20 Jan 2005 11:37:06 +0200 (EET) Received: from forge (unknown [192.168.0.11]) by ar-sd.net (Postfix) with SMTP id 4B8641E9B9; Thu, 20 Jan 2005 11:37:06 +0200 (EET) Message-ID: <010a01c4fed4$3fba1ca0$0b00a8c0@forge> From: "Andrei Bintintan" To: "Michael Fuhr" Cc: References: <006b01c4fecd$6d37c9e0$0b00a8c0@forge> <20050120093331.GA37672@winnie.fuhr.org> Subject: Re: SQL timestamp to date cast Date: Thu, 20 Jan 2005 11:41:41 +0200 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-Virus-Scanned: by amavisd-new at ar-sd.net X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.016 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200501/160 X-Sequence-Number: 19948 > Another possibility would be to create a functional index on datetime: > > CREATE INDEX user_action_date_idx ON user_action (date(datetime)); GREAT!!! I thought it is possible but I didn't knew how to make such indexes. :)) Thank you !!! Andy. ----- Original Message ----- From: "Michael Fuhr" To: "Andrei Bintintan" Cc: Sent: Thursday, January 20, 2005 11:33 AM Subject: Re: [SQL] SQL timestamp to date cast > 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/ >