Re: Can this query be optimized?

From: Claus Heiko Niesen <cniesen(at)gmx(dot)net>
To: "Josh Berkus" <josh(at)agliodbs(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Can this query be optimized?
Date: 2001-11-08 19:19:25
Message-ID: 5.1.0.14.2.20011108122508.00ae2350@pop.gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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;

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Ross J. Reedstrom 2001-11-08 20:00:52 Re: Can this query be optimized?
Previous Message Claus Heiko Niesen 2001-11-08 19:11:34 Re: Can this query be optimized?