Обсуждение: Can this query be optimized?

Поиск
Список
Период
Сортировка

Can this query be optimized?

От
Claus Heiko Niesen
Дата:
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



Re: Can this query be optimized?

От
Tom Lane
Дата:
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


Re: Can this query be optimized?

От
"Josh Berkus"
Дата:
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
informationtechnology      josh@agliodbs.com  and data management solutions       (415) 565-7293 for law firms, small
businesses       fax 621-2533   and non-profit organizations.      San Francisco
 


Re: Can this query be optimized?

От
Claus Heiko Niesen
Дата:
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




Re: Can this query be optimized?

От
Claus Heiko Niesen
Дата:
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;




Re: Can this query be optimized?

От
"Ross J. Reedstrom"
Дата:
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


Re: Can this query be optimized?

От
"fstelpstra@yahoo.com"
Дата:
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)
>