当前位置:网站首页>Interpretation of new features | MySQL 8.0 GIPK invisible primary key
Interpretation of new features | MySQL 8.0 GIPK invisible primary key
2022-08-02 12:32:00 【Aikesheng open source community】
作者:杨奇龙
网名“北在南方”,资深 DBA,主要负责数据库架构设计和运维平台开发工作,擅长数据库性能调优、故障诊断.
本文来源:原创投稿
*爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源.
一、前言
作为 MySQL DBA ,I believe everyone has experienced in copy mode,如果没有主键,遇到 load data ,大事务,ddl When there is a large number of table data row scan behavior,It will bring serious master-slave delay,It brings hidden dangers to database stability and data consistency.
MySQL 8.0.30 已于近日 GA ,The new version provides us with a surprising feature -(Generated Invisible Primary Keys)简称 GIPK .An overview is: 当开启GIPK模式后,MySQL The primary key will not be displayedInnoDBAn invisible primary key is automatically generated on the table.
For already using the cloudRDS的朋友,May enjoy the cloud very early RDS MySQL 提供的隐式主键特性.But for enterprises that build their own databases,GIPK Still a relatively expected feature,(Of course, having it and using it are two different things!)
长话短说,This article is based on actual test cases to school 如何使用 GIPK .
二、实践出真知
2.1 开启
GIPK 由参数sql_generate_invisible_primary_key
控制,默认关闭,表示禁用,如果需要使用该特性,need to be explicitly enabled.
master [localhost:22031]> show variables like 'sql_generate_invisible_primary_key';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| sql_generate_invisible_primary_key | OFF |
+------------------------------------+-------+
1 row in set (0.00 sec)
master [localhost:22031]> set sql_generate_invisible_primary_key=on;
Query OK, 0 rows affected (0.00 sec)
master [localhost:22031]> show variables like 'sql_generate_invisible_primary_key';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| sql_generate_invisible_primary_key | ON |
+------------------------------------+-------+
1 row in set (0.00 sec)
2.2 测试
We create two non-primary key tables with this feature turned off and turned on, respectively:
master [localhost:22031]> create table t1(id int ,c1 int);
Query OK, 0 rows affected (0.00 sec)
master [localhost:22031] {msandbox} (test) > show create table t1 \G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int DEFAULT NULL,
`c1` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
开启 GIPK And create table without primary key t3 .
master [localhost:22031]> set sql_generate_invisible_primary_key=on;
Query OK, 0 rows affected (0.00 sec)
master [localhost:22031] {msandbox} (test) > show variables like 'sql_generate_invisible_primary_key';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| sql_generate_invisible_primary_key | ON |
+------------------------------------+-------+
1 row in set (0.00 sec)
master [localhost:22031]> create table t3(id int ,c1 int);
Query OK, 0 rows affected (0.01 sec)
master [localhost:22031]>
master [localhost:22031]> show create table t3 \G
*************************** 1. row ***************************
Table: t3
Create Table: CREATE TABLE `t3` (
`my_row_id` bigint unsigned NOT NULL AUTO_INCREMENT /*!80023 INVISIBLE */,
`id` int DEFAULT NULL,
`c1` int DEFAULT NULL,
PRIMARY KEY (`my_row_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
我们可以通过 show create table 发现 t3 的表结构,出现名为 my_row_id
The invisible primary key of .Insert data into two tables to see the difference:
master [localhost:22031]> insert into t1 values(1,1),(2,2),(3,3);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
master [localhost:22031]> select * from t1;
+------+------+
| id | c1 |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)
master [localhost:22031]> insert into t3 values(1,1),(2,2),(3,3);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
master [localhost:22031]> select * from t3;
+------+------+
| id | c1 |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)
直接通过 select * from table 查询时,t3 and normal tablest1无差异. 因为 GIPK It is implemented based on invisible columns,If we explicitly specify access my_row_id
,Then you can view the hidden primary key my_row_id
.
master [localhost:22031]> select my_row_id,id,c1 from t3;
+-----------+------+------+
| my_row_id | id | c1 |
+-----------+------+------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 3 | 3 |
+-----------+------+------+
3 rows in set (0.00 sec)
总的来说,From the point of view of business programs accessing the database,开启 GIPK 对业务是透明的.
2.3 关于 DDL
当开启 GIPK 特性时,MySQL The generated primary key cannot be changed,只能在 VISIBLE 和 INVISIBLE 之间进行切换.比如:
使 GIPK Primary key is visible: alter table TALBE_NAME
alter column my_row_id
set visible;
master [localhost:22031]> alter table t3 alter column my_row_id set visible;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
master [localhost:22031]> show create table t3 \G
*************************** 1. row ***************************
Table: t3
Create Table: CREATE TABLE `t3` (
`my_row_id` bigint unsigned NOT NULL AUTO_INCREMENT, ### explicitly visible
`id` int DEFAULT NULL,
`c1` int DEFAULT NULL,
PRIMARY KEY (`my_row_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
### And can be directly queried
master [localhost:22031]> select * from t3;
+-----------+------+------+
| my_row_id | id | c1 |
+-----------+------+------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 3 | 3 |
+-----------+------+------+
3 rows in set (0.00 sec)
Turn off visibility : alter table TABLE_NAME
alter column my_row_id
set invisible;
master [localhost:22031]> alter table t3 alter column my_row_id set invisible;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
master [localhost:22031]> show create table t3 \G
*************************** 1. row ***************************
Table: t3
Create Table: CREATE TABLE `t3` (
`my_row_id` bigint unsigned NOT NULL AUTO_INCREMENT /*!80023 INVISIBLE */,
`id` int DEFAULT NULL,
`c1` int DEFAULT NULL,
PRIMARY KEY (`my_row_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
## 再次通过select * Query is not visible my_row_id
master [localhost:22031]> select * from t3;
+------+------+
| id | c1 |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)
The other is to open GIPK 之后 ,my_row_id
是系统关键字,When we create a table without a primary key,Cannot contain named my_row_id
的字段 .
master [localhost:22031]> create table t6(my_row_id int not null ,c1 int);
ERROR 4108 (HY000): Failed to generate invisible primary key. Column 'my_row_id' already exists.
当然如果 MySQL Allows creation of include names my_row_id
The primary key of the table :
master [localhost:22031]> create table t5(my_row_id int not null auto_increment primary key ,c1 int);
Query OK, 0 rows affected (0.01 sec)
当开启 GIPK 模式时,If the invisible primary key cannot be deleted directly.A new primary key must be explicitly added and then deleted GIPK
master [localhost:22031]> alter table t3 drop PRIMARY KEY;
ERROR 1235 (42000): This version of MySQL doesn't yet support 'existing primary key drop without adding a new primary key. In @@sql_generate_invisible_primary_key=ON mode table should have a primary key. Please add a new primary key to be able to drop existing primary key.'
master [localhost:22031]> alter table t3 drop PRIMARY KEY,add primary key(id);
ERROR 4111 (HY000): Please drop primary key column to be able to drop generated invisible primary key.
master [localhost:22031]> alter table t3 drop column my_row_id,add primary key(id);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
2.4 关于主从复制
需要注意的是 set sql_generate_invisible_primary_key=on|off
and will not be copied to the slave library,If this feature is enabled on the main library,The slave library will not be enabled GIPK .That is to say, the slave library will not create a primary key for any table that does not have a primary key created on the source library.Readers may have questionsIf the main library turns off this feature,But the display from the library is turned on? 做个测试看看
在 master Turn off this feature on and create a table without a primary keyt6
master [localhost:22031]> set sql_generate_invisible_primary_key=off;
Query OK, 0 rows affected (0.00 sec)
master [localhost:22031]>
master [localhost:22031]>show variables like 'sql_generate_invisible_primary_key';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| sql_generate_invisible_primary_key | OFF |
+------------------------------------+-------+
1 row in set (0.00 sec)
master [localhost:22031]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t1 |
| t2 |
| t3 |
| t4 |
| t5 |
+----------------+
5 rows in set (0.00 sec)
master [localhost:22031]> create table t6(id int ,c1 int);
Query OK, 0 rows affected (0.01 sec)
master [localhost:22031]> show create table t6\G
*************************** 1. row ***************************
Table: t6
Create Table: CREATE TABLE `t6` (
`id` int DEFAULT NULL,
`c1` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
Enable this feature on the slave library
slave1 [localhost:22032]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t1 |
| t2 |
| t3 |
| t4 |
| t5 |
+----------------+
5 rows in set (0.00 sec)
slave1 [localhost:22032]> set sql_generate_invisible_primary_key=on;
Query OK, 0 rows affected (0.00 sec)
slave1 [localhost:22032]> show variables like 'sql_generate_invisible_primary_key';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| sql_generate_invisible_primary_key | ON |
+------------------------------------+-------+
1 row in set (0.00 sec)
slave1 [localhost:22032]> show create table t6\G
*************************** 1. row ***************************
Table: t6
Create Table: CREATE TABLE `t6` (
`id` int DEFAULT NULL,
`c1` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
结果: 主库关闭 GIPK ,从库开启 GIPK ,Create a table without a primary key on the source database,The slave library does not actively create a primary key for the table.
2.5 逻辑备份
Most instances take logical backups,如果 开启GIPK 模式时,MySQL 8.0.30 版本的 mysqldump 提供的 --skip-generated-invisible-primary-key
options are ignored GIPK 信息.简单来说,mysqldump 时 不带该参数,Logically exported data will contain an implicit primary key,如果带上该参数,without an implicit primary key.
2.6 限制
- 只支持 InnoDB 存储引擎.
- 支持 row 模式复制,不支持 statement 模式复制.
my_row_id
becomes a system keyword.
三、小结
总体而言,This feature is definitely a strong demand.毕竟林子大了,Anything can happen.运(chu)维(li)经(gu)验(zhang)比较丰富 DBA 而言,MySQL Database stability suffers from no primary key,For self-built scenarios, especially companies without an audit process,This feature can improve the stability and security of the database system.
参考文档
边栏推荐
猜你喜欢
How to set up wireless PPI communication between Weiluntong touch screen and S7-200smart?
js炫酷仪表盘插件
FreeRTOS--stack experiment
以Boost为例的type3电压环补偿器实例
SuperSlide系列之轮播图
如何搭建威纶通触摸屏与S7-200smart之间无线PPI通信?
SQL Server 数据库之生成与执行 SQL 脚本
photo-sphere-viewer中文文档
Manual architecture, Mysql interview 126 questions
FreeRTOS--栈实验
随机推荐
MD5 detailed explanation (check file integrity)
zabbix自动化监控脚本
图神经网络(GNN)的简介「建议收藏」
手撸架构,Redis面试41问
photo-sphere-viewer中文文档
MD5详解(校验文件完整性)
力扣704-二分查找
1.3 Rapid Spanning Tree Protocol RSTP
QListView的使用
js stopwatch countdown plugin
力扣58-左旋转字符串
Process finished with exit code 1
Likou 704 - binary search
无线振弦采集仪远程修改参数方式
ABAP-OOAVL template program
FreeRTOS creation tasks - dynamic creation, static creation
Object.entries()
Likou 209 - String with the Minimum Length - Sliding Window Method
How to set up wireless PPI communication between Weiluntong touch screen and S7-200smart?
太厉害了,终于有人能把TCP/IP 协议讲的明明白白了