Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 03A74F6332B for ; Thu, 19 Jul 2012 09:44:30 -0300 (ADT) Received: from plane.gmane.org ([80.91.229.3]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Srq5l-00038l-6y for pgsql-sql@postgresql.org; Thu, 19 Jul 2012 12:44:30 +0000 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1Srq5T-0008Rw-Fn for pgsql-sql@postgresql.org; Thu, 19 Jul 2012 14:44:11 +0200 Received: from 217.110.94.67 ([217.110.94.67]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 19 Jul 2012 14:44:11 +0200 Received: from spam_eater by 217.110.94.67 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 19 Jul 2012 14:44:11 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Thomas Kellerer Subject: DELETE using an outer join Date: Thu, 19 Jul 2012 14:43:46 +0200 Lines: 38 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 217.110.94.67 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 X-Pg-Spam-Score: -0.7 (/) X-Archive-Number: 201207/19 X-Sequence-Number: 36756 Hi, (this is not a real world problem, just something I'm playing around with). Lately I had some queries of the form: select t.* from some_table t where t.id not in (select some_id from some_other_table); I could improve the performance of them drastically by changing the NOT NULL into an outer join: select t.* from some_table t left join some_other_table ot on ot.id = t.id where ot.id is null; Now I was wondering if a DELETE statement could be rewritten with the same "strategy": Something like: delete from some_table where id not in (select min(id) from some_table group by col1, col2 having count(*) > 1); (It's the usual - at least for me - "get rid of duplicates" statement) The DELETE .. USING seems to only allow inner joins because it requires the join to be done in the WHERE clause. So I can't think of a way to turn that NOT IN from the DELETE into an outer join with a derived table. Am I right that this kind of transformation is not possible or am I missing something? Regards Thomas