Re: count of occurences PLUS optimisation

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: "Thurstan R(dot) McDougle" <trmcdougle(at)my-deja(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: count of occurences PLUS optimisation
Date: 2001-09-14 06:38:00
Message-ID: 20010914163800.A8613@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

On Thu, Sep 13, 2001 at 05:38:56PM +0100, Thurstan R. McDougle wrote:
> What I am talking about is WHEN the sort is required we could make the
> sort more efficient as inserting into a SHORT ordered list should be
> better than building a BIG list and sorting it, then only keeping a
> small part of the list.

For a plain SORT, it would be possible. Anything to avoid materialising the
entire table in memory. Unfortunatly it won't help if there is a GROUP
afterwards because the group can't really know when to stop.

But yes, if you had LIMIT<SORT<...>> you could do that. I can't imagine it
would be too hard to arrange.

> In the example in question there would be perhaps 400 records, but only
> 10 are needed. From the questions on these lists it seems quite common
> for only a very low proportion of the records to be required (less then
> 10%/upto 100 typically), in these cases it would seem to be a usefull
> optimisation.

Say you have a query:

select id, count(*) from test group by id order by count desc limit 10;

This becomes:

LIMIT < SORT < GROUP < SORT < test > > > >

The inner sort would still have to scan the whole table, unless you have an
index on id. In that case your optimisation would be cool.

Have I got it right now?

--
Martijn van Oosterhout <kleptog(at)svana(dot)org>
http://svana.org/kleptog/
> Magnetism, electricity and motion are like a three-for-two special offer:
> if you have two of them, the third one comes free.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ryan Mahoney 2001-09-14 08:25:21 Re: business perspective
Previous Message Corn 2001-09-14 05:05:27 How to implement transaction in plpgsql?

Browse pgsql-hackers by date

  From Date Subject
Next Message Marius Andreiana 2001-09-14 11:45:29 XML support?
Previous Message jiby george 2001-09-14 05:56:16 querying system catalogs to extract foreign keys