运行时间比较长,操作的数据比较多的事务我们称之为大事务。
例如,执行超过5s,10s,1min…
我这里按公司实际场景,规定了,每次操作/获取数据量应该少于5000条,结果集应该小于2M
建表sql
CREATE TABLE `apple_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`a` int(11) NOT NULL DEFAULT '0' COMMENT 'a',
`b` int(11) NOT NULL DEFAULT '0' COMMENT 'b',
`updated_ts` timestamp(6) NOT NULL DEFAULT '0000-00-00 00:00:00.000000' ON UPDATE CURRENT_TIMESTAMP(6),
`created_ts` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) COMMENT '创建时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
插入大量数据,模拟大事务
insert into apple_test(`a`, `b`) values(1,1);
insert into apple_test(a, b) select a,b from apple_test;
在反复执行的过程中,我们会发现,执行耗时越来越长。
这是因为每次插入的数据量越来越大。
例如,当我们多次执行,数量达到8388608时,
select count(*) from apple_test;
+----------+
| count(*) |
+----------+
| 8388608 |
+----------+
1 row in set (1.87 sec)
insert into apple_test(a, b) select a,b from test_test;
Query OK, 8388608 rows affected (1 min 20.68 sec)
Records: 8388608 Duplicates: 0 Warnings: 0
实际上,上面的插入语句,就是一个事务。
select a.trx_started,now(),(UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(a.trx_started)) diff_sec,b.id,b.user,b.host,b.db,d.SQL_TEXT from information_schema.innodb_trx a inner join information_schema.PROCESSLIST b on a.TRX_MYSQL_THREAD_ID=b.id and b.command in('Query', 'Sleep', 'Connect') inner join performance_schema.threads c ON b.id = c.PROCESSLIST_ID inner join performance_schema.events_statements_current d ON d.THREAD_ID = c.THREAD_ID;
+---------------------+---------------------+----------+--------+-------------+------+------+----------+
| trx_started | now() | diff_sec | id | user | host | db | SQL_TEXT |
+---------------------+---------------------+----------+--------+-------------+------+------+----------+
| 2021-08-07 22:12:20 | 2021-08-07 22:13:26 | 66 | 808786 | system user | | NULL | BEGIN |
+---------------------+---------------------+----------+--------+-------------+------+------+----------+
1 row in set (0.01 sec)