Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 510A19F9D61 for ; Mon, 13 Aug 2007 16:40:46 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 92274-04 for ; Mon, 13 Aug 2007 16:40:43 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 2587A9F9D2F for ; Mon, 13 Aug 2007 16:40:43 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.1/8.14.1) with ESMTP id l7DJbe6v020124; Mon, 13 Aug 2007 15:37:41 -0400 (EDT) To: ksimpson@mailchannels.com cc: pgsql-sql@postgresql.org Subject: Re: Comparing two slices within one table efficiently In-reply-to: <1694202581-1187027999-cardhu_decombobulator_blackberry.rim.net-401169594-@bxe119.bisx.prod.on.blackberry> References: <1694202581-1187027999-cardhu_decombobulator_blackberry.rim.net-401169594-@bxe119.bisx.prod.on.blackberry> Comments: In-reply-to "=?utf-8?B?S2VuIFNpbXBzb24=?=" message dated "Mon, 13 Aug 2007 17:58:16 -0000" Date: Mon, 13 Aug 2007 15:37:40 -0400 Message-ID: <20123.1187033860@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200708/112 X-Sequence-Number: 29050 "=?utf-8?B?S2VuIFNpbXBzb24=?=" writes: > select * from t t1 where exists (select 1 from t t2 where > t2.mta_id=t1.mta_id and t2.domain_id=t1.domain_id and (t2.attribute1 > != t1.attribute1 or t2.attribute2 != t1.attribute2) > This query takes millenia... Yeah, because you're effectively forcing the least efficient style of join --- a nestloop is generally going to suck for a full-table join, even if you've got indexes. Try something like this: select * from t t1 join t t2 on (t2.mta_id=t1.mta_id and t2.domain_id=t1.domain_id) where (t2.attribute1 != t1.attribute1 or t2.attribute2 != t1.attribute2) Make sure you've got work_mem cranked up to something appropriate. regards, tom lane