In MySQL,LOAD DATA INFILE
Commands are not executed as a single transaction by default. This means that by default, the data is imported into the table step by step, and if an error is encountered during the import process, the imported data will not be automatically rolled back. This may cause some of the data to be inserted, thus breaking the consistency of the database.
Default behavior
-
Non-transactional storage engines (such as MyISAM): For storage engines that do not support transactions,
LOAD DATA INFILE
The operation will directly apply changes and there is no transaction protection. -
Transactional storage engines (such as InnoDB): Although InnoDB supports transactions,
LOAD DATA INFILE
By default, its operations are not wrapped in a transaction. Therefore, if the import fails in the middle, the data that was successfully imported before will be retained in the table and will not be automatically rolled back. -
By default,
LOAD DATA INFILE
Not executed in a transaction, even on a storage engine that supports transactions.
Example
Suppose you have a CSV file to be loaded withyour_table
InnoDB table, and want to ensure that the import operation is atomic:
LOAD DATA INFILE '/path/to/your/' INTO TABLE your_table FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'
To ensureLOAD DATA INFILE
Atomicity of operations and data consistency, especially when processing large amounts of data.
LOAD DATA INFILE
Commands are used to efficiently import data from external files into database tables. When you need to ensure the atomicity of the import operation (i.e., if all succeed or all fail, there will be no partial data being imported), you can use it in combination--single-transaction
Options and transaction controls to achieve this.
--single-transaction option
--single-transaction
is a very useful option, especially when dealing with large amounts of data. It ensures that during the import process, the entire operation is treated as a single transaction. This means:
- Atomicity: If any error occurs during the import process, all imported data will be rolled back to ensure the consistency of the data.
- Concurrent control: This option avoids long locking of tables and allows other sessions to continue querying and modifying data by setting the transaction isolation level to Repeatable Read (REPEATABLE READ) and creating a snapshot at the beginning.
Use --single-transaction and transaction control
althoughLOAD DATA INFILE
On InnoDB tables, it is transaction-safe by default, but explicitly uses transaction control statements (BEGIN
, COMMIT
, ROLLBACK
) can provide clearer control. However, in use--single-transaction
When you do not need to manually start the transaction, because this option itself implies the beginning of a transaction.
Example
Suppose you have a CSV file to be loaded withyour_table
InnoDB table, and you want to make sure that you can roll back in any error situation:
LOAD DATA INFILE '/path/to/your/' INTO TABLE your_table FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' -- There is no explicit one here BEGIN, because --single-transaction The beginning of the transaction is already implicit -- If an error occurs,The entire operation will roll back -- You don't need to be manually COMMIT or ROLLBACK --single-transaction;
In this example,--single-transaction
Ensures that the entire import process is executed as a transaction. If any errors occur, such as violation of uniqueness constraints or type conversion errors, the entire import operation will be rolled back and no part of the imported data will be left.
-
Notice:although
--single-transaction
Provides transaction support, but it does not change the basic transaction mechanism of MySQL. If you start a transaction manually (for example, byBEGIN
orSTART TRANSACTION
), then executeLOAD DATA INFILE
, you need to call it explicitlyCOMMIT
orROLLBACK
to end the transaction.
Things to note
-
Only available for transaction-enabled storage engines: Such as InnoDB. If you try to use MyISAM table
--single-transaction
, it will not have any effect because MyISAM does not support transactions. -
Performance impact:Although
--single-transaction
Provides the benefits of data consistency, but it may slow down imports slightly because it involves more logging and transaction management overhead. -
Compatible with other options: You can do with other
LOAD DATA INFILE
Use options together--single-transaction
,For exampleFIELDS TERMINATED BY
、LINES TERMINATED BY
etc. -
Error handling: Even if used
--single-transaction
, you should also check the import results to make sure no errors occur. You can confirm by viewing MySQL error logs or observing the returned messages in the SQL client.
In short, use--single-transaction
Options can be effectively guaranteedLOAD DATA INFILE
The atomicity of operations and data consistency are particularly suitable for batch data import scenarios that require high reliability.
This is the end of this article about the single-transaction option of mysql transaction. For more related contents of mysql single-transaction option, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!