mysql> show create table t4\G
*************************** 1. row ***************************Table: t4
Create Table: CREATE TABLE `t4` (`id` int(11) NOT NULL,`id1` int(11) DEFAULT NULL,`id2` int(11) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)mysql> show create table t5\G
*************************** 1. row ***************************Table: t5
Create Table: CREATE TABLE `t5` (`id` int(11) NOT NULL,`id1` int(11) DEFAULT NULL,`id2` int(11) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)mysql> show variables like 'innodb_locks_unsafe_for_binlog';
+--------------------------------+-------+
| Variable_name | Value |
+--------------------------------+-------+
| innodb_locks_unsafe_for_binlog | OFF |
+--------------------------------+-------+
1 row in set (0.00 sec)mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)mysql> select * from t4;
+----+------+------+
| id | id1 | id2 |
+----+------+------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 3 | 3 |
| 4 | 4 | 4 |
| 5 | 5 | 5 |
+----+------+------+
5 rows in set (0.00 sec)mysql> select * from t5;
Empty set (0.00 sec)mysql> insert into t5 select * from t4 where id2=3;
Query OK, 1 row affected (0.34 sec)
Records: 1 Duplicates: 0 Warnings: 0mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> update t4 set id1=33 where id2=3; //被hang住mysql> commit;
Query OK, 0 rows affected (0.01 sec)Query OK, 1 row affected (9.15 sec) //事物一提交后,update操作執(zhí)行成功 Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from t5;
+----+------+------+
| id | id1 | id2 |
+----+------+------+
| 3 | 3 | 3 |
+----+------+------+
1 row in set (0.00 sec)mysql> select * from t4 where id2=3;
+----+------+------+
| id | id1 | id2 |
+----+------+------+
| 3 | 3 | 3 |
+----+------+------+
1 row in set (0.00 sec)
? 示例二:innodb_locks_unsafe_for_binlog=on的情況下
mysql> show variables like 'innodb_locks_unsafe_for_binlog';
+--------------------------------+-------+
| Variable_name | Value |
+--------------------------------+-------+
| innodb_locks_unsafe_for_binlog | ON |
+--------------------------------+-------+
1 row in set (0.35 sec)mysql> select * from t4;
+----+------+------+
| id | id1 | id2 |
+----+------+------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 3 | 3 |
| 4 | 4 | 4 |
| 5 | 5 | 5 |
+----+------+------+
5 rows in set (0.00 sec)mysql> select * from t5;
Empty set (0.00 sec)mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)mysql> insert into t5 select * from t4 where id2=3;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0mysql> select * from t5;
+----+------+------+
| id | id1 | id2 |
+----+------+------+
| 3 | 3 | 3 |
+----+------+------+
1 row in set (0.00 sec)mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> update t4 set id2=333 where id2=3; //事物一未提交的情況下,直接更新成功,不會有阻塞Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from t4 where id=3; +----+------+------+ | id | id1 | id2 | +----+------+------+ | 3 | 3 | 333 | +----+------+------+ 1 row in set (0.00 sec) mysql> select * from t5; Empty set (0.00 sec) mysql> commit; //事物二先提交 Query OK, 0 rows affected (0.00 sec) mysql> commit; //事物一后提交
Query OK, 0 rows affected (0.01 sec) mysql> select * from t5;
+----+------+------+
| id | id1 | id2 |
+----+------+------+
| 3 | 3 | 3 |
+----+------+------+
1 row in set (0.00 sec) mysql> select * from t4 where id=3;
+----+------+------+
| id | id1 | id2 |
+----+------+------+
| 3 | 3 | 333 |
+----+------+------+
1 row in set (0.00 sec)
? 查看binlog日志:
BEGIN
/*!*/;
# at 565
#180926 16:27:06 server id 1013306 end_log_pos 609 Table_map: `test1`.`t4` mapped to number 125
# at 609
#180926 16:27:06 server id 1013306 end_log_pos 667 Update_rows: table id 125 flags: STMT_END_FBINLOG '
muurWxM6dg8ALAAAAGECAAAAAH0AAAAAAAEABXRlc3QxAAJ0NAADAwMDAAY=
muurWx86dg8AOgAAAJsCAAAAAH0AAAAAAAEAAgAD///4AwAAAAMAAAADAAAA+AMAAAADAAAATQEA
AA==
'/*!*/;
### UPDATE `test1`.`t4` //事物二先提交,所以binlog日志中先記錄對t4的更新操作
### WHERE
### @1=3 /* INT meta=0 nullable=0 is_null=0 */
### @2=3 /* INT meta=0 nullable=1 is_null=0 */
### @3=3 /* INT meta=0 nullable=1 is_null=0 */
### SET
### @1=3 /* INT meta=0 nullable=0 is_null=0 */
### @2=3 /* INT meta=0 nullable=1 is_null=0 */
### @3=333 /* INT meta=0 nullable=1 is_null=0 */
# at 667
#180926 16:28:38 server id 1013306 end_log_pos 694 Xid = 82
COMMIT/*!*/;
# at 694
#180926 16:28:52 server id 1013306 end_log_pos 755 GTID last_committed=1 sequence_number=3 rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'f2754eef-6a6e-11e8-8b99-000c2971d3ea:1451'/*!*/;
# at 755
#180926 16:25:57 server id 1013306 end_log_pos 824 Query thread_id=2 exec_time=0 error_code=0
SET TIMESTAMP=1537993557/*!*/;
BEGIN
/*!*/;
# at 824
#180926 16:25:57 server id 1013306 end_log_pos 868 Table_map: `test1`.`t5` mapped to number 126
# at 868
#180926 16:25:57 server id 1013306 end_log_pos 912 Write_rows: table id 126 flags: STMT_END_FBINLOG '
VeurWxM6dg8ALAAAAGQDAAAAAH4AAAAAAAEABXRlc3QxAAJ0NQADAwMDAAY=
VeurWx46dg8ALAAAAJADAAAAAH4AAAAAAAEAAgAD//gDAAAAAwAAAAMAAAA=
'/*!*/;
### INSERT INTO `test1`.`t5` //事物一后提交所以對于insert ... select 操作在binlog中后記錄,但是set的記錄仍然是事物二為修改之前的值
### SET
### @1=3 /* INT meta=0 nullable=0 is_null=0 */
### @2=3 /* INT meta=0 nullable=1 is_null=0 */
### @3=3 /* INT meta=0 nullable=1 is_null=0 */
# at 912
#180926 16:28:52 server id 1013306 end_log_pos 939 Xid = 78
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;