Question

I have two tables in Netezza. Table A have ~70B records and table B have ~15K records. Delete need to happen on table A however I have to join on 2 columns. My query looks like this

Delete from A where (a.col1, a.col2) in (select col1, col2 from B).

I see plan is very costly and looking for alternative approach. Does netezza supports JOIN ON DELETE? Does anybody have any other approach??

Was it helpful?

Solution

You can use the rowid to achieve what you are looking for:

delete from table_A
where rowid in (select a.rowid
from table_A a inner join
table_B b
on a.col1=b.col1
and a.col2=b.col2)

OTHER TIPS

You can also use EXISTS() as an alternative syntax:

delete from table_A a
where exists(select 1 from table_b b
    where b.col1=a.col1
      and b.col2=a.col2
)

Edit: The overhead of this is fairly low because it doesn't have to build the join or collect anything, it just has to check if the record is there.

An alternative approach would be to create a table using CTAS without the records and rename it .I believe the cost incurred will be less here .

create table T1 as select 
col1,col2 from A where (col1,col2) 
not in (select col1,col2 from B);

drop table A;

alter table T1 rename to A;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top