Postgresql Postgres规则有助于CSV导入
我需要导入一个csv文件,每天有10的数千行到一个Postgres数据库。我正在寻找最有效的方法,因为csv文件中的每一行都可以是一条新记录或一条现有记录,如果存在,则应进行更新。经过多次搜索,我偶然发现了一个解决方案,我使用了:Postgresql Postgres规则有助于CSV导入,postgresql,csv,import,Postgresql,Csv,Import,我需要导入一个csv文件,每天有10的数千行到一个Postgres数据库。我正在寻找最有效的方法,因为csv文件中的每一行都可以是一条新记录或一条现有记录,如果存在,则应进行更新。经过多次搜索,我偶然发现了一个解决方案,我使用了: CREATE OR REPLACE RULE insert_on_duplicate_update_advertiser_campaign_keywords_table AS ON INSERT TO advertiser_campaign_keywords
CREATE OR REPLACE RULE insert_on_duplicate_update_advertiser_campaign_keywords_table AS
ON INSERT TO advertiser_campaign_keywords
WHERE (new.phrase, new.match_type, new.advertiser_campaign_id) IN (
SELECT phrase, match_type, advertiser_campaign_id
FROM advertiser_campaign_keywords
WHERE phrase = new.phrase AND match_type = new.match_type AND advertiser_campaign_id = new.advertiser_campaign_id AND state != 'deleted')
DO INSTEAD
UPDATE advertiser_campaign_keywords
SET bid_price_cpc = new.bid_price_cpc
WHERE phrase = new.phrase AND match_type = new.match_type AND advertiser_campaign_id = new.advertiser_campaign_id;
这是我找到的最接近工作解决方案,但它还不完整。它在如下所示的插入上失败:
INSERT INTO advertiser_campaign_keywords (phrase, bid_price_cpc, match_type, advertiser_campaign_id) VALUES
('dollar', 1::text::money, 'Broad', 1450),
('two words', 1.2::text::money, 'Broad', 1450),
('two words', 1.0::text::money, 'Broad', 1450),
('three words exact', 2.5::text::money, 'Exact', 1450),
('four words broad match', 1.1::text::money, 'Exclusive', 1450),
('three words exact', 2.1::text::money, 'Exact', 1450);
错误消息是:
duplicate key value violates unique constraint "unique_phrase_campaign_combo"
独特的\u短语\u活动\u组合看起来像:
CONSTRAINT "unique_phrase_campaign_combo" UNIQUE ("phrase", "advertiser_campaign_id", "match_type", "deleted_at")
除非记录被标记为已删除,否则删除的\u at为空
有人知道我如何解决这个问题吗
谢谢最好的方法是添加一个临时表。使用“复制”填充临时表。然后使用它进行插入和更新
UPDATE target_table t
SET ...
FROM staging_table s
WHERE t.id = s.id
INSERT INTO target_table
SELECT * FROM staging_table s
WHERE s.id NOT EXISTS (
SELECT id FROM target_table
)
最好的方法是添加一个临时表。使用“复制”填充临时表。然后使用它进行插入和更新
UPDATE target_table t
SET ...
FROM staging_table s
WHERE t.id = s.id
INSERT INTO target_table
SELECT * FROM staging_table s
WHERE s.id NOT EXISTS (
SELECT id FROM target_table
)
顺便说一句,在MySQL中,你可以使用这里提供的答案:warren:MySQL有insert ignore,postgres没有。在MySQL中,你可以使用这里提供的答案:warren:MySQL有insert ignore,postgres没有