## Proposed changes Add transaction for the operation of insert. It will cost less time than non-transaction(it will cost 1/1000 time) when you want to insert a amount of rows. ### Syntax ``` BEGIN [ WITH LABEL label]; INSERT INTO table_name ... [COMMIT | ROLLBACK]; ``` ### Example commit a transaction: ``` begin; insert into Tbl values(11, 22, 33); commit; ``` rollback a transaction: ``` begin; insert into Tbl values(11, 22, 33); rollback; ``` commit a transaction with label: ``` begin with label test_label; insert into Tbl values(11, 22, 33); commit; ``` ### Description ``` begin: begin a transaction, the next insert will execute in the transaction until commit/rollback; commit: commit the transaction, the data in the transaction will be inserted into the table; rollback: abort the transaction, nothing will be inserted into the table; ``` ### The main realization principle: ``` 1. begin a transaction in the session. next sql is executed in the transaction; 2. insert sql will be parser and get the database name and table name, they will be used to select a be and create a pipe to accept data; 3. all inserted values will be sent to the be and write into the pipe; 4. a thread will get the data from the pipe, then write them to disk; 5. commit will complete this transaction and make these data visible; 6. rollback will abort this transaction ``` ### Some restrictions on the use of update syntax. 1. Only ```insert``` can be called in a transaction. 2. If something error happened, ```commit``` will not succeed, it will ```rollback``` directly; 3. By default, if part of insert in the transaction is invalid, ```commit``` will only insert the other correct data into the table. 4. If you need ```commit``` return failed when any insert in the transaction is invalid, you need execute ```set enable_insert_strict = true``` before ```begin```.
2.4 KiB
2.4 KiB
title, language
| title | language |
|---|---|
| BEGIN | en |
BEGIN, COMMIT, ROLLBACK
Description
Syntax
BEGIN;
INSERT INTO table_name ...
COMMIT;
BEGIN [ WITH LABEL label];
INSERT INTO table_name ...
ROLLBACK;
Parameters
label: the label for this transaction, if you need to set it to a string.
Note
A transaction can only be used on insert, nor update or delete. You can check the state of this transaction by SHOW TRANSACTION WHERE LABEL = 'label'
example
- Begin a transaction without a label, then commit it
BEGIN
INSERT INTO test VALUES (1, 2);
INSERT INTO test (c1, c2) VALUES (1, 2);
INSERT INTO test (c1, c2) VALUES (1, DEFAULT);
INSERT INTO test (c1) VALUES (1);
COMMIT:
All the data in the sql between begin and commit will be inserted into the table.
- Begin a transaction without a label, then abort it
BEGIN
INSERT INTO test VALUES (1, 2);
INSERT INTO test (c1, c2) VALUES (1, 2);
INSERT INTO test (c1, c2) VALUES (1, DEFAULT);
INSERT INTO test (c1) VALUES (1);
ROLLBACK:
All the data in the sql between begin and rollback will be aborted, nothing will be inserted into the table.
- Begin a transaction with a label, then commit it
BEGIN WITH LABEL test_label1
INSERT INTO test VALUES (1, 2);
INSERT INTO test (c1, c2) VALUES (1, 2);
INSERT INTO test (c1, c2) VALUES (1, DEFAULT);
INSERT INTO test (c1) VALUES (1);
COMMIT:
All the data in the sql between begin and commit will be inserted into the table.
The label of test_label1 will be set to mark this transaction. You can check this transaction by SHOW TRANSACTION WHERE LABEL = 'test_label1'.
keyword
BEGIN, COMMIT, ROLLBACK