As a general rule, correlated subqueries are not for Netezza. Obviously you can use them in many cases, but often at the cost of turning an MPP platform into a serial processor. Bad.
You can rewrite your insert a number of ways, but this seems the clearest to me. I have no idea what this logic is trying to do, but I replicated it nonetheless. You may have an issue if view
contains duplicates; this can be addressed with a little more knowledge of your data.
insert into tableA (
a
,b
,c
,d
)
select
viw.a
,viw.b
,viw.c
,viw.d
from
view viw
join tableA tba on
(viw.a = tba.a)
or (
viw.a = tba.a
and viw.b <> tba.b
or viw.c <> tba.c
or viw.d <> tba.d
)
You could remove the possibility of view duplicates by inserting into tableA
from an actual table and use the rowids found there. Perhaps something like this:
create temporary table temp_view as
select * from view
distribute on (some_key);
Then collect rowids
to insert like so:
insert into tableA (
a
,b
,c
,d
)
with mar as ( --Match 'a' records.
select
viw.rowid insert_rowid
from
temp_view viw
join tableA tba using (a)
), mnb as ( --Match against 'b'
select
viw.rowid
from
temp_view viw
join tableA tba on
viw.a = tba.a
and viw.b <> tba.b
or viw.c <> tba.c
or viw.d <> tba.d
), rws as ( --All rowids.
select * from mar
union select * from mnb
)
select
a
,b
,c
,d
from
temp_view viw
join rws on rws.insert_rowid = viw.rowid;