|
|
 | | From: | Andrei Bintintan | | Subject: | SQL timestamp to date cast | | Date: | Thu, 20 Jan 2005 10:52:50 +0200 |
|
|
 | This is a multi-part message in MIME format.
------=_NextPart_000_0068_01C4FEDE.2F2671E0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Hi,=20
I have the following query:
SELECT DISTINCT(nummer) FROM user_action WHERE = datetime::date=3D'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.=20
Explain analyze SELECT DISTINCT(nummer) FROM user_action WHERE = datetime::date=3D'11/11/2004' AND id_action IN (5,6,9)
Unique (cost=3D18141.71..18143.72 rows=3D45 width=3D4) (actual = time=3D418.122..418.340 rows=3D85 loops=3D1) -> Sort (cost=3D18141.71..18142.72 rows=3D402 width=3D4) (actual = time=3D418.119..418.194 rows=3D192 loops=3D1) Sort Key: nummer -> Seq Scan on user_action (cost=3D0.00..18124.33 rows=3D402 = width=3D4) (actual time=3D366.240..417.890 rows=3D192 loops=3D1) Filter: (((datetime)::date =3D '2004-11-11'::date) AND = ((id_action =3D 5) OR (id_action =3D 6) OR (id_action =3D 9))) Total runtime: 418.419 ms
Best regards. Andy. ------=_NextPart_000_0068_01C4FEDE.2F2671E0 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
charset=3Diso-8859-1">
Hi,
I have the following = query:
SELECT DISTINCT(nummer) FROM = user_action WHERE=20 datetime::date=3D'11/11/2004' AND id_action IN (5,6,9)
Now, datetime has the type timestamp. = How can I=20 make an index or write different this query so that it runs faster? It = really=20 takes some time sometimes. Usually about 3-4secs. user_action has about = 300k=20 rows and increasing ~ 5-10k a day. Explain size=3D2>analyze=20 SELECT DISTINCT(nummer) FROM user_action WHERE = datetime::date=3D'11/11/2004' AND=20 id_action IN (5,6,9)
Unique (cost=3D18141.71..18143.72 = rows=3D45=20 width=3D4) (actual time=3D418.122..418.340 rows=3D85 = loops=3D1) -> =20 Sort (cost=3D18141.71..18142.72 rows=3D402 width=3D4) (actual=20 time=3D418.119..418.194 rows=3D192=20 loops=3D1) Sort Key:=20 nummer -> Seq = Scan on=20 user_action (cost=3D0.00..18124.33 rows=3D402 width=3D4) (actual=20 time=3D366.240..417.890 rows=3D192=20 loops=3D1)  = ; =20 Filter: (((datetime)::date =3D '2004-11-11'::date) AND ((id_action =3D = 5) OR=20 (id_action =3D 6) OR (id_action =3D 9))) Total runtime: 418.419=20 ms
Best regards.
Andy.
------=_NextPart_000_0068_01C4FEDE.2F2671E0--
|
|
 | | From: | Michael Fuhr | | Subject: | Re: SQL timestamp to date cast | | Date: | Thu, 20 Jan 2005 02:33:31 -0700 |
|
|
 | 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/
---------------------------(end of broadcast)--------------------------- TIP 9: the planner will ignore your desire to choose an index scan if your joining column's datatypes do not match
|
|
 | | From: | Andrei Bintintan | | Subject: | Re: SQL timestamp to date cast | | Date: | Thu, 20 Jan 2005 11:41:41 +0200 |
|
|
 | > 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/ >
---------------------------(end of broadcast)--------------------------- TIP 2: you can get off all lists at once with the unregister command (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
|
|
 | | From: | Michael Fuhr | | Subject: | Re: SQL timestamp to date cast | | Date: | Thu, 20 Jan 2005 02:51:16 -0700 |
|
|
 | 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/
---------------------------(end of broadcast)--------------------------- TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
|
|
|