Group by count() and indexes

From: Anuradha Ratnaweera <anuradha(at)gnu(dot)com>
To: pgsql-general(at)postgresql(dot)org, pgsql-hackers(at)postgresql(dot)org
Cc: anuradha(at)gnu(dot)org
Subject: Group by count() and indexes
Date: 2003-02-17 15:43:35
Message-ID: 20030217154335.GA25666@aratnaweera.virtusa.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers


Consider the following query on a large table with lots of different
`id's:

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

It has an (usually unique) 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 work out a patch.

Thanks in advance.

Anuradha

--

Debian GNU/Linux (kernel 2.4.21-pre4)

It is contrary to reasoning to say that there is a vacuum or space in
which there is absolutely nothing.
-- Descartes

Browse pgsql-general by date

  From Date Subject
Next Message Nigel J. Andrews 2003-02-17 15:43:37 Re: Transaction Logs Recycling Problem
Previous Message Jeff Eckermann 2003-02-17 15:27:59 Re: HOWTO Migration

Browse pgsql-hackers by date

  From Date Subject
Next Message Curt Sampson 2003-02-17 15:43:59 Re: Questions about indexes?
Previous Message Christoph Haller 2003-02-17 15:36:57 Re: IpcSemaphoreKill: ...) failed: Invalid argument