pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Fuhr <mike@fuhr.org>
To: Andrei Bintintan <klodoma@ar-sd.net>
Cc: pgsql-sql@postgresql.org
Subject: Re: SQL timestamp to date cast
Date: Thu, 20 Jan 2005 02:33:31 -0700
Message-ID: <20050120093331.GA37672@winnie.fuhr.org> (raw)
In-Reply-To: <006b01c4fecd$6d37c9e0$0b00a8c0@forge>
References: <006b01c4fecd$6d37c9e0$0b00a8c0@forge>

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/



view thread (4+ messages)  latest in thread

Message-ID: <20050120093331.GA37672@winnie.fuhr.org>
Permalink:  ../20050120093331.GA37672@winnie.fuhr.org/
Also on:    postgresql.org/message-id/20050120093331.GA37672@winnie.fuhr.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mike@fuhr.org, klodoma@ar-sd.net
  Subject: Re: SQL timestamp to date cast
  In-Reply-To: <20050120093331.GA37672@winnie.fuhr.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox