Re: Group by, count, order by and limit

From: Christoph Haller <ch(at)rodos(dot)fzk(dot)de>
To: pgsql-hackers(at)postgresql(dot)org
Cc: anuradha(at)lklug(dot)pdn(dot)ac(dot)lk
Subject: Re: Group by, count, order by and limit
Date: 2003-02-18 10:18:05
Message-ID: 3E52085D.AC957AD0@rodos.fzk.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

>
> Consider this query on a large table with lots of different IDs:
>
> SELECT id FROM my_table GROUP BY id ORDER BY count(id) LIMIT 10;
>
> It has an index on id. Obviously, the index helps to evaluate
count(id)
> for a given value of id, but count()s for all the `id's should be
> evaluated, so sort() will take most of the time.
>
> Is there a way to improve performance of this query? If not, please
> give some indication to do a workaround on the source itself, so
perhaps
> I may be able to come out with a patch.
>
Is there a difference in performance if you re-write it as

SELECT id, count(id) FROM my_table GROUP BY id ORDER BY 2 LIMIT 10 ;

?

Regards, Christoph

Browse pgsql-hackers by date

  From Date Subject
Next Message Christoph Haller 2003-02-18 12:13:30 Re: pg environment? metadata?
Previous Message Kevin Brown 2003-02-18 09:40:49 Re: Detecting corrupted pages earlier