MySQL|什么情况下拓展字段长度会锁表?

Posted yangyidba

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL|什么情况下拓展字段长度会锁表?相关的知识,希望对你有一定的参考价值。

作为产品DBA,经常被开发问,修改字段长度锁表吗?然后凭借"经验"给出回答:如果字段长度超过256个字符就会锁表。

现在看来回答错误 。看看mysql 官方文档

Extending VARCHAR column size

The number of length bytes(字节) required by a VARCHAR column must remain the same. For VARCHAR columns of 0 to 255 bytes in size, one length byte is required to encode the value.

For VARCHAR columns of 256 bytes in size or more, two length bytes are required. As a result, in-place ALTER TABLE only supports increasing VARCHAR column size from 0 to 255 bytes, or from 256 bytes to a greater size. In-place ALTER TABLE does not support increasing the size of a VARCHAR column from less than 256 bytes to a size equal to or greater than 256 bytes. In this case, the number of required length bytes changes from 1 to 2, which is only supported by a table copy (ALGORITHM=COPY).

翻译上面的意思

  1. 字符串的字段是以字节为单位存储的,utf8 一个字符需要三个字节,utf8mb4 一个字符需要4个字节。对于小于等于255字节以内的长度可以使用一个byte 存储。大于255个字节的长度则需要使用2个byte存储。

  2. online ddl  in-place 模式(不锁表)只支持字段的字节长度从0到255之间 或者256到更大值之间变化。

  3. 如果修改字段的长度,导致字段的字节长度无法使用 1 byte表示,得使用2个byte才能表示,比如从 240 修改为 256 ,如果在默认字符集为utf8mb4的情况下,varchar(60) 修改为 varchar(64),则DDL需要以copy模式,也即会锁表,阻塞写操作。

另外就是不支持以 in-place 方式缩小字段长度。

Decreasing VARCHAR size using in-place ALTER TABLE is not supported. Decreasing VARCHAR size requires a table copy (ALGORITHM=COPY).

实践出真知

t1 表的字符集为utf8mb4,初始字段长度为20 ,80个字节,可以使用1byte表示。分别修改字符串长度为 60--->64--->128。当字段的字节数变动 跨越了256 则会锁表。


关于MySQL online ddl   奉献一份 收藏多年的流程图, 以xmind 文件方式分享到百度云盘上,方便给位查看,可以直接访问原文直达百度云盘。



链接: https://pan.baidu.com/s/1kzMqNK0ZhV1GUzFjWCc88w

提取码: 8duj 

-The End-




以上是关于MySQL|什么情况下拓展字段长度会锁表?的主要内容,如果未能解决你的问题,请参考以下文章

sqlserver锁表机制

mysql lock 锁表

mysql中select会锁表吗?如果锁表,锁表范围怎么样(程序员必知)

mysql中select会锁表吗?如果锁表,锁表范围怎么样(程序员必知)

informix行锁会锁表吗

小白学习MySQL - 查询会锁表?