Re: efficiency of group by 1 order by 1

From: "Merlin Moncure" <mmoncure(at)gmail(dot)com>
To: tjo(at)acm(dot)org
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: efficiency of group by 1 order by 1
Date: 2006-03-17 18:15:53
Message-ID: b42b73150603171015l4686dc17y1281e3625d318be8@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> I use a similar SQL, e.g.:
> select func(x)....group by func(x) order by func(x)
> but my func is rather expensive. Is func(x) evaluated three times
> in the above statement? Would it be evaluated only once if I used
> select func(x)....group by 1 order by 1

try:

select q.v from (select func(t.x) as v from t) q order group by 1 order by 1;

Is your function immutable? You have to be very careful with
expensive functions in the select clause. for example

select f(x) from t where id = 1 order by n;

can cause f to execute for the entire table even if id is unique.
Solution is to subquery as in the above.

merlin

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message karly 2006-03-17 19:21:23 pgsql variables from records
Previous Message SunWuKung 2006-03-17 18:07:06 Re: pgsql variables from records