MySQL Gtid 临时表限制

  1. 不能在事务中创建临时表,可以在事务外创建,但要求 autocommit = 1。
  2. 不可以将临时表放在触发器和函数中
  3. 存储过程是可以的

验证

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> create temporary table tmp_t2 (id int);
ERROR 1787 (HY000): Statement violates GTID consistency: CREATE TEMPORARY TABLE and DROP TEMPORARY TABLE can only be executed outside transactional context. These statements are also not allowed in a function or trigger because functions and triggers are also considered to be multi-statement transactions.
mysql> select @@autocommit;
+--------------+
| @@autocommit |
+--------------+
| 1 |
+--------------+
1 row in set (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> create temporary table tmp_t2 (id int);
Query OK, 0 rows affected (0.00 sec)

mysql> drop table tmp_t2;
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter //

mysql> create procedure p2() begin create temporary table tmp_01 select * from t3; create temporary table tmp_02 select * from t3; end//
Query OK, 0 rows affected (0.01 sec)

mysql> delimiter ;
mysql> call p2();
Query OK, 1 row affected (0.01 sec)

mysql> select * from tmp_01;
+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id | c |
+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 1 | {"logins": 2, "login_at": "2017-01-25T05:22:27.246Z", "created_at": "1970-01-01T00:00:42.544Z", "mobile": 18298765562, "logout_at": "2017-01-25T06:33:53.198Z", "type": "company", "nickname": "u6210u90fdu6c47u9a8fu8054u90a6u79d1u6280u6709u9650u79d1u6280", "email": "ab112@sina.com", "mobile_verified": true} |
+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from tmp_02;
+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id | c |
+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 1 | {"logins": 2, "login_at": "2017-01-25T05:22:27.246Z", "created_at": "1970-01-01T00:00:42.544Z", "mobile": 18298765562, "logout_at": "2017-01-25T06:33:53.198Z", "type": "company", "nickname": "u6210u90fdu6c47u9a8fu8054u90a6u79d1u6280u6709u9650u79d1u6280", "email": "ab112@sina.com", "mobile_verified": true} |
+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

第三方程序执行的时候需要注意使用自动提交方式。