当前位置: 首页 > 知识库问答 >
问题:

VarChar的行大小不超过

岑俊明
2023-03-14

我的表:

create table test( pk bigint primary key,
value1 varchar(255),
value2 varchar(255),
value3 varchar(255),
value4 varchar(255),
value5 varchar(255),
value6 varchar(255),
value7 varchar(255),
value8 varchar(255),
value9 varchar(255),
value10 varchar(255),
value11 varchar(255),
);

插入查询:

insert into test values(1, '‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱','‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱','‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱','‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱','‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱','‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱','‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱','‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱','‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱','‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱','‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱');

我的页面大小是16KB。因此,我的表中的一行最多可以包含8192字节(即8KB)。

我创建了11个VARCHAR列(每个255个字符),其中这11列最多可以容纳255*11 = 2805字符。

如果我存储2805-3字节的字符,它将需要(255*11*3)=8415字节,这超过了最大行大小的限制(8192字节)。

现在我尝试插入上面的单行查询,它有8415字节数据。但是我的MYSQL没有抛出接受该插入查询的错误。

3 byte character - ‱

Row Format - DYNAMIC

Collation - UTF8mb3

Character set - utf8_general_ci

Mysql - 5.7

更新:CHAR列也发生了同样的情况(将VarChar更改为CHAR),这是一个固定长度的列。


共有3个答案

计均
2023-03-14

在内部,对于utf8mb3和utf8mb4等可变长度字符集,InnoDB尝试通过修剪尾随空格将CHAR(N)存储在N个字节中。如果CHAR(N)列值的字节长度超过N个字节,则将尾随空格修剪为列值字节长度的最小值。CHAR(N)列的最大长度为最大字符字节长度×N。

至少为CHAR(N)保留N个字节。在许多情况下,保留最小空间N可以在不导致索引页碎片的情况下就地完成列更新。相比之下,在使用冗余行格式时,CHAR(N)列占据了最大字符字节长度×N。

-- https://dev.mysql.com/doc/refman/5.6/en/innodb-row-format.html#innodb-紧凑行格式特征

上述内容指的是ROW\u格式,即压缩、动态和压缩(压缩前)。这是关于CHAR的页上存储。

程鸿畅
2023-03-14

正如SHADOW对VARHCAR列所说的

如果一行小于半页长,则全部存储在页面内的本地。如果它超过半页,则选择可变长度列进行外部页外存储,直到该行适合半页

对于CHAR

InnoDB将长度大于或等于768字节的固定长度字段编码为可变长度字段,可以在页外存储。例如,如果字符集的最大字节长度大于3,则字符(255)列可能超过768个字节,就像utf8mb4一样。https://forums.mysql.com/read.php?24,645115645215#msg-645215

mysql在内部将固定长度字段转换为可变长度字段https://dev.mysql.com/doc/refman/5.6/en/storage-requirements.html

屈昊天
2023-03-14

您误解了innodb行大小限制的描述。引用mysql手册(重点是我的):

对于4KB、8KB、16KB和32KB页面大小,除了可变长度列(VARBINARY、VARCHAR、BLOB和TEXT)之外,最大行长度略小于页面的一半。例如,默认innodb\u page\u大小为16KB的最大行长度约为8000字节。。。

如果一行的长度小于半页,则所有行都将本地存储在该页中。如第14.12.2节“文件空间管理”所述,如果超过半页,则选择可变长度列进行外部页外存储,直到该行位于半页内。

由于您的字段是varchar(可变长度的fiekd类型),因此超过半页大小限制的数据只是存储在其他页面外位置,因此您的sql语句是正确的。

编辑

对于字符字段,行为取决于行格式和使用的字符集。

字符字段的长度在字符数方面是固定的,但根据字符集的不同,字节长度可以是固定的(例如latin1是固定的1字节/字符),也可以是可变的(例如utf8mb3是可变的1-3字节/字符)。

对于紧凑格式,字符集无关紧要,如果要创建表时最大可能字节长度超过从页面大小配置派生的数据页面限制,则会收到错误消息。

对于动态行格式,如果字符集是固定长度并且字节长度超过数据页限制,则在要创建表时会出现错误。但是,如果字符集是可变长度的,则数据会存储在溢出页面中。

 类似资料:
  • 问题内容: 如果我在表中有一列具有类型字段,并且如果我尝试插入长度为16的数据,MySQL会提示错误 有谁知道为什么MySQL中的VARCHAR字段采用固定长度?另外,基于给定的大小,每个记录的VARCHAR字段会占用多少字节? 问题答案: 如果将一列设置为允许的最大字节数为15,则在不修改该列以使其支持超过15个字符的情况下,最多不能传递15个字符。如果存储4个字符串,则只能使用约4个字节可能的

  • 问题内容: MySQL如何存储varchar字段?我可以假设以下模式代表合理的存储大小: 1,2,4,8,16,32,64,128,255(最大) 通过示例进行说明。可以说我有20个字符的varchar字段。MySQL在创建此字段时是否基本上保留32个字节的空间(不确定是否为字节),但只允许输入20个字节? 我想我担心优化大型表的磁盘空间。 问题答案: 为了回答这个问题,在磁盘上MySql使用1

  • 问题内容: 我正在尝试用单词列表填充SQL表。该表本身非常简单: 我遇到的问题是这样的:当我执行以下操作时,会背对背插入 第二个插入因约束冲突而失败(“键’WORD’的条目’seth’重复”)。 我如何获得区分大小写的约束? 问题答案: 看起来mysql 默认是不区分大小写的: 您可能需要使用区分大小写的排序规则创建列(例如utf8_bin):

  • 问题内容: 我注意到秋千上的容器之间有奇怪的行为。 为了举例说明该测试,我创建了一个JFrame和一个JPanel,并将面板设置为 contentPane。我将首选和最大的JPanel大小定义为400,300。所有 这些都可以在下面的示例中看到: The result is: 令我惊讶的是,终端的输出为: 面板尺寸:[400,300] 框架尺寸:[416,338] 我不明白为什么框架会增加这个额外

  • 问题内容: 我需要将数据(varbinary(max))从一个表迁移到另一个表。执行更新以执行此操作时,出现以下错误 这是我用来从表DocumentPublication复制到DocumentVersion的更新 有无演员 通过逐行执行更新,我隔离了给出错误的行。奇怪的是,该字段中的数据仅为3950字节,而其他具有更少或更多(例如2000字节或20MB)的行则可以正常工作。 然后,我用另一个名称重

  • 问题内容: 当尺寸相同时,numpy Mean函数可以很好地工作。 但是如果我用改变行大小来做到这一点就会出现错误 我在文档中找不到有关此问题的任何内容。我可以自己计算平均值,但我想为此使用内置函数。 问题答案: 这是一种方法- 样品运行- 使用列表理解的更简单的替代方法-