mysql transaction processing

Create the table:
CREATE TABLE `role` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=40006 DEFAULT CHARSET=utf-8;


Create a stored procedure:

every time an insert is performed, a commit is made
DELIMITER $$
CREATE PROCEDURE load0(count INT UNSIGNED,iname varchar(50),iage INT)
BEGIN
DECLARE s INT UNSIGNED DEFAULT 1;
START TRANSACTION;
WHILE s<count DO
INSERT INTO role(name,age)VALUES(iname,iage);
SET s=s+1;
COMMIT;
END WHILE;
END$$
DELIMITER ;


With load0, there is no difference because mysql turns on auto-commit by default
DELIMITER $$
CREATE PROCEDURE load1(count INT UNSIGNED,iname varchar(50),iage INT)
BEGIN
DECLARE s INT UNSIGNED DEFAULT 1;
START TRANSACTION;
WHILE s<count DO
INSERT INTO role(name,age)VALUES(iname,iage);
SET s=s+1;
END WHILE;
END$$
DELIMITER ;


All inserts in one commit:
DELIMITER $$
CREATE PROCEDURE load2(count INT UNSIGNED,iname varchar(50),iage INT)
BEGIN
DECLARE s INT UNSIGNED DEFAULT 1;
START TRANSACTION;
WHILE s<count DO
INSERT INTO role(name,age)VALUES(iname,iage);
SET s=s+1;
END WHILE;
COMMIT;
END$$
DELIMITER ;


Execute the stored procedure:
call load0(10000,'jack',23);
[SQL]call load0(10000,'jack',23);

Affected Rows: 0
Time: 26.520s
truncate table role;
[SQL]call load1(10000,'jack',23);

Affected Rows: 1
Time: 0.483s
mysql> select * from information_schema.INNODB_TRX;
+--------+-----------+---------------------+-----------------------+------------------+------------+---------------------+-----------+---------------------+-------------------+-------------------+------------------+-----------------------+-----------------+-------------------+-------------------------+---------------------+-------------------+------------------------+----------------------------+---------------------------+---------------------------+------------------+----------------------------+
| trx_id | trx_state | trx_started         | trx_requested_lock_id | trx_wait_started | trx_weight | trx_mysql_thread_id | trx_query | trx_operation_state | trx_tables_in_use | trx_tables_locked | trx_lock_structs | trx_lock_memory_bytes | trx_rows_locked | trx_rows_modified | trx_concurrency_tickets | trx_isolation_level | trx_unique_checks | trx_foreign_key_checks | trx_last_foreign_key_error | trx_adaptive_hash_latched | trx_adaptive_hash_timeout | trx_is_read_only | trx_autocommit_non_locking |
+--------+-----------+---------------------+-----------------------+------------------+------------+---------------------+-----------+---------------------+-------------------+-------------------+------------------+-----------------------+-----------------+-------------------+-------------------------+---------------------+-------------------+------------------------+----------------------------+---------------------------+---------------------------+------------------+----------------------------+
| 27177  |[color=red] RUNNING[/color]   | [color=red]2016-07-29 15:53:43[/color] | NULL                  | NULL             |      10000 |                  21 | NULL      | NULL                |                 0 |                 0 |                1 |                   360 |               0 |              9999 |                       0 | REPEATABLE READ     |                 1 |                      1 | NULL                       |                         0 |                     10000 |                0 |                          0 |
+--------+-----------+---------------------+-----------------------+------------------+------------+---------------------+-----------+---------------------+-------------------+-------------------+------------------+-----------------------+-----------------+-------------------+-------------------------+---------------------+-------------------+------------------------+----------------------------+---------------------------+---------------------------+

My computer time is 15:56, and the transaction has not ended. Although the stored procedure is called in the foreground, the return is fast, but the background transaction is still running.
truncate table role;
call load2(10000,'jack',23);
[SQL]call load2(10000,'jack',23);

Affected Rows: 0
Time: 0.462s
select * from information_schema.INNODB_TRX;

Check that the transaction table is empty;

it can be analyzed from the above three that the execution time of load0 is longer. This is because mysql has to write the redo log redo for each submission (ib_logfile0, 1oad1 Although the time in the foreground is not long, the transaction Running in the background, load3 is very fast because it only does one redo log. Of course, we recommend not to submit transactions in a loop, but to submit all statements after execution. It is best not to open transactions in the stored procedure, because if A rollback occurs, and I don't know what went wrong. It's better to control it in the code
as follows:
try{
con.setautocommint(false)
con.commit ();
}
catch(SQLException e){
con.rollback();
}


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326692808&siteId=291194637