Re: [SQL] DELETE using an outer join

2012-07-20 Thread Sergey Konoplev
On Fri, Jul 20, 2012 at 5:51 PM, Tom Lane wrote: >> DELETE FROM some_table USING some_table AS s >> WHERE >> some_table.col1 = s.col1 AND >> some_table.col2 = s.col2 AND >> some_table.id < s.id; > > No, that's a self-join, which isn't what the OP wanted. You can make it > work if you

Re: [SQL] DELETE using an outer join

2012-07-20 Thread Tom Lane
Sergey Konoplev writes: > On Thu, Jul 19, 2012 at 6:52 PM, Tom Lane wrote: >>> Now I was wondering if a DELETE statement could be rewritten with the same >>> "strategy": >> Not at the moment. There have been discussions of allowing the same >> table name to be respecified in USING, but there a

Re: [SQL] DELETE using an outer join

2012-07-20 Thread Sergey Konoplev
On Fri, Jul 20, 2012 at 2:27 PM, Thomas Kellerer wrote: Now I was wondering if a DELETE statement could be rewritten with the same "strategy": >>> >>> Not at the moment. There have been discussions of allowing the same >>> table name to be respecified in USING, but there are complicatio

Re: [SQL] DELETE using an outer join

2012-07-20 Thread Thomas Kellerer
Sergey Konoplev, 20.07.2012 10:21: On Thu, Jul 19, 2012 at 6:52 PM, Tom Lane wrote: Now I was wondering if a DELETE statement could be rewritten with the same "strategy": Not at the moment. There have been discussions of allowing the same table name to be respecified in USING, but there are

Re: [SQL] DELETE using an outer join

2012-07-20 Thread Sergey Konoplev
On Thu, Jul 19, 2012 at 6:52 PM, Tom Lane wrote: >> Now I was wondering if a DELETE statement could be rewritten with the same >> "strategy": > > Not at the moment. There have been discussions of allowing the same > table name to be respecified in USING, but there are complications. However it

Re: [SQL] DELETE using an outer join

2012-07-20 Thread Thomas Kellerer
Tom Lane, 19.07.2012 16:52: If you're using a reasonably recent version of PG, replacing the NOT IN by a NOT EXISTS test should also help. Thanks. I wasn't aware of that (and the NOT EXISTS does indeed produce the same plan as the OUTER JOIN solution) Now I was wondering if a DELETE stateme

Re: [SQL] DELETE using an outer join

2012-07-19 Thread Tom Lane
Thomas Kellerer writes: > 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.* >

Re: [SQL] DELETE using an outer join

2012-07-19 Thread Sergey Konoplev
On Thu, Jul 19, 2012 at 4:43 PM, Thomas Kellerer wrote: >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

[SQL] DELETE using an outer join

2012-07-19 Thread Thomas Kellerer
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 i