MySQL 8.0 Reference Manual(读书笔记83节-- InnoDB and Online DDL (3))

这篇具有很好参考价值的文章主要介绍了MySQL 8.0 Reference Manual(读书笔记83节-- InnoDB and Online DDL (3))。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1. Foreign Key Operations

The following table provides an overview of online DDL support for foreign key operations. An asterisk【ˈæstərɪsk 星号(置于词语旁以引起注意或另有注释);】 indicates additional information, an exception, or a dependency.

Operation Instant In Place Rebuilds Table Permits Concurrent DML Only Modifies Metadata
Adding a foreign key constraint No Yes* No Yes Yes
Dropping a foreign key constraint No Yes No Yes Yes

• Adding a foreign key constraint

The INPLACE algorithm is supported when foreign_key_checks is disabled. Otherwise, only the COPY algorithm is supported.

ALTER TABLE tbl1 ADD CONSTRAINT fk_name FOREIGN KEY index (col1)
 REFERENCES tbl2(col2) referential_actions;

• Dropping a foreign key constraint

ALTER TABLE tbl DROP FOREIGN KEY fk_name;

Dropping a foreign key can be performed online with the foreign_key_checks option enabled or disabled.

If you do not know the names of the foreign key constraints on a particular table, issue the following statement and find the constraint name in the CONSTRAINT clause for each foreign key:

SHOW CREATE TABLE table\G

Or, query the Information Schema TABLE_CONSTRAINTS table and use the CONSTRAINT_NAME and CONSTRAINT_TYPE columns to identify the foreign key names.

You can also drop a foreign key and its associated index in a single statement:

ALTER TABLE table DROP FOREIGN KEY constraint, DROP INDEX index;

补充说明

If foreign keys are already present in the table being altered (that is, it is a child table containing a FOREIGN KEY ... REFERENCE clause), additional restrictions【riˈstrɪkʃənz 限制;约束;制约因素;限制规定;限制法规;】 apply to online DDL operations, even those not directly involving the foreign key columns:

• An ALTER TABLE on the child table could wait for another transaction to commit, if a change to the parent table causes associated changes in the child table through an ON UPDATE or ON DELETE clause using the CASCADE or SET NULL parameters.

• In the same way, if a table is the parent table in a foreign key relationship, even though it does not contain any FOREIGN KEY clauses, it could wait for the ALTER TABLE to complete if an INSERT, UPDATE, or DELETE statement causes an ON UPDATE or ON DELETE action in the child table.

2. Table Operations

The following table provides an overview of online DDL support for table operations. An asterisk indicates additional information, an exception, or a dependency【dɪˈpendənsi (尤指不正常或不必要的)依靠,依赖;附属国;附属地;】.

Operation Instant In Place Rebuilds Table Permits Concurrent DML Only Modifies Metadata
Changing the ROW_FORMAT No Yes Yes Yes No
Changing the KEY_BLOCK_SIZE No Yes Yes Yes No
Setting persistent table statistics No Yes No Yes Yes
Specifying a character set No Yes Yes* Yes No
Converting a character set No No Yes* No No
Optimizing a table No Yes* Yes Yes No
Rebuilding with the FORCE option No Yes* Yes Yes No
Performing a null rebuild No Yes* Yes Yes No
Renaming a table Yes Yes No Yes Yes

• Changing the ROW_FORMAT

ALTER TABLE tbl_name ROW_FORMAT = row_format, ALGORITHM=INPLACE, LOCK=NONE;

Data is reorganized substantially【səbˈstænʃəli 基本上;大体上;非常;大大地;总的来说;】, making it an expensive operation.

• Changing the KEY_BLOCK_SIZE

ALTER TABLE tbl_name KEY_BLOCK_SIZE = value, ALGORITHM=INPLACE, LOCK=NONE;

Data is reorganized substantially, making it an expensive operation.

• Setting persistent【pərˈsɪstənt 持续的;持久的;坚持不懈的;执著的;不屈不挠的;反复出现的;连绵的;】 table statistics options

