- 17.12.1 在线 DDL 操作17.12.1 Online DDL Operations
- 17.12.2 在线 DDL 性能与并发17.12.2 Online DDL Performance and Concurrency
- 17.12.3 在线 DDL 空间需求17.12.3 Online DDL Space Requirements
- 17.12.4 在线 DDL 内存管理17.12.4 Online DDL Memory Management
- 17.12.5 配置在线 DDL 操作的并行线程17.12.5 Configuring Parallel Threads for Online DDL Operations
- 17.12.6 使用在线 DDL 简化 DDL 语句17.12.6 Simplifying DDL Statements with Online DDL
- 17.12.7 在线 DDL 失败条件17.12.7 Online DDL Failure Conditions
- 17.12.8 在线 DDL 限制17.12.8 Online DDL Limitations
在线 DDL 功能提供对即时和就地表修改以及并发 DML 的支持。这一功能的优点包括:The online DDL feature provides support for instant and in-place table alterations and concurrent DML. Benefits of this feature include:
在繁忙的生产环境中提高响应能力和可用性,因为让表不可用几分钟或几小时是不切实际的。Improved responsiveness and availability in busy production environments, where making a table unavailable for minutes or hours is not practical.
对于就地操作,可以使用
LOCK子句在 DDL 操作期间调整性能和并发之间的平衡。请参阅 LOCK 子句。For in-place operations, the ability to adjust the balance between performance and concurrency during DDL operations using theLOCKclause. See The LOCK clause.比表复制方法占用更少的磁盘空间和 I/O 开销。Less disk space usage and I/O overhead than the table-copy method.
通常,您无需执行任何特殊操作即可启用在线 DDL。默认情况下,MySQL 会尽可能立即或就地执行操作,并尽量减少锁定。Typically, you do not need to do anything special to enable online DDL. By default, MySQL performs the operation instantly or in place, as permitted, with as little locking as possible.
您可以使用 ALGORITHM 和 LOCK 子句来控制 DDL 操作的某些方面,这些子句用于 ALTER TABLE 语句。它们放在语句的末尾,并用逗号与表和列的定义分开。例如:You can control aspects of a DDL operation using the
ALGORITHM and LOCK clauses of
the ALTER TABLE statement. These
clauses are placed at the end of the statement, separated from the
table and column specifications by commas. For example:
ALTER TABLEtbl_nameADD PRIMARY KEY (column), ALGORITHM=INPLACE;
LOCK 子句可用于就地执行的操作,并且有助于在操作过程中微调对表的并发访问程度。对于即时执行的操作,仅支持 LOCK=DEFAULT。ALGORITHM 子句主要用于性能比较,并在遇到问题时作为旧表复制行为的备用方案。例如:The LOCK clause may be used for operations that
are performed in place and is useful for fine-tuning the degree of
concurrent access to the table during operations. Only
LOCK=DEFAULT is supported for operations that are
performed instantly. The ALGORITHM clause is
primarily intended for performance comparisons and as a fallback to
the older table-copying behavior in case you encounter any issues.
For example:
为了避免在原地
ALTER TABLE操作期间意外使表无法进行读取、写入或两者,应该在ALTER TABLE语句中指定一个子句,例如LOCK=NONE(允许读取和写入)或LOCK=SHARED(允许读取)。如果所请求的并发级别不可用,操作将立即停止。To avoid accidentally making the table unavailable for reads, writes, or both, during an in-placeALTER TABLEoperation, specify a clause on theALTER TABLEstatement such asLOCK=NONE(permit reads and writes) orLOCK=SHARED(permit reads). The operation halts immediately if the requested level of concurrency is not available.要比较算法之间的性能,请运行带有
ALGORITHM=INSTANT、ALGORITHM=INPLACE和ALGORITHM=COPY的语句。您还可以运行启用old_alter_table配置选项的语句,以强制使用ALGORITHM=COPY。To compare performance between algorithms, run a statement withALGORITHM=INSTANT,ALGORITHM=INPLACEandALGORITHM=COPY. You can also run a statement with theold_alter_tableconfiguration option enabled to force the use ofALGORITHM=COPY.为了避免使用复制表的
ALTER TABLE操作占用服务器,请包含ALGORITHM=INSTANT或ALGORITHM=INPLACE。如果无法使用指定的算法,语句将立即停止。To avoid tying up the server with anALTER TABLEoperation that copies the table, includeALGORITHM=INSTANTorALGORITHM=INPLACE. The statement halts immediately if it cannot use the specified algorithm.