当前位置: 首页 > article >正文

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

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.kler.cn/news/335705.html

相关文章:

  • GOME数据IDL处理
  • 大数据新视界 --大数据大厂之 DataFusion:超越传统的大数据集成与处理创新工具
  • FP7209: 用于紫外线消毒灯的 升压LED恒流驱动芯片
  • GO语言工程构建示例-mac和linux适用
  • 【动态规划-最长公共子序列(LCS)】【hard】力扣1458. 两个子序列的最大点积
  • Windows搭建RTMP服务器
  • SpringBoot:让开发更加简单
  • 爬虫案例——爬取情话网数据
  • 程序bug的修复
  • C# 文件与文件夹操作指南:深入探索流、文件流及文件夹管理
  • ReGCL Rethinking Message Passingin Graph Contrastive Learning
  • Python知识点:如何使用Edge Impulse与Python进行机器学习模型部署
  • 实现mnist手写数字识别
  • Elasticsearch——数据聚合、数据同步与集群搭建
  • vscode提交修改Failed to connect to github.com port 443: Timed out
  • 研究生系统化入门教程(四)【机器学习】分类算法:决策树(信息熵,信息增益);集成学习方法之随机森林:估计器的工作流程是什么?为何采用BootStrap抽样?
  • Redis:cpp.redis++通用接口
  • Prometheus+Grafana备忘
  • T-Sql分支判断语句
  • 排版套料系统设计说明