ALTER TABLE tbl_name STATS_PERSISTENT=0, STATS_SAMPLE_PAGES=20, STATS_AUTO_RECALC=1, ALGORITHM=INPLACE, LOCK=NONE;

Only modifies table metadata.

Persistent statistics include STATS_PERSISTENT, STATS_AUTO_RECALC, and STATS_SAMPLE_PAGES.

• Specifying a character set

ALTER TABLE tbl_name CHARACTER SET = charset_name, ALGORITHM=INPLACE, LOCK=NONE;

Rebuilds the table if the new character encoding is different.

• Converting a character set

ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name, ALGORITHM=COPY;

Rebuilds the table if the new character encoding is different.

• Optimizing a table

OPTIMIZE TABLE tbl_name;

In-place operation is not supported for tables with FULLTEXT indexes. The operation uses the INPLACE algorithm, but ALGORITHM and LOCK syntax is not permitted.

• Rebuilding a table with the FORCE option

ALTER TABLE tbl_name FORCE, ALGORITHM=INPLACE, LOCK=NONE;

Uses ALGORITHM=INPLACE as of MySQL 5.6.17. ALGORITHM=INPLACE is not supported for tables with FULLTEXT indexes.

• Performing a "null" rebuild

ALTER TABLE tbl_name ENGINE=InnoDB, ALGORITHM=INPLACE, LOCK=NONE;

Uses ALGORITHM=INPLACE as of MySQL 5.6.17. ALGORITHM=INPLACE is not supported for tables with FULLTEXT indexes.

• Renaming a table

ALTER TABLE old_tbl_name RENAME TO new_tbl_name, ALGORITHM=INSTANT;

Renaming a table can be performed instantly or in place. MySQL renames files that correspond to the table tbl_name without making a copy. (You can also use the RENAME TABLE statement to rename tables.) Privileges granted specifically for the renamed table are not migrated to the new name. They must be changed manually.

3.Tablespace Operations

The following table provides an overview of online DDL support for tablespace operations.

Operation Instant In Place Rebuilds Table Permits Concurrent DML Only Modifies Metadata
Renaming a general tablespace No Yes No Yes Yes
Enabling or disabling general tablespace encryption No Yes No Yes No
Enabling or disabling file-per-table tablespace encryption No No Yes No No

• Renaming a general tablespace

ALTER TABLESPACE tablespace_name RENAME TO new_tablespace_name;

ALTER TABLESPACE ... RENAME TO uses the INPLACE algorithm but does not support the ALGORITHM clause.

• Enabling or disabling general tablespace encryption

ALTER TABLESPACE tablespace_name ENCRYPTION='Y';

ALTER TABLESPACE ... ENCRYPTION uses the INPLACE algorithm but does not support the ALGORITHM clause.

• Enabling or disabling file-per-table tablespace encryption

ALTER TABLE tbl_name ENCRYPTION='Y', ALGORITHM=COPY;

 文章来源地址https://www.toymoban.com/news/detail-844305.html

4.Online DDL Performance and Concurrency

Online DDL improves several aspects of MySQL operation:

• Applications that access the table are more responsive【rɪˈspɑːnsɪv 反应敏捷的;反应热烈的;热情的;反应敏捷;反应积极;】 because queries and DML operations on the table can proceed while the DDL operation is in progress. Reduced locking and waiting for MySQL server resources leads to greater scalability, even for operations that are not involved in the DDL operation. 

• Instant operations only modify metadata in the data dictionary. An exclusive metadata lock on the table may be taken briefly during the execution phase of the operation. Table data is unaffected, making operations instantaneous. Concurrent DML is permitted.

• Online operations avoid the disk I/O and CPU cycles associated with the table-copy method, which minimizes overall load on the database. Minimizing load helps maintain good performance and high throughput during the DDL operation.

