Group by, count, order by and limit

From: Anuradha Ratnaweera <anuradha(at)lklug(dot)pdn(dot)ac(dot)lk>
To: pgsql-general(at)postgresql(dot)org, pgsql-hackers(at)postgresql(dot)org
Cc: anuradha(at)gnu(dot)org
Subject: Group by, count, order by and limit
Date: 2003-02-18 04:26:46
Message-ID: 20030218042646.GA5847@lklug.pdn.ac.lk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers


My 3rd attempt to post ...

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.

Thanks in advance.

Anuradha

--

Debian GNU/Linux (kernel 2.4.21-pre4)

There are three ways to get something done:
(1) Do it yourself.
(2) Hire someone to do it for you.
(3) Forbid your kids to do it.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-02-18 04:40:21 Re: Index not used with IS NULL
Previous Message Dennis Gearon 2003-02-18 03:42:28 Re: Index not used with IS NULL

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2003-02-18 04:49:58 Re: Detecting corrupted pages earlier
Previous Message Tom Lane 2003-02-18 04:04:46 Re: Detecting corrupted pages earlier