Difference between INSERT and COPY

元气小坏坏 提交于 2020-02-01 01:06:30

问题


As per the documentation,

Loading large number of rows using COPY is always faster than using INSERT, even if PREPARE is used and multiple insertions are batched into a single transaction.

Why COPY is faster than INSERT (multiple insertion are batched into single transaction) ?


回答1:


Quite a number of reasons, actually, but the main ones are:

  • Typically, client applications wait for confirmation of one INSERT's success before sending the next. So there's a round-trip delay for each INSERT, scheduling delays, etc. (PgJDBC supports pipelineing INSERTs in batches, but I'm not aware of any other clients that do).

  • Each INSERT has to go through the whole executor. Use of a prepared statement bypasses the need to run the parser, rewriter and planner, but there's still executor state to set up and tear down for each row. COPY does some setup once, and has an extremely low overhead for each row, especially where no triggers are involved.

The first point is the most significant. It's all about network round-trips and rescheduling delays.




回答2:


This is because COPY is a single statement, while each INSERT is a separate statement. Since each single statement is normally subject to logging (manual), even inside a unique transaction, the use of many INSERT is slower than the use of a single COPY.



来源:https://stackoverflow.com/questions/32043420/difference-between-insert-and-copy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!