Detailed explanation of the use of MySQL Online DDL

Detailed explanation of the use of MySQL Online DDL

text

Online DDL is only supported in MySQL 5.6. In versions 5.5 and earlier, using commands such as alter table/create index to modify the table structure will lock the table, which is obviously unacceptable in a production environment.

In MySQL 5.7, Online DDL has been continuously optimized in terms of performance and stability. It has significant performance advantages, has little impact on business load, and has controllable downtime. Compared with pt-osc/gh-ost, it does not require the installation of third-party dependency packages. It also supports Online DDL of the Inplace algorithm. Since there is no need to copy tables, the required disk space is also smaller.

Let's first look at a common DDL statement:

ALTER TABLE tbl_name ADD PRIMARY KEY (column), ALGORITHM=INPLACE, LOCK=NONE;

Among them, LOCK describes the degree of concurrency during the DDL operation, and ALGORITHM describes the implementation method of DDL.

LOCK parameter

  1. LOCK=NONE: Allow concurrent queries and DML operations
  2. LOCK=SHARED: allows concurrent queries but blocks DML operations
  3. LOCK=DEFAULT: Allow as much concurrency as possible (concurrent queries, DML, or both), as determined by the system. If the LOCK clause is omitted, it is equivalent to specifying LOCK=DEFAULT
  4. LOCK=EXCLUSIVE: Block concurrent queries and DML operations.

ALGORITHM parameters

  1. ALGORITHM=COPY: Use the copy method to modify the table, similar to pt-osc/gh-ost;
  2. ALGORITHM=INPLACE: Only the engine layer data needs to be changed, and the server layer is not involved;

COPY TABLE Process

  1. First, create a temporary table with the structure changed by ALTAR TABLE.
  2. Import the data in the original table into a temporary table (the server layer creates a temporary table, and there will be an IBD file displayed)
  3. Delete the original table
  4. Rename the temporary table to the original table name

At the same time, in order to maintain data consistency during this process, the table is locked and read-only during the intermediate data copying (Copy Table). If a write request comes in, the service will not be able to be provided, which will cause the number of connections to explode.

IN-PLACE Process

  1. Create a temporary file and scan all data pages of the original table primary key
  2. Generate a B+ tree using the original table records in the data page and store it in a temporary file (create a temporary file in the innodb_temp_data_file_path temporary tablespace)
  3. In the process of generating temporary files, all operations on the original table are recorded in a log file (rowlog)
  4. After the temporary file is generated, apply the operations in the log file to the temporary file to obtain a table that is identical to the original table in terms of data.
  5. Data files (log files record and replay operations)
  6. Replace the original table data file with a temporary file

During this process, the alter statement acquires the MDL write lock when it is started, but this write lock degenerates into a read lock before the data is actually copied. That is to say, during the most time-consuming process of copying data to a temporary file, the original table can be operated with DML. It is only locked during the final switching phase between the old and new tables. This rename process is very fast.

Allow concurrent DML and DDL operations

  • Create/add a secondary index
  • Rename a secondary index
  • Deleting a secondary index
  • Changing the index type (USING {BTREE | HASH})
  • Add a primary key (expensive cost)
  • Drop the primary key and add another (expensive cost) (ALTER TABLE tbl_name DROP PRIMARY KEY, ADD PRIMARY KEY (column), ALGORITHM=INPLACE, LOCK=NONE;)
  • New column (expensive cost)
  • Delete column (expensive cost)
  • Rename columns
  • Column reordering (expensive cost)
  • Changing column default values
  • Remove column default value
  • Change the column auto-increment value
  • Setting column attributes null/not null (expensive cost)
  • Modify the definition of an enumeration or collection column
  • Change ROW_FORMAT
  • Change key block size

Although Online DDL is allowed for operations marked as expensive cost, they will impose a high burden on the server IO and CPU. They will also cause replication blockage, which will cause another form of slave replication delay. Therefore, if the table is large, it is recommended to execute it during off-peak hours.

DDL operations that do not allow concurrent DML

  • Add full-text index
  • Adding a spatial index
  • Deleting a primary key
  • Changing column data types
  • Add an auto-increment column (add a new column->change it to an auto-increment column)
  • Change table character set
  • Modify the length of the data type
    • Special case: If the length of varchar character changes from 10 to less than 255, the table will not be locked if the inplace method is used; but if it changes from 255 to 10, the table will be locked;

The above is the detailed content of the detailed explanation of the use of MySQL Online DDL. For more information about the use of MySQL Online DDL, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • How to quickly add columns in MySQL 8.0
  • How to solve the synchronization delay caused by MySQL DDL
  • Detailed explanation of MySQL 8.0 atomic DDL syntax
  • MySQL online DDL tool gh-ost principle analysis
  • Use of MySQL DDL statements
  • Summary of common Mysql DDL operations
  • Analysis of the new features of MySQL 8.0 - transactional data dictionary and atomic DDL
  • Basic statements of MySQL data definition language DDL
  • MySQL 8.0 DDL atomicity feature and implementation principle
  • Summary of using MySQL online DDL gh-ost
  • Solve the problem of blocking positioning DDL in MySQL 5.7
  • MySQL 8.0 new features: support for atomic DDL statements
  • MySQL exposes Riddle vulnerability that can cause username and password leakage
  • Summary of MySQL 8.0 Online DDL Quick Column Addition

<<:  Implementation process of the magnifying glass effect in the Javascript example project

>>:  Make a nice flip login and registration interface based on html+css

Recommend

This article will show you how JavaScript garbage collection works

Table of contents 1. Overview 2. Memory Managemen...

JavaScript to implement the aircraft war game

This article shares with you how to use canvas an...

How to use firewall iptables strategy to forward ports on Linux servers

Forwarding between two different servers Enable p...

Docker adds a bridge and sets the IP address range

I don't know if it's because the binary d...

How to clean up the disk space occupied by Docker

Docker takes up a lot of space. Whenever we run c...

Use of Linux dynamic link library

Compared with ordinary programs, dynamic link lib...

JavaScript Composition and Inheritance Explained

Table of contents 1. Introduction 2. Prototype ch...

Vue implements multiple selections in the bottom pop-up window

This article example shares the specific code of ...

IE6 web page creation reference IE6 default style

This is not actually an official document of IE. I...

The "3I" Standards for Successful Print Advertising

For many domestic advertisers, the creation and ev...

Understanding v-bind in vue

Table of contents 1. Analysis of key source code ...

Self-study of MySql built-in functions knowledge points summary

String functions Check the ascii code value of th...

Several ways to store images in MySQL database

Usually the pictures uploaded by users need to be...

What to do after installing Ubuntu 20.04 (beginner's guide)

Ubuntu 20.04 has been released, bringing many new...