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

ops/2024/10/19 1:41:00/

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/ops/122203.html

相关文章

基于auth2的单点登录原理理解

创作背景:基于auth2实现企业门户与业务系统的单点登录跳转。 架构组成:4A统一认证中心,门户系统,业务系统,用户; 实现目标:用户登录门户系统后,可通过点击业务系统菜单&#xff0c…

【每日一题 | 24.10.7】Fizz Buzz 经典问题

1. 题目2. 解题思路3. 代码实现(AC_Code) 个人主页:C_GUIQU 归属专栏:每日一题 1. 题目 Fizz Buzz 经典问题 2. 解题思路 【法1】逻辑硬解:按照题目逻辑分四种情况,用if else 判断即可。 【法2】switc…

封装el-upload组件,用于上传图片和视频

使用环境 vue3element-ui plus 需要根据后端返回结构修改的函数&#xff1a;onPreview onRemove onSuccess 组件使用 基本使用 源代码&#xff1a; <script setup> import AutoUploadFile from /components/auto-upload-file/index.vue function change(urls){console.…

python爬虫 - 初识requests模块

&#x1f308;个人主页&#xff1a;https://blog.csdn.net/2401_86688088?typeblog &#x1f525; 系列专栏&#xff1a;https://blog.csdn.net/2401_86688088/category_12797772.html 前言 requests 是一个用于发送 HTTP 请求的 Python 库&#xff0c;设计简单且功能强大&am…

Python知识点:如何使用AWS Greengrass与Python进行边缘计算

开篇&#xff0c;先说一个好消息&#xff0c;截止到2025年1月1日前&#xff0c;翻到文末找到我&#xff0c;赠送定制版的开题报告和任务书&#xff0c;先到先得&#xff01;过期不候&#xff01; 如何使用AWS Greengrass与Python进行边缘计算 边缘计算是云计算的有力补充&…

《重生到现代之从零开始的C语言生活》—— 内存函数

memcpy函数 和strncpy的用法基本一致 而不一样的是&#xff0c;size_t num是从源内存向后复制num个字节的数据到目标内存 但是如果有重叠的部分&#xff0c;就不能用memcpy了我们得用memmove memmove函数 **memcpy和memmove基本一致&#xff0c;**但是区别是如果源内存和目标…

js拼接html代码在线工具

具体请前往&#xff1a;在线Html转Js--将Html代码转成javascript动态拼接代码并保持原有格式

C# (.net6)实现Redis发布和订阅简单案例

概念&#xff1a; 在 .NET 6 中使用 Redis 的/订发布阅模式。发布/订阅&#xff08;Pub/Sub&#xff09;是 Redis 支持的一种消息传递模式&#xff0c;其中一个或多个发布者向一个或多个订阅者发送消息,Redis 客户端可以订阅任意数量的频道。 多个客户端可以订阅一个相同的频道…