Bulk insert in Java using prepared statements batch update

前端 未结 4 2170
[愿得一人]
[愿得一人] 2020-11-30 00:02

I am trying to fill a resultSet in Java with about 50,000 rows of 10 columns and then inserting them into another table using the batchExecute method of P

4条回答
  •  Happy的楠姐
    2020-11-30 00:40

    The batch will be done in "all at once" - that's what you've asked it to do.

    50,000 seems a bit large to be attempting in one call. I would break it up into smaller chunks of 1,000, like this:

    final int BATCH_SIZE = 1000;
    for (int i = 0; i < DATA_SIZE; i++) {
      statement.setString(1, "a@a.com");
      statement.setLong(2, 1);
      statement.addBatch();
      if (i % BATCH_SIZE == BATCH_SIZE - 1)
        statement.executeBatch();
    }
    if (DATA_SIZE % BATCH_SIZE != 0)
      statement.executeBatch();
    

    50,000 rows shouldn't take more than a few seconds.

提交回复
热议问题