• Online operations read less data into the buffer pool than table-copy operations, which reduces purging of frequently accessed data from memory. Purging of frequently accessed data can cause a temporary performance dip after a DDL operation.

4.1 The LOCK clause

 By default, MySQL uses as little locking as possible during a DDL operation. The LOCK clause can be specified for in-place operations and some copy operations to enforce more restrictive locking, if required. If the LOCK clause specifies a less restrictive level of locking than is permitted for a particular DDL operation, the statement fails with an error. LOCK clauses are described below, in order of least to most restrictive:

 • LOCK=NONE:

Permits concurrent【kənˈkɜːrənt 同时发生的;同意的,一致的;】 queries and DML. For example, use this clause for tables involving【ɪnˈvɑːlvɪŋ 需要;影响;(使)参加,加入;包含;牵涉;牵连;使成为必然部分(或结果);】 customer signups or purchases【ˈpɜːrtʃəsɪz 购买;采购;买;】, to avoid making the tables unavailable during lengthy DDL operations.

• LOCK=SHARED:

Permits concurrent queries but blocks DML.

For example, use this clause on data warehouse tables, where you can delay data load operations until the DDL operation is finished, but queries cannot be delayed for long periods【ˈpɪriədz 时期;(人生或国家历史的)阶段,时代;一段时间;纪(地质年代,代下分纪);】.

• LOCK=DEFAULT:

Permits as much concurrency【并发】 as possible (concurrent queries, DML, or both). Omitting【əˈmɪtɪŋ 忽略;遗漏;删除;漏掉;不做;未能做;】 the LOCK clause is the same as specifying LOCK=DEFAULT.

Use this clause when you do not expect the default locking level of the DDL statement to cause any availability problems for the table.

• LOCK=EXCLUSIVE:

Blocks concurrent queries and DML.

Use this clause if the primary concern is finishing the DDL operation in the shortest amount of time possible, and concurrent query and DML access is not necessary. You might also use this clause if the server is supposed to be idle【ˈaɪdl 空闲的;闲置的;懒惰的;闲散的;懈怠的;没有工作的;漫无目的的;】, to avoid unexpected table accesses.

 4.2 Online DDL and Metadata Locks

Online DDL operations can be viewed as having three phases【ˈfeɪzɪz 阶段;时期;】:

• Phase 1: Initialization

In the initialization phase, the server determines how much concurrency【并发;】 is permitted during the operation, taking into account【考虑到;虑及;】 storage engine capabilities, operations specified in the statement, and user-specified ALGORITHM and LOCK options. During this phase, a shared upgradeable metadata lock is taken to protect the current table definition.

• Phase 2: Execution

In this phase, the statement is prepared and executed. Whether the metadata lock is upgraded to exclusive depends on the factors assessed in the initialization phase. If an exclusive metadata lock is required, it is only taken briefly during statement preparation.

• Phase 3: Commit Table Definitio

In the commit table definition phase, the metadata lock is upgraded to exclusive to evict the old table definition and commit the new one. Once granted, the duration of the exclusive metadata lock is brief.

Due to the exclusive metadata lock requirements outlined above, an online DDL operation may have to wait for concurrent transactions that hold metadata locks on the table to commit or rollback. Transactions started before or during the DDL operation can hold metadata locks on the table being altered. In the case of a long running or inactive transaction, an online DDL operation can time out waiting for an exclusive metadata lock. Additionally, a pending exclusive metadata lock requested by an online DDL operation blocks subsequent transactions on the table.

The following example demonstrates an online DDL operation waiting for an exclusive metadata lock, and how a pending metadata lock blocks subsequent transactions on the table.

Session 1:

mysql> CREATE TABLE t1 (c1 INT) ENGINE=InnoDB;
mysql> START TRANSACTION;
mysql> SELECT * FROM t1;

The session 1 SELECT statement takes a shared metadata lock on table t1.

Session 2:

mysql> ALTER TABLE t1 ADD COLUMN x INT, ALGORITHM=INPLACE, LOCK=NONE;

