pgsql DATE

Started by Andras Baloghalmost 26 years ago6 messagesgeneral
Jump to latest
#1Andras Balogh
abalogh@grafx.ro

Hi all,

I have a question. I don't know what date type to use with postgres.
I have to have a date like '26/04/2000 14:30'. I have created my field as
varchar
and doing all the querying with comparing:
SELECT * FROM pages WHERE date_time ~* '04/2000'
This would return all records from April 2000.

But if in my table are lot of records(150.000) this will be very slow. I
have thought to
create an index but in won't help me because the date_time field is varchar.

So i am looking a date type that would allow me to do the following with one
(or a few) query
(and to be fast too):
-select all records for a given month
-select the count of records where the hour is 14.
-select all the records from the 7th week.
-select count of records where the day of the week is Sunday.
etc.

It is possible to create indexes with date types?

If anybody has some comments please answer me.

Thank You for reading this mail.

Best wishes,

Andras.

#2Martijn van Oosterhout
kleptog@svana.org
In reply to: Andras Balogh (#1)
Re: pgsql DATE

Andras Balogh wrote:

So i am looking a date type that would allow me to do the following with one
(or a few) query
(and to be fast too):
-select all records for a given month
-select the count of records where the hour is 14.
-select all the records from the 7th week.
-select count of records where the day of the week is Sunday.
etc.

How about you write your queries like:

date >= '01/4/2000' and date < '01/5/2000'

or equivalent. It allows use of the index but works the same.
Except for the day is Sunday though...
--
Martijn van Oosterhout <kleptog@cupid.suninternet.com>
http://cupid.suninternet.com/~kleptog/

#3Charles Tassell
ctassell@isn.net
In reply to: Martijn van Oosterhout (#2)
Re: pgsql DATE

Might want to try date BETWEEN '01/4/2000' AND '01/5/2000' as well, this
is probably optimized better (although I may be wrong.)

At 11:41 AM 4/26/00, Martijn van Oosterhout wrote:

Show quoted text

Andras Balogh wrote:

So i am looking a date type that would allow me to do the following

with one

(or a few) query
(and to be fast too):
-select all records for a given month
-select the count of records where the hour is 14.
-select all the records from the 7th week.
-select count of records where the day of the week is Sunday.
etc.

How about you write your queries like:

date >= '01/4/2000' and date < '01/5/2000'

or equivalent. It allows use of the index but works the same.
Except for the day is Sunday though...
--
Martijn van Oosterhout <kleptog@cupid.suninternet.com>
http://cupid.suninternet.com/~kleptog/

#4Andras Balogh
abalogh@grafx.ro
In reply to: Andras Balogh (#1)
Re: pgsql DATE

Hi,

Thank You for your reply.
BUT as i mentioned the date_time field is VARCHAR.
So if i use:
date >= '01/4/2000' and date < '01/5/2000'

this won't compare the dates it will compare Strings.
so '02/4/2000' will be GREATER than '01/5/2000'.

That is why i need a DATE TYPE for my field that can be used to create an
index.

So any other ideas?

----- Original Message -----
From: Martijn van Oosterhout <kleptog@cupid.suninternet.com>
To: Andras Balogh <abalogh@grafx.ro>
Cc: <pgsql-general@postgresql.org>
Sent: Wednesday, April 26, 2000 5:41 PM
Subject: Re: [GENERAL] pgsql DATE

Andras Balogh wrote:

So i am looking a date type that would allow me to do the following with

one

Show quoted text

(or a few) query
(and to be fast too):
-select all records for a given month
-select the count of records where the hour is 14.
-select all the records from the 7th week.
-select count of records where the day of the week is Sunday.
etc.

How about you write your queries like:

date >= '01/4/2000' and date < '01/5/2000'

or equivalent. It allows use of the index but works the same.
Except for the day is Sunday though...
--
Martijn van Oosterhout <kleptog@cupid.suninternet.com>
http://cupid.suninternet.com/~kleptog/

#5Lincoln Yeoh
lylyeoh@mecomb.com
In reply to: Andras Balogh (#4)
Re: pgsql DATE

At 11:03 AM 27-04-2000 +0300, Andras Balogh wrote:

Hi,

Thank You for your reply.
BUT as i mentioned the date_time field is VARCHAR.
So if i use:
date >= '01/4/2000' and date < '01/5/2000'

this won't compare the dates it will compare Strings.
so '02/4/2000' will be GREATER than '01/5/2000'.

That is why i need a DATE TYPE for my field that can be used to create an
index.

So any other ideas?

If you need a date type for your field, use a date type.

http://www.postgresql.org/docs/postgres/datatype1143.htm

Cheerio,
Link.

#6Peter Eisentraut
peter_e@gmx.net
In reply to: Charles Tassell (#3)
Re: pgsql DATE

Charles Tassell writes:

Might want to try date BETWEEN '01/4/2000' AND '01/5/2000' as well, this
is probably optimized better (although I may be wrong.)

It isn't. It's immediately converted to the below, so it's actually a few
cycles slower to use it.

How about you write your queries like:

date >= '01/4/2000' and date < '01/5/2000'

--
Peter Eisentraut Sernanders v�g 10:115
peter_e@gmx.net 75262 Uppsala
http://yi.org/peter-e/ Sweden