pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
SQL timestamp to date cast
4+ messages / 2 participants
[nested] [flat]

* SQL timestamp to date cast
@ 2005-01-20 08:52 Andrei Bintintan <klodoma@ar-sd.net>
  2005-01-20 09:33 ` Re: SQL timestamp to date cast Michael Fuhr <mike@fuhr.org>
  0 siblings, 1 reply; 4+ messages in thread

From: Andrei Bintintan @ 2005-01-20 08:52 UTC (permalink / raw)
  To: pgsql-sql

Hi, 

I have the following query:

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? It really takes some time sometimes. Usually about 3-4secs. user_action has about 300k rows and increasing ~ 5-10k a day. 


Explain analyze SELECT DISTINCT(nummer) FROM user_action WHERE datetime::date='11/11/2004' AND id_action IN (5,6,9)

Unique  (cost=18141.71..18143.72 rows=45 width=4) (actual time=418.122..418.340 rows=85 loops=1)
  ->  Sort  (cost=18141.71..18142.72 rows=402 width=4) (actual time=418.119..418.194 rows=192 loops=1)
        Sort Key: nummer
        ->  Seq Scan on user_action  (cost=0.00..18124.33 rows=402 width=4) (actual time=366.240..417.890 rows=192 loops=1)
              Filter: (((datetime)::date = '2004-11-11'::date) AND ((id_action = 5) OR (id_action = 6) OR (id_action = 9)))
Total runtime: 418.419 ms


Best regards.
Andy.

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: SQL timestamp to date cast
  2005-01-20 08:52 SQL timestamp to date cast Andrei Bintintan <klodoma@ar-sd.net>
@ 2005-01-20 09:33 ` Michael Fuhr <mike@fuhr.org>
  2005-01-20 09:41   ` Re: SQL timestamp to date cast Andrei Bintintan <klodoma@ar-sd.net>
  0 siblings, 1 reply; 4+ messages in thread

From: Michael Fuhr @ 2005-01-20 09:33 UTC (permalink / raw)
  To: Andrei Bintintan <klodoma@ar-sd.net>; +Cc: pgsql-sql

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/



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: SQL timestamp to date cast
  2005-01-20 08:52 SQL timestamp to date cast Andrei Bintintan <klodoma@ar-sd.net>
  2005-01-20 09:33 ` Re: SQL timestamp to date cast Michael Fuhr <mike@fuhr.org>
@ 2005-01-20 09:41   ` Andrei Bintintan <klodoma@ar-sd.net>
  2005-01-20 09:51     ` Re: SQL timestamp to date cast Michael Fuhr <mike@fuhr.org>
  0 siblings, 1 reply; 4+ messages in thread

From: Andrei Bintintan @ 2005-01-20 09:41 UTC (permalink / raw)
  To: Michael Fuhr <mike@fuhr.org>; +Cc: pgsql-sql

> 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" <mike@fuhr.org>
To: "Andrei Bintintan" <klodoma@ar-sd.net>
Cc: <pgsql-sql@postgresql.org>
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/
> 




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: SQL timestamp to date cast
  2005-01-20 08:52 SQL timestamp to date cast Andrei Bintintan <klodoma@ar-sd.net>
  2005-01-20 09:33 ` Re: SQL timestamp to date cast Michael Fuhr <mike@fuhr.org>
  2005-01-20 09:41   ` Re: SQL timestamp to date cast Andrei Bintintan <klodoma@ar-sd.net>
@ 2005-01-20 09:51     ` Michael Fuhr <mike@fuhr.org>
  0 siblings, 0 replies; 4+ messages in thread

From: Michael Fuhr @ 2005-01-20 09:51 UTC (permalink / raw)
  To: Andrei Bintintan <klodoma@ar-sd.net>; +Cc: pgsql-sql

On Thu, Jan 20, 2005 at 11:41:41AM +0200, Andrei Bintintan wrote:
> >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. :))

See the "Indexes on Expressions" section in the "Indexes" chapter
of the documentation.

-- 
Michael Fuhr
http://www.fuhr.org/~mfuhr/



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2005-01-20 09:51 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-01-20 08:52 SQL timestamp to date cast Andrei Bintintan <klodoma@ar-sd.net>
2005-01-20 09:33 ` Michael Fuhr <mike@fuhr.org>
2005-01-20 09:41   ` Andrei Bintintan <klodoma@ar-sd.net>
2005-01-20 09:51     ` Michael Fuhr <mike@fuhr.org>

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