亲宝软件园·资讯

展开

MySQL行大小限制

T.Y.Bao 人气:0

字段数量限制 Column Count Limits

MySQL对于每个表的字段数量是严格控制在4096个,但是实际情况下的限制大小取决于以下因素(也就是基本达不到4096就被限制了):

行大小限制 Row Size Limits

row size由以下因素影响:

mysql> CREATE TABLE t (a VARCHAR(10000), b VARCHAR(10000),
       c VARCHAR(10000), d VARCHAR(10000), e VARCHAR(10000),
       f VARCHAR(10000), g VARCHAR(6000)) ENGINE=InnoDB CHARACTER SET latin1;
ERROR 1118 (42000): Row size too large. The maximum row size for the used
table type, not counting BLOBs, is 65535. This includes storage overhead,
check the manual. You have to change some columns to TEXT or BLOBs

看,由于Latin1编码1个字符是1个字节,总共不能超过65535个字符,超过就报错;而常用的utf8mb4编码最多1个字符占用4个字节,所以当使用utf8mb4编码时,最多只能有65535/4=16383个字符(实际测肯定会小点,因为还有字节去记录变长字段长度):

test> CREATE TABLE t (a VARCHAR(10000), b VARCHAR(6384)
             ) ENGINE=InnoDB CHARACTER SET utf8mb4
[2022-07-21 15:05:56] [42000][1118] Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs

test> CREATE TABLE t (a VARCHAR(10000), b VARCHAR(6383)
             ) ENGINE=InnoDB CHARACTER SET utf8mb4
[2022-07-21 15:09:51] [42000][1118] Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs
test> CREATE TABLE t (a VARCHAR(10000), b VARCHAR(6382)
             ) ENGINE=InnoDB CHARACTER SET utf8mb4
[2022-07-21 15:09:58] completed in 33 ms

根据提示,如果超过row size限制,可以使用TEXT or BLOBs类型,这个在row size中只占用9到12个字节。

8126B限制

65535B的限制主要针对不定长类型的限制,而定长类型的限制更为严格,像在Innodb存储引擎中,只能达到8KB多也就是页大小的一半(可以修改)。

innodb_strict_mode is enabled in the following example to ensure that InnoDB returns an error if the defined columns exceed the InnoDB row size limit. When innodb_strict_mode is disabled (the default), creating a table that uses REDUNDANT or COMPACT row format succeeds with a warning if the InnoDB row size limit is exceeded.

mysql> SET SESSION innodb_strict_mode=1;
mysql> CREATE TABLE t4 (
       c1 CHAR(255),c2 CHAR(255),c3 CHAR(255),
       c4 CHAR(255),c5 CHAR(255),c6 CHAR(255),
       c7 CHAR(255),c8 CHAR(255),c9 CHAR(255),
       c10 CHAR(255),c11 CHAR(255),c12 CHAR(255),
       c13 CHAR(255),c14 CHAR(255),c15 CHAR(255),
       c16 CHAR(255),c17 CHAR(255),c18 CHAR(255),
       c19 CHAR(255),c20 CHAR(255),c21 CHAR(255),
       c22 CHAR(255),c23 CHAR(255),c24 CHAR(255),
       c25 CHAR(255),c26 CHAR(255),c27 CHAR(255),
       c28 CHAR(255),c29 CHAR(255),c30 CHAR(255),
       c31 CHAR(255),c32 CHAR(255),c33 CHAR(255)
       ) ENGINE=InnoDB ROW_FORMAT=COMPACT DEFAULT CHARSET latin1;
ERROR 1118 (42000): Row size too large (> 8126). Changing some columns to TEXT or BLOB or using
ROW_FORMAT=DYNAMIC or ROW_FORMAT=COMPRESSED may help. In current row format, BLOB prefix of 768
bytes is stored inline.

Row Limits总结

Row Limits 一般为 MySQL本身65535B限制和存储引擎8126B限制(默认Innodb)。

加载全部内容

相关教程
猜你喜欢
用户评论