MySQL 8.0新特性--快速新增列

Instant Add Column

功能限制

MySQL 8.0提供了快速新增列的功能,使用该功能限制:

  • 添加列不能在同一语句中与ALTER TABLE不支持的其他操作组合使用ALGORITHM=INSTANT
  • 只能将一列添加为表的最后一列。不支持将列添加到其他列中的任何其他位置。
  • 列不能添加到使用的表中 ROW_FORMAT=COMPRESSED
  • 列不能添加到包含FULLTEXT索引的表中 。
  • 列不能添加到临时表中。临时表仅支持ALGORITHM=COPY
  • 列不能添加到驻留在数据字典表空间中的表中。
  • 添加列时不评估行大小限制。但是,在插入和更新表中的行的DML操作期间,将检查行大小限制。

演示Demo

mysql> show create table tb1001 G
*************************** 1. row ***************************
       Table: tb1001
Create Table: CREATE TABLE `tb1001` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `c1` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2490316 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)

mysql> show table status like 'tb1001'G
*************************** 1. row ***************************
           Name: tb1001
         Engine: InnoDB
        Version: 10
     Row_format: Dynamic
           Rows: 2092860
 Avg_row_length: 40
    Data_length: 85639168
Max_data_length: 0
   Index_length: 0
      Data_free: 3145728
 Auto_increment: 2490316
    Create_time: 2021-03-06 21:32:57
    Update_time: NULL
     Check_time: NULL
      Collation: utf8mb4_0900_ai_ci
       Checksum: NULL
 Create_options:
        Comment:
1 row in set (0.00 sec)

mysql> alter table tb1001 add c2 int not null default 10;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table tb1001 add c3 int not null default 10, algorithm=INSTANT;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table tb1001 add c4 int not null default 10, algorithm=copy;
Query OK, 2097152 rows affected (9.66 sec)
Records: 2097152  Duplicates: 0  Warnings: 0

学习总结

SQL Server在早期版本就实现了快速加列的特性,在执行DDL操作时仅修改表的元数据,在DML操作时读取记录时根据当前记录数据+当前表元数据来"生成"完整数据。

与MySQL的相同点有:

  • 都通过修改元数据来实现快速加列
  • 都只允许将列新增至表中最后位置

与MySQL的异同点:

  • MySQL允许新增列为NOT NULL,而SQL Server只能新增列不能为NOT NULL
原文地址:https://www.cnblogs.com/gaogao67/p/14492553.html