The online DDL operation in session 2, which requires an exclusive metadata lock on table t1 to commit table definition changes, must wait for the session 1 transaction to commit or roll back.

Session 3:

mysql> SELECT * FROM t1;

The SELECT statement issued in session 3 is blocked waiting for the exclusive metadata lock requested by the ALTER TABLE operation in session 2 to be granted.

You can use SHOW FULL PROCESSLIST to determine if transactions are waiting for a metadata lock.

mysql> SHOW FULL PROCESSLIST\G
...
*************************** 2. row ***************************
 Id: 5
 User: root
 Host: localhost
 db: test
Command: Query
 Time: 44
 State: Waiting for table metadata lock
 Info: ALTER TABLE t1 ADD COLUMN x INT, ALGORITHM=INPLACE, LOCK=NONE
...
*************************** 4. row ***************************
 Id: 7
 User: root
 Host: localhost
 db: test
Command: Query
 Time: 5
 State: Waiting for table metadata lock
 Info: SELECT * FROM t1
4 rows in set (0.00 sec)

Metadata lock information is also exposed【ɪkˈspoʊzd 暴露;露出;揭露;使面临,使遭受(危险或不快);显露;揭穿;】 through the Performance Schema metadata_locks table【通过系统表也可以查看】, which provides information about metadata lock dependencies between sessions, the metadata lock a session is waiting for, and the session that currently holds the metadata lock.

4.3 Online DDL Performance

The performance of a DDL operation is largely determined by【很大程度上被...所决定】 whether the operation is performed instantly, in place, and whether it rebuilds the table.

To assess the relative performance of a DDL operation, you can compare results using ALGORITHM=INSTANT, ALGORITHM=INPLACE, and ALGORITHM=COPY. A statement can also be run with old_alter_table enabled to force the use of ALGORITHM=COPY.

For DDL operations that modify table data, you can determine whether a DDL operation performs changes in place or performs a table copy by looking at the “rows affected” value displayed after the command finishes. For example:

• Changing the default value of a column (fast, does not affect the table data):

Query OK, 0 rows affected (0.07 sec)

• Adding an index (takes time, but 0 rows affected shows that the table is not copied):

Query OK, 0 rows affected (21.42 sec)

• Changing the data type of a column (takes substantial time and requires rebuilding all the rows of the table):

Query OK, 1671168 rows affected (1 min 35.54 sec)

Before running a DDL operation on a large table, check whether the operation is fast or slow as follows:

1. Clone the table structure.

2. Populate the cloned table with a small amount of data.

3. Run the DDL operation on the cloned table.

4. Check whether the “rows affected” value is zero or not. A nonzero value means the operation copies table data, which might require special planning. For example, you might do the DDL operation during a period of scheduled downtime, or on each replica server one at a time.

说明

For a greater understanding of the MySQL processing associated with a DDL operation, examine Performance Schema and INFORMATION_SCHEMA tables related to InnoDB before and after DDL operations to see the number of physical reads, writes, memory allocations, and so on.

Because there is some processing work involved with recording the changes made by concurrent DML operations, then applying those changes at the end, an online DDL operation could take longer overall than the table-copy mechanism that blocks table access from other sessions. The reduction in raw performance is balanced against better responsiveness for applications that use the table. When evaluating the techniques for changing table structure, consider end-user perception of performance, based on factors such as load times for web pages.

 

