Check if records exists in a Postgres table

前端 未结 2 1025
北荒
北荒 2021-01-07 10:39

I have to read a CSV every 20 seconds. Each CSV contains min. of 500 to max. 60000 lines. I have to insert the data in a Postgres table, but before that I need to check if t

2条回答
  •  遥遥无期
    2021-01-07 11:04

    This should perform well:

    CREATE TEMP TABLE tmp AS SELECT * FROM tbl LIMIT 0 -- copy layout, but no data
    
    COPY tmp FROM '/absolute/path/to/file' FORMAT csv;
    
    INSERT INTO tbl
    SELECT tmp.*
    FROM   tmp
    LEFT   JOIN tbl USING (tbl_id)
    WHERE  tbl.tbl_id IS NULL;
    
    DROP TABLE tmp; -- else dropped at end of session automatically
    

    Closely related to this answer.

提交回复
热议问题