Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id C693F9FB72F for ; Mon, 9 Apr 2007 17:55:40 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 47589-07 for ; Mon, 9 Apr 2007 17:55:27 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 575019FB6B6 for ; Mon, 9 Apr 2007 17:55:30 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id l39KtR1V010503; Mon, 9 Apr 2007 16:55:28 -0400 (EDT) To: "Spiegelberg, Greg" cc: pgsql-performance@postgresql.org Subject: Re: DELETE with filter on ctid In-reply-to: <82E74D266CB9B44390D3CCE44A781ED90B6ADC@POSTOFFICE.cranel.local> References: <82E74D266CB9B44390D3CCE44A781ED90B6ADC@POSTOFFICE.cranel.local> Comments: In-reply-to "Spiegelberg, Greg" message dated "Mon, 09 Apr 2007 16:01:53 -0400" Date: Mon, 09 Apr 2007 16:55:27 -0400 Message-ID: <10502.1176152127@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200704/212 X-Sequence-Number: 24089 "Spiegelberg, Greg" writes: > We have a query which generates a small set of rows (~1,000) which are > to be used in a DELETE on the same table. The problem we have is that > we need to join on 5 different columns and it takes far too long. I > have a solution but I'm not sure it's the right one. Instead of joining > on 5 columns in the DELETE the join uses the ctid column. > BEGIN; > CREATE INDEX gregs_table_ctid_idx ON gregs_table(ctid); > DELETE FROM gregs_table gt > USING (SELECT ctid FROM gregs_table WHERE ...) as s > WHERE gt.ctid=s.ctid; > DROP INDEX gregs_table_ctid_idx; > COMMIT; Forget the index, it's useless here (hint: ctid is a physical address). I'm wondering though why you don't just transpose the subquery's WHERE condition into the DELETE's WHERE? Or is this example oversimplified? regards, tom lane