Mysql – How does autocommit=off affects bulk inserts performance in thesql using innodb

MySQLtransaction

I know that turning off autocommit can improve bulk insert performance a lot according to:

Is it better to use AUTOCOMMIT = 0

And I have made experiments to confirm that conclusion. But what I want to know is, why turning off autocommit can improve bulk inserts? And what happens internally in inno db when a transaction is commited?

Best Answer

If autocommit is turned off, you can always do a ROLLBACK to get back to the state the database was in before you started doing something. COMMIT makes sure all changes to the database are written permanently, doing this for every INSERT takes more time than doing this for a lot of INSERTs, because not only the records needs to be inserted, also the indexes needs to be updated. Doing this last piece for a lot of records is more efficient than doing this for 1 record at a time.