There are 3 ways to do transactions. All 3 of the following involve transactions.
SET autocommit = ON;
INSERT ... VALUES (1,2), (2,3), ...; -- This is one transaction
another INSERT/UPDATE/etc; -- This is another transaction
Or...
SET autocommit = OFF;
INSERT ... VALUES (1,2), (2,3), ...;
another INSERT/UPDATE/etc;
COMMIT; -- Terminate the ONE transaction (with 2 statements)
Or
BEGIN; -- aka START TRANSACTION;
INSERT ... VALUES (1,2), (2,3), ...;
another INSERT/UPDATE/etc;
COMMIT; -- Terminate the ONE transaction (with 2 statements)
If you are doing a bulk INSERT
, you probably want it to be a transaction unto itself, since you have already combined a lot of 1-row inserts into a "bulk" insert.
Speed:
- A transaction has some overhead.
- A statement has some overhead.
Therefore:
- Fastest: Bulk
INSERT
(one statement in one transaction)
- Medium: A bunch of 1-row
INSERT
statements between BEGIN
and COMMIT
is
- Slowest: 1-row
INSERTs
, each one being a separate transaction.
Tip: Don't use autocommit=0
because you might forget to issue the COMMIT
.