Postgres insert optimization

前端 未结 7 1623
一个人的身影
一个人的身影 2021-02-04 18:25

I have a script that generates tens of thousands of inserts into a postgres db through a custom ORM. As you can imagine, it\'s quite slow. This is used for development purpose

7条回答
  •  伪装坚强ぢ
    2021-02-04 19:07

    For inserts that number in the hundreds to thousands, batch them:

    begin;
    insert1 ...
    insert2 ...
    ...
    insert10k ... 
    commit;
    

    For inserts in the millions use copy:

    COPY test (ts) FROM stdin;
    2010-11-29 22:32:01.383741-07
    2010-11-29 22:32:01.737722-07
    ... 1Million rows
    \.
    

    Make sure any col used as an fk in another table is indexed if it's more than trivial in size in the other table.

提交回复
热议问题