Re: Self-referencing table question

From: Sean Davis <sdavis2(at)mail(dot)nih(dot)gov>
To: "Sean Davis" <sdavis2(at)mail(dot)nih(dot)gov>
Cc: "PostgreSQL SQL" <pgsql-sql(at)postgresql(dot)org>, "Richard Huxton" <dev(at)archonet(dot)com>
Subject: Re: Self-referencing table question
Date: 2005-03-23 11:04:04
Message-ID: 117faba5d7a78ad4c2ca7bd81f879224@mail.nih.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


On Mar 22, 2005, at 7:07 PM, Sean Davis wrote:

>
> ----- Original Message ----- From: "Richard Huxton" <dev(at)archonet(dot)com>
> To: "Sean Davis" <sdavis2(at)mail(dot)nih(dot)gov>
> Cc: "PostgreSQL SQL" <pgsql-sql(at)postgresql(dot)org>
> Sent: Tuesday, March 22, 2005 3:59 PM
> Subject: Re: [SQL] Self-referencing table question
>
>
>> Sean Davis wrote:
>>> I answer my own question, if only for my own records. The following
>>> query is about 5-6 times faster than the original. Of course, if
>>> anyone else has other ideas, I'd be happy to hear them.
>>>
>>> Sean
>>>
>>> explain analyze select from_id,to_id,val from exprsdb.correlation
>>> where from_id in (select to_id from exprsdb.correlation where
>>> from_id=2424 order by val desc limit 100) and to_id in (select to_id
>>> from exprsdb.correlation where from_id=2424 order by val desc limit
>>> 100) and val>0.6 and to_id<from_id;
>>
>> Might not be any faster, but you can do this as a self-join with
>> subquery:
>>
>> SELECT c1.from_id, c1.to_id, c1.val
>> FROM
>> correlation c1,
>> (
>> SELECT to_id FROM correlation WHERE from_id=2424
>> ORDER BY val DESC LIMIT 100
>> ) AS c2
>> (
>> SELECT to_id FROM correlation WHERE from_id=2424
>> ORDER BY val DESC LIMIT 100
>> ) AS c3
>> WHERE
>> c1.from_id = c2.to_id
>> AND c1.to_id = c3.to_id
>> AND c1.val > 0.5
>> AND c1.to_id < from_id
>> ;
>>
>> I think PG should be smart enough nowadays to figure out these two
>> queries are basically the same.

Oops, I DID do a different query in my previous email than what you
suggest in the your email. Testing both against each other, the two
queries--using subselects in 'in' and doing a self-join via
subquery--have basically the same performance.

Thanks again for the help.

Sean

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Gary Stainburn 2005-03-23 17:26:06 Re: best way to swap two records (computer details)
Previous Message Tambet Matiisen 2005-03-23 08:13:14 Re: "Flattening" query result into columns