Search code examples
javamysqldatabasesql-insertbatch-insert

Insert to database a lot of data with batch insert


I create a program that inserts to MySql database millions of values. I read about batch insert that will optimize my program and make it faster but when I tried to do it, it worked in the same way. Instead of inserting each value to the database I kept in a list each time 500 values and then insert them in one big loop like this:

for(int i=0;i<500;i++)
{
   insertData(list.get(i));
}

Then i remove all the values in the list and start collecting 500 values again. Shouldn't it work better?
My Insert code is:

public void insertToNameTable(String id,String name) throws SQLException
       {
           PreparedStatement ps=null;

            ps= conn.prepareStatement("INSERT INTO NameTable values(?,?,?)",user.getId(),user.getName());


            ps.setString(1,id);
            ps.setString(2,name);
            ps.setBoolean(3,false);
            ps.executeUpdate();

       }

I have some questions:
1.why it isn't work faster when i do batch insert?
2.how many values I should enter each time in order to make it faster?(500,1000,10000) the more values enter together is better?
3. is the way I insert the values to my database is the best way?


Solution

  • This is the efficient way for batch insert.

    Connection connection = new getConnection();
    Statement statement = connection.createStatement();
     
    for (String query : queries) {
        statement.addBatch(query);
    }
    statement.executeBatch();
    statement.close();
    connection.close();