Insert multiple rows using one statement vs batch insert in mysql

时间:2019-04-16 22:32:58

标签: java mysql

Which one will give me better performance?

  1. Use Java simply loop the value and added to the sql string and execute the statement at once? Note that PreparedStatement is also used.

    INSERT INTO tbl ( c1 , c2 , c3 ) VALUES ('r1c1', 'r1c2', 'r1c3'), ('r2c1', 'r2c2', 'r2c3'), ('r3c1', 'r3c2', 'r3c3')

  2. Use the batch execution as below.

    String SQL_INSERT = "INSERT INTO tbl (c1, c2, c3) VALUES (?, ?, ?);";

    try (
            Connection connection = database.getConnection();
            PreparedStatement statement = connection.prepareStatement(SQL_INSERT);
        ) {
    
        int i = 0;
        for (Entity entity : entities) {
            statement.setString(1, entity.getSomeProperty());
            // ...
    
            statement.addBatch();
            i++;
    
            if (i % 1000 == 0 || i == entities.size()) {
                statement.executeBatch(); // Execute every 1000 items.
            }
        }
    }
    

1 个答案:

答案 0 :(得分:1)

I did a presentation a few years ago I called Load Data Fast!. I compared many different methods of inserting data as fast as possible, and benchmarked them.

LOAD DATA INFILE was much faster than any other method.

But there are other factors that affect the speed, like the type of data, and the type of hardware, and perhaps the load on the system from other concurrent clients of the database. The results I got only describe what the performance is on a Macbook Pro.

Ultimately, you need to test your specific case on your server to get the most accurate answer.

This is what being a software engineer is about. You don't always get the answers spoon-fed to you. You have to do some testing to confirm them.