![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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) |
#3
| |||
| |||
|
|
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": |
#4
| |||
| |||
|
|
If you're using a reasonably recent version of PG, replacing the NOT IN by a NOT EXISTS test should also help. |
|
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. |
#5
| |||
| |||
|
|
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. |
#6
| |||
| |||
|
|
On Thu, Jul 19, 2012 at 6:52 PM, Tom Lane <tgl (AT) sss (DOT) pgh.pa.us> 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 works. 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; But that's not an outer join |
#7
| |||
| |||
|
|
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 works. 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; But that's not an outer join |
#8
| |||
| |||
|
|
On Thu, Jul 19, 2012 at 6:52 PM, Tom Lane <tgl (AT) sss (DOT) pgh.pa.us> 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 works. 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; |
#9
| |||
| |||
|
|
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 self-join on the primary key and then left join to the other table, but that's pretty klugy and inefficient. What was being discussed is allowing people to write directly DELETE FROM some_table USING some_table LEFT JOIN other_table ... where the respecification of the table in USING would be understood to mean the target table. Right now this is an error case because of duplicate table aliases. |
![]() |
| Thread Tools | |
| Display Modes | |
| |