MySQL 8.0 新特性之自增变量持久化

server/2024/10/11 10:59:31/

MySQL 8.0 新特性之自增变量持久化

文章目录

  • MySQL 8.0 新特性之自增变量持久化
    • MySQL 5.7 vs 8.0 测试对比
      • MySQL 5.7
      • MySQL 8.0
    • 参考资料

MySQL 8.0 中支持自增变量持久化,实际也是解决之前版本中存在的自增主键重启重置的 BUG 问题( BUG #199:Innodb autoincrement stats los on restart) 。

MySQL 8.0 开始,当前最大的自增计数器每当发生变化,值会被写入 redo log 中,并在每个检查点时保存在 engine-private system table 中,对 AUTO_INCREMENT 值进行持久化,MySQL 重启后,该值也不会改变。

MySQL 5.7 vs 8.0 测试对比

MySQL 5.7

# MySQL 5.7
mysql> select * from t2;
+----+------+------+------+
| id | c1   | c2   | c3   |
+----+------+------+------+
|  1 |    1 |   11 |   22 |
|  2 |    2 |   22 |   33 |
|  3 |    2 |   22 |   25 |
|  4 |    2 |   11 |   22 |
+----+------+------+------+
4 rows in set (0.01 sec)# 对应自增值(AUTO_INCREMENT)为 5
mysql> show create table t2\G
*************************** 1. row ***************************Table: t2
Create Table: CREATE TABLE `t2` (`id` int(11) NOT NULL AUTO_INCREMENT,`c1` int(11) DEFAULT NULL,`c2` int(11) DEFAULT NULL,`c3` int(11) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)# 修改主键值
mysql> update t2 set id=5 where id=4;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from t2;
+----+------+------+------+
| id | c1   | c2   | c3   |
+----+------+------+------+
|  1 |    1 |   11 |   22 |
|  2 |    2 |   22 |   33 |
|  3 |    2 |   22 |   25 |
|  5 |    2 |   11 |   22 |
+----+------+------+------+
4 rows in set (0.00 sec)# 自增值(AUTO_INCREMENT)未发生变化
mysql> show create table t2\G
*************************** 1. row ***************************Table: t2
Create Table: CREATE TABLE `t2` (`id` int(11) NOT NULL AUTO_INCREMENT,`c1` int(11) DEFAULT NULL,`c2` int(11) DEFAULT NULL,`c3` int(11) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)# 插入数据,此时报错“主键冲突”
mysql> insert into t2 (c1,c2,c3) values (3,11,12);
ERROR 1062 (23000): Duplicate entry '5' for key 'PRIMARY'# 再次查看自增值(AUTO_INCREMENT)已变为 6
mysql> show create table t2\G
*************************** 1. row ***************************Table: t2
Create Table: CREATE TABLE `t2` (`id` int(11) NOT NULL AUTO_INCREMENT,`c1` int(11) DEFAULT NULL,`c2` int(11) DEFAULT NULL,`c3` int(11) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)# 测试再次插入数据,此时正常
mysql> insert into t2 (c1,c2,c3) values (3,11,12);
Query OK, 1 row affected (0.00 sec)
mysql> select * from t2;
+----+------+------+------+
| id | c1   | c2   | c3   |
+----+------+------+------+
|  1 |    1 |   11 |   22 |
|  2 |    2 |   22 |   33 |
|  3 |    2 |   22 |   25 |
|  5 |    2 |   11 |   22 |
|  6 |    3 |   11 |   12 |
+----+------+------+------+
5 rows in set (0.00 sec)# 自增值(AUTO_INCREMENT)此时变为 7
mysql> show create table t2\G
*************************** 1. row ***************************Table: t2
Create Table: CREATE TABLE `t2` (`id` int(11) NOT NULL AUTO_INCREMENT,`c1` int(11) DEFAULT NULL,`c2` int(11) DEFAULT NULL,`c3` int(11) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)

MySQL 8.0

# MySQL 8.0
mysql> select * from t2;
+----+------+------+------+
| id | c1   | c2   | c3   |
+----+------+------+------+
|  1 |    1 |   11 |   22 |
|  2 |    2 |   22 |   33 |
|  3 |    2 |   22 |   25 |
|  4 |    2 |   11 |   22 |
+----+------+------+------+
4 rows in set (0.01 sec)# 对应自增值(AUTO_INCREMENT)为 5
mysql> show create table t2\G
*************************** 1. row ***************************Table: t2
Create Table: CREATE TABLE `t2` (`id` int NOT NULL AUTO_INCREMENT,`c1` int DEFAULT NULL,`c2` int DEFAULT NULL,`c3` int DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.01 sec)# 修改主键值
mysql> update t2 set id=5 where id=4;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from t2;
+----+------+------+------+
| id | c1   | c2   | c3   |
+----+------+------+------+
|  1 |    1 |   11 |   22 |
|  2 |    2 |   22 |   33 |
|  3 |    2 |   22 |   25 |
|  5 |    2 |   11 |   22 |
+----+------+------+------+
4 rows in set (0.00 sec)# 查看自增值(AUTO_INCREMENT)已变为 6
mysql> show create table t2\G
*************************** 1. row ***************************Table: t2
Create Table: CREATE TABLE `t2` (`id` int NOT NULL AUTO_INCREMENT,`c1` int DEFAULT NULL,`c2` int DEFAULT NULL,`c3` int DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)# 再次修改主键值
mysql> update t2 set id=8 where id=5;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from t2;
+----+------+------+------+
| id | c1   | c2   | c3   |
+----+------+------+------+
|  1 |    1 |   11 |   22 |
|  2 |    2 |   22 |   33 |
|  3 |    2 |   22 |   25 |
|  8 |    2 |   11 |   22 |
+----+------+------+------+
4 rows in set (0.00 sec)# 查看自增值(AUTO_INCREMENT)已变为 9
mysql> show create table t2\G
*************************** 1. row ***************************Table: t2
Create Table: CREATE TABLE `t2` (`id` int NOT NULL AUTO_INCREMENT,`c1` int DEFAULT NULL,`c2` int DEFAULT NULL,`c3` int DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)# 插入数据正常
mysql> insert into t2 (c1,c2,c3) values (3,11,12);
Query OK, 1 row affected (0.00 sec)
mysql> select * from t2;
+----+------+------+------+
| id | c1   | c2   | c3   |
+----+------+------+------+
|  1 |    1 |   11 |   22 |
|  2 |    2 |   22 |   33 |
|  3 |    2 |   22 |   25 |
|  8 |    2 |   11 |   22 |
|  9 |    3 |   11 |   12 |
+----+------+------+------+
5 rows in set (0.00 sec)# 查看自增值(AUTO_INCREMENT)正常变为 10
mysql> show create table t2\G
*************************** 1. row ***************************Table: t2
Create Table: CREATE TABLE `t2` (`id` int NOT NULL AUTO_INCREMENT,`c1` int DEFAULT NULL,`c2` int DEFAULT NULL,`c3` int DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.01 sec)

参考资料

【1】AUTO_INCREMENT Handling in InnoDB


http://www.ppmy.cn/server/130091.html

相关文章

蓝桥杯:求平均年龄

#include<stdio.h> int main() { int num 0; float age 0,sum0; printf("请输入总人数: "); scanf_s("%d" ,& num); for (int i1; i <num;i) { scanf_s("%f", &age); sum age…

高含金量WebGIS学习教程?

智慧校园——适合0基础入门 智慧交通——适合0基础入门 VUE-适合前端进阶 Mapbox项目开发实例 Openlayers零基础入门 智慧机场——适合有前端基础 threejs三维开发入门 三维进阶&#xff1a;cesium零基础入门教程 面试讲解&#xff1a;剖析地信大厂技术面试真题&#x…

wasm在云原生领域的运用

Wasm 最初是以浏览器安全沙盒为目的开发的&#xff0c;发展到目前为止&#xff0c;WebAssembly 已经成为一个用于云原生软件组件的高性能、跨平台和多语言软件沙箱环境&#xff0c;Wasm 轻量级容器也非常适合作为下一代无服务器平台运行时 Envoy 社区在 Envoy 中嵌入了 WASM 虚…

esp8266 at指令链接wifi时一直connect disconnest

那是你的连接wifi的名字密码有误或者热点有问题&#xff0c;看看热点是不是把设备拉入黑名单或者设置为5G或者连了校园网或者设置了最多链接设备

Linux——echo-tail-重定向符

echo命令 类似printf 输出 反引号 重定向符 > 和 >> > 覆盖 >> 追加 tail命令 查看文件尾部内容&#xff0c;追踪文件最新更改 tail -num 从尾部往上读num行&#xff0c;默认10行 tail -f 持续跟踪

Cherno游戏引擎笔记(61~72)

---------------一些维护和更改------------- 》》》》 Made Win-GenProjects.bat work from every directory 代码更改&#xff1a; echo off->pushd ..\->pushd %~dp0\..\call vendor\bin\premake\premake5.exe vs2019popdPAUSE 为什么要做这样的更改&#xff1f; …

moectf-Web题解

1、弗拉格之地的入口 2、垫刀之路01: MoeCTF&#xff1f;启动&#xff01; 3、ez_http 4、ProveYourLove 5、弗拉格之地的挑战 6、ImageCloud前置 7、垫刀之路02: 普通的文件上传 8、垫刀之路03: 这是一个图床 9、垫刀之路05: 登陆网站 10、垫刀之路06: pop base mini …

笔记本电脑重启输入密码后黑屏

型号&#xff1a;Dell G3 3590 系统&#xff1a;Win10 背景 原本想安装火绒清一清缓存之类的垃圾文件的&#xff0c;安装完火绒之后点击垃圾清理&#xff0c;提示需要更新工具&#xff0c;ok,更新&#xff0c;进度到50%就不动了&#xff0c;然后火绒通知我 发现危险信息&…