pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedCan this query be optimized?
7+ messages / 5 participants
[nested] [flat]
* Can this query be optimized?
@ 2001-11-07 21:00 Claus Heiko Niesen <cniesen@gmx.net>
2001-11-08 14:30 ` Re: Can this query be optimized? Tom Lane <tgl@sss.pgh.pa.us>
2001-11-08 19:11 ` Re: Can this query be optimized? Claus Heiko Niesen <cniesen@gmx.net>
0 siblings, 2 replies; 7+ messages in thread
From: Claus Heiko Niesen @ 2001-11-07 21:00 UTC (permalink / raw)
To: pgsql-sql
I'm stuck optimizing the following query:
select distinct extract(year from date) as year, extract(month from date)
as month, date from week_pics order by date desc;
The table "week_pics" contains the column "date" which is indexed in
descending order. Unfortunately postresql isn't using the index since I'm
using extract and order by the resulting values. Instead postgresql will
sort the result set. To order by the date implies that the date needs to
be in my select statement, which interferes with uniqueness of the
year-month pair.
Is there anything I could do to optimize this query besides breaking the
date into three separate columns?
Claus
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Can this query be optimized?
2001-11-07 21:00 Can this query be optimized? Claus Heiko Niesen <cniesen@gmx.net>
@ 2001-11-08 14:30 ` Tom Lane <tgl@sss.pgh.pa.us>
2001-11-08 16:49 ` Re: Can this query be optimized? Josh Berkus <josh@agliodbs.com>
2001-11-08 19:19 ` Re: Can this query be optimized? Claus Heiko Niesen <cniesen@gmx.net>
2001-11-09 00:26 ` Re: Can this query be optimized? fstelpstra@yahoo.com <fstelpstra@yahoo.com>
1 sibling, 3 replies; 7+ messages in thread
From: Tom Lane @ 2001-11-08 14:30 UTC (permalink / raw)
To: Claus Heiko Niesen <cniesen@gmx.net>; +Cc: pgsql-sql
Claus Heiko Niesen <cniesen@gmx.net> writes:
> I'm stuck optimizing the following query:
> select distinct extract(year from date) as year, extract(month from date)
> as month, date from week_pics order by date desc;
Perhaps it would work to forget the "distinct" and instead GROUP BY
date.
regards, tom lane
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Can this query be optimized?
2001-11-07 21:00 Can this query be optimized? Claus Heiko Niesen <cniesen@gmx.net>
2001-11-08 14:30 ` Re: Can this query be optimized? Tom Lane <tgl@sss.pgh.pa.us>
@ 2001-11-08 16:49 ` Josh Berkus <josh@agliodbs.com>
2 siblings, 0 replies; 7+ messages in thread
From: Josh Berkus @ 2001-11-08 16:49 UTC (permalink / raw)
To: Claus Heiko Niesen <cniesen@gmx.net>; +Cc: pgsql-sql
Claus,
> > I'm stuck optimizing the following query:
> > select distinct extract(year from date) as year, extract(month from
> date)
> > as month, date from week_pics order by date desc;
>
> Perhaps it would work to forget the "distinct" and instead GROUP BY
> date.
Also, do the following:
-- Make sure you have an index on week_pics.date
(Tom, did you guys ever straignten out the issue with DESC sorts on
indexes?)
-- Build the above query as a VIEW, which should improve performance
somewhat.
Technically, you could build an index on extract(month from date), I
believe, but I'm not sure how effective such an index would be in
practice.
-Josh
______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh@agliodbs.com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Can this query be optimized?
2001-11-07 21:00 Can this query be optimized? Claus Heiko Niesen <cniesen@gmx.net>
2001-11-08 14:30 ` Re: Can this query be optimized? Tom Lane <tgl@sss.pgh.pa.us>
@ 2001-11-08 19:19 ` Claus Heiko Niesen <cniesen@gmx.net>
2001-11-08 20:00 ` Re: Can this query be optimized? Ross J. Reedstrom <reedstrm@rice.edu>
2 siblings, 1 reply; 7+ messages in thread
From: Claus Heiko Niesen @ 2001-11-08 19:19 UTC (permalink / raw)
To: Josh Berkus <josh@agliodbs.com>; +Cc: pgsql-sql
At 10:49 AM 11/08/2001, Josh wrote:
>Claus,
>
> > > I'm stuck optimizing the following query:
> > > select distinct extract(year from date) as year, extract(month from
> > date)
> > > as month, date from week_pics order by date desc;
> >
> > Perhaps it would work to forget the "distinct" and instead GROUP BY
> > date.
>
>Also, do the following:
>-- Make sure you have an index on week_pics.date
Yes I do have one where the date is the first key of the index. But since
I only extract partial data from the index column postgresql seems to
ignore the index. Probably to difficult for it to see that the index is in
the extract(year from date), extract(month from date) order.
>-- Build the above query as a VIEW, which should improve performance
>somewhat.
What's the secret about the improved performance. Is it just that the
query access plan doesn't have to be determined every time the query is
executed or is there something else?
>Technically, you could build an index on extract(month from date), I
>believe, but I'm not sure how effective such an index would be in
>practice.
I just tried to build an index to see what the explain path would do but I
get an error on the word extract:
# create index week_pics_month_key on week_pics (extract(year from
time_stamp), extract(month from time_stamp));
ERROR: parser: parse error at or near "extract"
>-Josh
Thanks for the help Josh
Claus
PS: Accidently I had also the date column selected in the query that I
posted. This was incorrect and the query should have been:
select distinct extract(year from date) as year, extract(month from date)
as month from week_pics order by year desc, month desc;
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Can this query be optimized?
2001-11-07 21:00 Can this query be optimized? Claus Heiko Niesen <cniesen@gmx.net>
2001-11-08 14:30 ` Re: Can this query be optimized? Tom Lane <tgl@sss.pgh.pa.us>
2001-11-08 19:19 ` Re: Can this query be optimized? Claus Heiko Niesen <cniesen@gmx.net>
@ 2001-11-08 20:00 ` Ross J. Reedstrom <reedstrm@rice.edu>
0 siblings, 0 replies; 7+ messages in thread
From: Ross J. Reedstrom @ 2001-11-08 20:00 UTC (permalink / raw)
To: Claus Heiko Niesen <cniesen@gmx.net>; +Cc: Josh Berkus <josh@agliodbs.com>; pgsql-sql
On Thu, Nov 08, 2001 at 01:19:25PM -0600, Claus Heiko Niesen wrote:
> At 10:49 AM 11/08/2001, Josh wrote:
>
> I just tried to build an index to see what the explain path would do but I
> get an error on the word extract:
>
> # create index week_pics_month_key on week_pics (extract(year from
> time_stamp), extract(month from time_stamp));
> ERROR: parser: parse error at or near "extract"
It's erroring on the second use of extract(). You can have multicolumn
indices, or you can have functional indices. You can't have a
multifunctional index (right now. I have'nt looked at the code to
see why.)
Ross
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Can this query be optimized?
2001-11-07 21:00 Can this query be optimized? Claus Heiko Niesen <cniesen@gmx.net>
2001-11-08 14:30 ` Re: Can this query be optimized? Tom Lane <tgl@sss.pgh.pa.us>
@ 2001-11-09 00:26 ` fstelpstra@yahoo.com <fstelpstra@yahoo.com>
2 siblings, 0 replies; 7+ messages in thread
From: fstelpstra@yahoo.com @ 2001-11-09 00:26 UTC (permalink / raw)
To: pgsql-sql
Hi,
Isn't it much faster if you extract a substring from the date field?
For example if the date field returns 2001/12/30, you could use the 4th and
5th position for the month part.
regards,
freek
Tom Lane wrote:
> Claus Heiko Niesen <cniesen@gmx.net> writes:
>> I'm stuck optimizing the following query:
>> select distinct extract(year from date) as year, extract(month from date)
>> as month, date from week_pics order by date desc;
>
> Perhaps it would work to forget the "distinct" and instead GROUP BY
> date.
>
> regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
>
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Can this query be optimized?
2001-11-07 21:00 Can this query be optimized? Claus Heiko Niesen <cniesen@gmx.net>
@ 2001-11-08 19:11 ` Claus Heiko Niesen <cniesen@gmx.net>
1 sibling, 0 replies; 7+ messages in thread
From: Claus Heiko Niesen @ 2001-11-08 19:11 UTC (permalink / raw)
To: pgsql-sql
Ooops, I posted one test SQL statements which really didn't make
sense. This is the correct one:
select distinct extract(year from date) as year, extract(month from date)
as month from week_pics order by year desc, month desc;
I try to get a list of all the months that have a picture. So the day part
of the date should be ignored.
Claus
At 08:30 AM 11/08/2001, Tom wrote:
>Claus Heiko Niesen <cniesen@gmx.net> writes:
> > I'm stuck optimizing the following query:
> > select distinct extract(year from date) as year, extract(month from date)
> > as month, date from week_pics order by date desc;
>
>Perhaps it would work to forget the "distinct" and instead GROUP BY
>date.
>
> regards, tom lane
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2001-11-09 00:26 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-11-07 21:00 Can this query be optimized? Claus Heiko Niesen <cniesen@gmx.net>
2001-11-08 14:30 ` Tom Lane <tgl@sss.pgh.pa.us>
2001-11-08 16:49 ` Josh Berkus <josh@agliodbs.com>
2001-11-08 19:19 ` Claus Heiko Niesen <cniesen@gmx.net>
2001-11-08 20:00 ` Ross J. Reedstrom <reedstrm@rice.edu>
2001-11-09 00:26 ` fstelpstra@yahoo.com <fstelpstra@yahoo.com>
2001-11-08 19:11 ` Claus Heiko Niesen <cniesen@gmx.net>
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