![](/img/trans.png)
[英]INSERT vs INSERT UPDATE into table with many NOT NULL constraint columns
[英]Update or Insert based on key columns in Redshift
我每天都將CSV文件加載到Redshift。 為了處理重復項,我將文件加載到登台表,然后使用基於鍵列的更新或插入腳本加載到目標表。 最近,我在目標表中意外發現重復數據。
我仔細檢查了我的腳本,沒有發現重復的任何原因。 以下是我正在使用的更新和插入腳本格式。
對於插入:
Insert into target (key1, key2, col3, col4)
Select key1, key2, col3, col4
From stage s where not exists (select 1 from target t
where s.key1 = t.key1 and)
s.key2 = t.key2);
並進行更新:
Update target Set
key1=s.key1, key2=s.key2, col3=s.col3, col4=s.col4
From stage s where target.key1=s.key1 and target.key2=s.key2;
任何幫助表示贊賞。
我也遇到了這個。 問題出在插入...選擇...中,選擇本身會產生重復。 對我們來說,一種解決方案是使用游標(在Redshift之外)運行一次select並一次插入一條記錄,但這被證明存在性能問題。 相反,我們現在使用初始選擇檢查重復項
select key1,key2 from stage group by key1,key2 having count(*) > 1;
如果返回記錄,則停止該過程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.