X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 61AEE53388 for ; Wed, 23 Mar 2005 11:04:14 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 28894-06 for ; Wed, 23 Mar 2005 11:04:04 +0000 (GMT) Received: from mx5.net.nih.gov (mailfwd.nih.gov [165.112.130.36]) by svr1.postgresql.org (Postfix) with ESMTP id 611EB5339B for ; Wed, 23 Mar 2005 11:04:03 +0000 (GMT) Received: from mx5.net.nih.gov (localhost [127.0.0.1]) by mx5.net.nih.gov (8.12.10/8.11.6) with ESMTP id j2NB45au008569 for ; Wed, 23 Mar 2005 06:04:05 -0500 Received: from [128.231.145.14] (holmes.nhgri.nih.gov [128.231.145.14]) by mx5.net.nih.gov (8.12.10/8.11.6) with ESMTP id j2NB45Kd008563; Wed, 23 Mar 2005 06:04:05 -0500 In-Reply-To: <000a01c52f3c$3ee7a980$1f6df345@WATSON> References: <0af8c6d3baaf3d4a6d918df294019507@mail.nih.gov> <7e154914b76fb1a7c55912b621400860@mail.nih.gov> <4240874E.2080301@archonet.com> <000a01c52f3c$3ee7a980$1f6df345@WATSON> Mime-Version: 1.0 (Apple Message framework v619.2) Content-Type: text/plain; charset=US-ASCII; format=flowed Message-Id: <117faba5d7a78ad4c2ca7bd81f879224@mail.nih.gov> Content-Transfer-Encoding: 7bit Cc: "PostgreSQL SQL" , "Richard Huxton" From: Sean Davis Subject: Re: Self-referencing table question Date: Wed, 23 Mar 2005 06:04:04 -0500 To: "Sean Davis" X-Mailer: Apple Mail (2.619.2) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200503/313 X-Sequence-Number: 20792 On Mar 22, 2005, at 7:07 PM, Sean Davis wrote: > > ----- Original Message ----- From: "Richard Huxton" > To: "Sean Davis" > Cc: "PostgreSQL SQL" > 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> >> 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