MySQL中Alter table 不长时间锁表的情况汇总。_MySQL
bitsCN.com
前言:
MySQL 的大表运维总是令人头疼的一件事,特别是大表表结构的修改尤为困难。
首先,alter table 的process不可被kill , 一旦执行就不可回退。
其次,大多数的alter table操作都会涉及 lock --- copy to new table --- rename --- unlock的过程,锁表时间会很长。
本文不是讨论如何进行大表表结构变更, 而是汇总一些不涉及copy to new table这一步的alter table情况。
这些情况下,mysql会直接修改frm文件,而lock的时间也仅是秒级的。
MySQL 5.0 系列 (即5.0.x 版本)
如果你是这个版本,很遗憾所有alter table 操作都会进行temp table copy。以下是摘自官方文档的解释:
If you use any option to ALTER TABLE
other than RENAME
, MySQL always creates a temporary table, even if the data wouldn't strictly need to be copied (such as when you change the name of a column).
MySQL 5.1 系列 (即5.0.x 版本)
以下操作不会有copy temp table操作,即锁表时间较短。
1. ALTER TABLE tbl_name
RENAME TO new_tbl_name
2. 不涉及数据修改的操作
2.1 列改名 (除了innodb)
2.2 修改默认值 (注意:必须使用 modify ,而不能使用change)
2.3 增加ENUM的枚举定义 (注意:仅当新增枚举在当前允许最大值內,例:1B 可存8个枚举,2B可存128个枚举)
3. 通过add partition 添加分区
4. 重命名索引
5. 添加删除索引 (仅 innodb plugin支持)
详见官方文档说明:
bitsCN.com
For
ALTER TABLE <em class="replaceable"><code>tbl_name
RENAME TOnew_tbl_name
without any other options, MySQL simply renames any files that correspond to the tabletbl_name
without making a copy. (You can also use theRENAME TABLE
statement to rename tables. See Section 13.1.33, “RENAME TABLE
Syntax”.) Any privileges granted specifically for the renamed table are not migrated to the new name. They must be changed manually.Alterations that modify only table metadata and not table data can be made immediately by altering the table's
.frm
file and not touching table contents. The following changes are fast alterations that can be made this way:
Renaming a column, except for the
InnoDB
storage engine.Changing the default value of a column (except for
NDB
tables; see Limitations ofNDBCLUSTER
online operations).Changing the definition of an
ENUM
orSET
column by adding new enumeration or set members to the end of the list of valid member values, as long as the storage side of the data type does not change. For example, adding a member to aSET
column that has 8 members changes the required storage per value from 1 byte to 2 bytes; this will require a table copy. Adding members in the middle of the list causes renumbering of existing members, which requires a table copy.
ALTER TABLE ... ADD PARTITION
creates no temporary table except when used withNDB
tables.ADD
orDROP
operations forRANGE
orLIST
partitions are immediate operations or nearly so.ADD
orCOALESCE
operations forHASH
orKEY
partitions copy data between changed partitions; unlessLINEAR HASH
orLINEAR KEY
was used, this is much the same as creating a new table (although the operation is done partition by partition).REORGANIZE
operations copy only changed partitions and do not touch unchanged ones.Renaming an index, except for
InnoDB
.Adding or dropping an index, for
InnoDB
(ifInnoDB Plugin
is used) andNDB
.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Title: Statements and specific code examples for viewing table data in MySQL MySQL is an open source relational database management system that is widely used in applications of all sizes. In MySQL, viewing table data is a very basic operation. The following will introduce how to implement this operation through specific statements and code examples. First, we will introduce the statements and specific code examples for viewing table data through the MySQL command line tool. Suppose we have a table named "employees", the following is the pass

Note 1. The Process class is an abstract class (all methods are abstract), encapsulating the process (ie, executing the program). 2. The Process class provides methods for inputting input from the process, executing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying the process. Example Processp=null;try{p=Runtime.getRuntime().exec("notepad.exe");p.waitFor();}catch(Exceptione){e.printStackTrace();}System.out.println(" I want to be printed...

MySQL is a commonly used relational database management system that supports the operation of renaming tables. Normally, renaming a table carries certain risks, so you should be very careful when performing this operation. In this article, we will explore how to implement the rename table statement in MySQL and provide detailed code examples. In MySQL, you can use the ALTERTABLE statement to rename a table. The following is the basic syntax of the ALTERTABLE rename statement: ALTERTABLEo

In the Oracle database, setting read-only permissions on tables is a very important operation, which can protect data security and prevent misoperations. The following will introduce how to set read-only permissions on tables in an Oracle database and provide specific code examples. First, we need to understand that in the Oracle database, users obtain permissions on tables through authorization. Table permissions include SELECT (query), INSERT (insert), UPDATE (update) and DELETE (delete) operations. Here we will introduce

A Microsoft computer case is a case used to protect your computer. It not only provides additional protection but also adds a decorative effect. For those who want to add some personalized elements to their computers, Microsoft computer cases are an ideal choice. The Microsoft Computer Case Recommended Configuration Table is a detailed table that lists the recommended configurations for different models and specifications of Microsoft computer cases. We will introduce the recommended configuration table of Microsoft computer cases in detail and explain some aspects of it. 1. Applicable models In the recommended configuration table, the first thing to consider is the applicable models of Microsoft computer cases. Different models of Microsoft computer cases may have different sizes and shapes, so you need to choose the appropriate case according to your computer model. The configuration table lists the microcontrollers applicable to different models.

Use MySQL to create a recommendation system table to implement the recommendation system function. The recommendation system is a system used to recommend personalized content to users based on their preferences and behaviors. In the recommendation system, the database is a key component, which stores information such as user data, item data, and user-item interaction data. As a commonly used relational database management system, MySQL can be used to create recommendation system tables and implement the functions of the recommendation system. This article will introduce how to use MySQL to create a recommendation system table and demonstrate it through code examples.

MySQL creates a user rights table to implement user rights management functions. MySQL is a commonly used relational database management system that provides powerful user rights management functions. In a multi-user database environment, it is very important to properly manage user permissions. This article will introduce how to implement the user rights management function by creating a user rights table and illustrate it with code examples. 1. Create a permission table First, we need to create a permission table to store the user's permission information. The fields of the permission table include user ID (user_

What situations in Oracle database can cause tables to be locked? In Oracle database, it is a common situation that the table is locked, usually caused by users performing data operations. Common table lock situations include row-level locking, transaction-level locking, and DDL operations. These situations will be introduced in detail below and corresponding code examples will be given. Row-level locking: When a user updates a table in a transaction, the database locks the corresponding data row to prevent other transactions from modifying the data at the same time. If the transaction is not
