回答編集履歴
1
サンプル追記
answer
CHANGED
@@ -1,4 +1,35 @@
|
|
1
1
|
[COPY FROM](https://www.postgresql.jp/document/10/html/sql-copy.html)で一時テーブル([TEMPORARY](https://www.postgresql.jp/document/10/html/sql-createtable.html))に取り込んで、そのテーブルから[INSERT ON CONFLICT](https://www.postgresql.jp/document/10/html/sql-insert.html)でUPSERTするのが高速だと思われます。
|
2
2
|
|
3
3
|
[PostgreSQLでCSVからUpdateをかける](https://qiita.com/kuwana/items/851125828a20d23cca1b)
|
4
|
-
[PostgreSQLのUPSERT方式の簡易性能比較](https://qiita.com/fujii_masao/items/6e9417f78dc0cbd0e13d)
|
4
|
+
[PostgreSQLのUPSERT方式の簡易性能比較](https://qiita.com/fujii_masao/items/6e9417f78dc0cbd0e13d)
|
5
|
+
|
6
|
+
追記
|
7
|
+
--
|
8
|
+
サンプル追記
|
9
|
+
```SQL
|
10
|
+
-- Create tmp table
|
11
|
+
create temp table tmp (id int, day date,value varchar(20)) on commit drop;
|
12
|
+
|
13
|
+
-- Import data from csv:
|
14
|
+
copy tmp from 'C:/test.csv' delimiter ',' csv header;
|
15
|
+
|
16
|
+
-- IDが重複しているもののうち最新の日付以外を削除
|
17
|
+
delete from tmp as target where day < (select max(day) from tmp where id=target.id);
|
18
|
+
|
19
|
+
-- Update table from tmp
|
20
|
+
update import_test_tbl set
|
21
|
+
id = tmp.id
|
22
|
+
, date = tmp.day
|
23
|
+
, value = tmp.value
|
24
|
+
from tmp
|
25
|
+
where import_test_tbl.id = tmp.id
|
26
|
+
;
|
27
|
+
-- Insert table from tmp
|
28
|
+
insert into import_test_tbl
|
29
|
+
select id, day, value
|
30
|
+
from tmp
|
31
|
+
where not exists( --キーが存在していないもの
|
32
|
+
select 1 from import_test_tbl where id = tmp.id
|
33
|
+
)
|
34
|
+
;
|
35
|
+
```
|