Re: PostgreSQL runs a query much slower than BDE and MySQL

From: "Peter Hardman" <peter(at)ssbg(dot)zetnet(dot)co(dot)uk>
To: pgsql-performance(at)postgresql(dot)org
Cc: Mario Weilguni <mweilguni(at)sime(dot)com>
Subject: Re: PostgreSQL runs a query much slower than BDE and MySQL
Date: 2006-08-17 09:07:54
Message-ID: 44E43FFA.23823.5E0BDD@peter.ssbg.zetnet.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance


On 17 Aug 2006 at 10:00, Mario Weilguni wrote:

> not really sure if this is right without any testdata, but isn't that what you
> want?
>
> CREATE index foo on sheep_flock (flock_no);
>
> SELECT DISTINCT on (f1.transfer_date) f1.regn_no, f1.transfer_date as date_in
> FROM SHEEP_FLOCK f1
> WHERE f1.flock_no = '1359'
> order by f1.transfer_date desc;
>
> best regards,
> mario weilguni
>
>
Mario, Thanks for the suggestion, but this query produces the wrong answer - but
then I provided no data, nor properly explained what the data would be.
Each sheep will have multiple records, starting with one for when it's first
registered, then one for each flock it's in (eg sold into) then one for when it dies
and goes to the 'big flock in the sky'.

So first I need to find the most recent record for each sheep and then select the
sheep who's most recent record matches the flock in question.

Your query finds all the sheep that have been in the flock in question, then selects
the first one from each set of records with the same date. So it collects data on
dead sheep, and only selects one sheep if several were bought or registered on
the same day.

Forgive me for being verbose - I want to make sure I understand it propely myself!

regards,
--
Peter Hardman
Acre Cottage, Horsebridge
King's Somborne
Stockbridge
SO20 6PT

== Breeder of Shetland Cattle and Shetland Sheep ==

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Peter Hardman 2006-08-17 09:21:01 Re: PostgreSQL runs a query much slower than BDE and MySQL
Previous Message lst_hoe01 2006-08-17 06:40:17 Re: unsubscribe