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) ?
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 INSERT
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With