## 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 |
|---|---|
| SHOW TRANSACTION | zh-CN |
SHOW TRANSACTION
description
该语法用于查看指定 transaction id 或 label 的事务详情。
语法:
SHOW TRANSACTION
[FROM db_name]
WHERE
[id = transaction_id]
[label = label_name];
返回结果示例:
TransactionId: 4005
Label: insert_8d807d5d-bcdd-46eb-be6d-3fa87aa4952d
Coordinator: FE: 10.74.167.16
TransactionStatus: VISIBLE
LoadJobSourceType: INSERT_STREAMING
PrepareTime: 2020-01-09 14:59:07
CommitTime: 2020-01-09 14:59:09
FinishTime: 2020-01-09 14:59:09
Reason:
ErrorReplicasCount: 0
ListenerId: -1
TimeoutMs: 300000
- TransactionId:事务id
- Label:导入任务对应的 label
- Coordinator:负责事务协调的节点
- TransactionStatus:事务状态
- PREPARE:准备阶段
- COMMITTED:事务成功,但数据不可见
- VISIBLE:事务成功且数据可见
- ABORTED:事务失败
- LoadJobSourceType:导入任务的类型。
- PrepareTime:事务开始时间
- CommitTime:事务提交成功的时间
- FinishTime:数据可见的时间
- Reason:错误信息
- ErrorReplicasCount:有错误的副本数
- ListenerId:相关的导入作业的id
- TimeoutMs:事务超时时间,单位毫秒
example
-
查看 id 为 4005 的事务:
SHOW TRANSACTION WHERE ID=4005;
-
指定 db 中,查看 id 为 4005 的事务:
SHOW TRANSACTION FROM db WHERE ID=4005;
-
查看 label 为 label_name的事务: SHOW TRANSACTION WHERE LABEL = 'label_name';
keyword
SHOW, TRANSACTION