Why are batch inserts/updates faster? How do batch updates work?

后端 未结 4 855
长情又很酷
长情又很酷 2020-11-30 21:11

Why are batch inserts faster? Is it because the connection and setup overhead for inserting a single row is the same for a set of rows? What other factors make batch inserts

4条回答
  •  误落风尘
    2020-11-30 22:08

    I was looking for an answer on the same subject, about "bulk/batch" update. People often describe the problem by comparing it with insert clause with multiple value sets (the "bulk" part).

    INSERT INTO mytable (mykey, mytext, myint)
    VALUES 
      (1, 'text1', 11),
      (2, 'text2', 22),
      ...
    

    Clear answer was still avoiding me, but I found the solution here: http://www.postgresql.org/docs/9.1/static/sql-values.html

    To make it clear:

    UPDATE mytable
    SET 
      mytext = myvalues.mytext,
      myint = myvalues.myint
    FROM (
      VALUES
        (1, 'textA', 99),
        (2, 'textB', 88),
        ...
    ) AS myvalues (mykey, mytext, myint)
    WHERE mytable.mykey = myvalues.mykey
    

    It has the same property of being "bulk" aka containing alot of data with one statement.

提交回复
热议问题