到了这里,关于MySQL 8.0 Reference Manual(读书笔记83节-- InnoDB and Online DDL (3))的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • MySQL 8.0 Reference Manual(读书笔记79节-- InnoDB Table and Page Compression (2))

    Overall application performance, CPU and I/O utilization and the size of disk files are good indicators of how effective compression is for your application. This section builds on the performance tuning advice and shows how to find problems that might not turn up during initial testing. To dig deeper into performance considerations for compressed tables, y

    2024年03月28日
    浏览(44)
  • MySQL 8.0 Reference Manual(读书笔记73节--Thread Concurrency for InnoDB and I/O Threads)

    InnoDB uses operating system threads to process requests from user transactions. (Transactions may issue many requests to InnoDB before they commit or roll back.) On modern operating systems and servers with multi-core processors, where context switching is efficient, most workloads run well without any limit on the number of concurrent threads. In situation

    2024年03月25日
    浏览(31)
  • MySQL 8.0 Reference Manual(读书笔记63节--InnoDB Locking)

    To implement a large-scale, busy, or highly reliable database application, to port substantial【səbˈstænʃl】 code from a different database system, or to tune MySQL performance, it is important to understand InnoDB locking and the InnoDB transaction model. InnoDB implements【ˈɪmplɪments 实施; 执行; 贯彻; 使生效; 】 standard row-level lock

    2024年04月22日
    浏览(31)
  • MySQL 8.0 Reference Manual(读书笔记69节--InnoDB Startup Configuration)

    有些InnoDB的配置,是在实例初始化时,就决定了,所以,建议写在configuration file 文件中。 Because MySQL uses data file, log file, and page size settings to initialize InnoDB, it is recommended that you define these settings in an option file that MySQL reads at startup, prior to initializing InnoDB. Normally, InnoDB is initialize

    2024年03月24日
    浏览(40)
  • MySQL 8.0 Reference Manual(读书笔记80节-- InnoDB Row Formats)

    The row format of a table determines how its rows are physically stored, which in turn can affect the performance of queries and DML operations. As more rows fit into a single disk page, queries and index lookups can work faster, less cache memory is required in the buffer pool, and less I/O is required to write out updated values. The data in each table is

    2024年04月08日
    浏览(43)
  • MySQL 8.0 Reference Manual(读书笔记70节--InnoDB Buffer Pool Configuration)

    When increasing or decreasing innodb_buffer_pool_size , the operation is performed in chunks. Chunk size is defined by the innodb_buffer_pool_chunk_size configuration option, which has a default of 128M.-----innodb_buffer_pool_size的扩容和缩容,都是以innodb_buffer_pool_chunk_size为单位进行的,其默认为128M 。 Buffer pool size must always

    2024年03月24日
    浏览(35)
  • MySQL 8.0 Reference Manual(读书笔记76节--Optimizer Statistics for InnoDB (2))

    开始讲解 非固化的统计数据 This section describes how to configure non-persistent optimizer statistics. Optimizer statistics are not persisted to disk when innodb_stats_persistent=OFF or when individual tables are created or altered with STATS_PERSISTENT=0. Instead, statistics are stored in memory, and are lost when the server is shut down. Statis

    2024年03月27日
    浏览(32)
  • MySQL 8.0 Reference Manual(读书笔记75节--Optimizer Statistics for InnoDB (1))

    This section describes how to configure persistent and non-persistent optimizer statistics for InnoDB tables. Persistent optimizer statistics are persisted across server restarts【意思是重启操作,对这些数据没有影响】, allowing for greater plan stability and more consistent query performance. Persistent optimizer statistics also provide con

    2024年03月27日
    浏览(30)
  • MySQL 8.0 Reference Manual(读书笔记72节--InnoDB Buffer Pool Configuration (3))

    主要内容为【热启动】,就是把之前常用的内存数据,按照划定的比例快速重新加载到内存中。 To reduce the warmup period after restarting the server, InnoDB saves a percentage of the most recently used pages for each buffer pool at server shutdown and restores these pages at server startup. The percentage of recently used pa

    2024年03月24日
    浏览(34)
  • MySQL 8.0 Reference Manual(读书笔记71节--InnoDB Buffer Pool Configuration (2))

    【目的是未来提高并发,减少竞争】 For systems with buffer pools in the multi-gigabyte range, dividing the buffer pool into separate instances can improve concurrency, by reducing contention as different threads read and write to cached pages. This feature is typically intended for systems with a buffer pool size in the multi-gigabyte range. Mu

    2024年03月24日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包