本节介绍对表中列数和单行大小的限制。This section describes limits on the number of columns in tables and the size of individual rows.
MySQL 对每个表有 4096 列的硬限制,但对于给定的表,有效最大值可能会更少。确切的列限制取决于几个因素:MySQL has hard limit of 4096 columns per table, but the effective maximum may be less for a given table. The exact column limit depends on several factors:
表的最大行大小限制了列的数量(可能还有大小),因为所有列的总长度不能超过此大小。请参阅行大小限制。The maximum row size for a table constrains the number (and possibly size) of columns because the total length of all columns cannot exceed this size. See Row Size Limits.
各个列的存储要求限制了适合给定最大行大小的列数。某些数据类型的存储要求取决于存储引擎、存储格式和字符集等因素。请参阅第 13.7 节“数据类型存储要求”。The storage requirements of individual columns constrain the number of columns that fit within a given maximum row size. Storage requirements for some data types depend on factors such as storage engine, storage format, and character set. See Section 13.7, “Data Type Storage Requirements”.
存储引擎可能会施加额外的限制来限制表列数。例如,
InnoDB每个表的列数限制为 1017。请参阅第 17.21 节“InnoDB 限制”。有关其他存储引擎的信息,请参阅第 18 章,替代存储引擎。Storage engines may impose additional restrictions that limit table column count. For example,InnoDBhas a limit of 1017 columns per table. See Section 17.21, “InnoDB Limits”. For information about other storage engines, see Chapter 18, Alternative Storage Engines.功能关键部分(请参阅部分 15.1.15,“CREATE INDEX 语句”)被实现为隐藏的虚拟生成存储列,因此表索引中的每个功能关键部分都计入表总列限制。Functional key parts (see Section 15.1.15, “CREATE INDEX Statement”) are implemented as hidden virtual generated stored columns, so each functional key part in a table index counts against the table total column limit.
给定表的最大行大小由几个因素决定:The maximum row size for a given table is determined by several factors:
MySQL 表的内部表示的最大行大小限制为 65,535 字节,即使存储引擎能够支持更大的行。
BLOB和TEXT列仅对行大小限制贡献 9 到 12 个字节,因为它们的内容与其余内容分开存储The internal representation of a MySQL table has a maximum row size limit of 65,535 bytes, even if the storage engine is capable of supporting larger rows.BLOBandTEXTcolumns only contribute 9 to 12 bytes toward the row size limit because their contents are stored separately from the rest of the row.InnoDB表的最大行大小适用于本地存储在数据库页中的数据,对于 4KB、8KB、16KB 和 32KB,略小于半页innodb_page_size设置。例如,对于默认的 16KBInnoDB页面大小,最大行大小略小于 8KB。对于 64KB 页面,最大行大小略小于 16KB。请参阅第 17.21 节“InnoDB 限制”。The maximum row size for anInnoDBtable, which applies to data stored locally within a database page, is slightly less than half a page for 4KB, 8KB, 16KB, and 32KBinnodb_page_sizesettings. For example, the maximum row size is slightly less than 8KB for the default 16KBInnoDBpage size. For 64KB pages, the maximum row size is slightly less than 16KB. See Section 17.21, “InnoDB Limits”.如果包含可变长度列的行超出了
InnoDB最大行大小,InnoDB选择可变长度列进行外部页外存储,直到该行符合InnoDB行大小限制。对于存储在页外的可变长度列,本地存储的数据量因行格式而异。有关详细信息,请参阅第 17.10 节“InnoDB 行格式”。If a row containing variable-length columns exceeds theInnoDBmaximum row size,InnoDBselects variable-length columns for external off-page storage until the row fits within theInnoDBrow size limit. The amount of data stored locally for variable-length columns that are stored off-page differs by row format. For more information, see Section 17.10, “InnoDB Row Formats”.不同的存储格式使用不同数量的页眉和页尾数据,这会影响行的可用存储量。Different storage formats use different amounts of page header and trailer data, which affects the amount of storage available for rows.
有关
InnoDB行格式,请参阅第 17.10 节,“InnoDB 行格式”。For information aboutInnoDBrow formats, see Section 17.10, “InnoDB Row Formats”.有关
MyISAM存储格式的信息,请参阅第 18.2.3 节“MyISAM 表存储格式”。For information aboutMyISAMstorage formats, see Section 18.2.3, “MyISAM Table Storage Formats”.
行大小限制示例Row Size Limit Examples
下面的
InnoDB和MyISAM示例。无论存储引擎如何,即使存储引擎可能能够支持更大的行,也会强制执行该限制。The MySQL maximum row size limit of 65,535 bytes is demonstrated in the followingInnoDBandMyISAMexamples. The limit is enforced regardless of storage engine, even though the storage engine may be capable of supporting larger rows.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 BLOBsmysql>
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=MyISAM 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在以下
MyISAM示例中,将列更改为TEXT可避免 65,535 字节的行大小限制,并允许操作成功是因为BLOB和TEXT列仅对行大小贡献 9 到 12 个字节。In the followingMyISAMexample, changing a column toTEXTavoids the 65,535-byte row size limit and permits the operation to succeed becauseBLOBandTEXTcolumns only contribute 9 to 12 bytes toward the 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 TEXT(6000)) ENGINE=MyISAM CHARACTER SET latin1;Query OK, 0 rows affected (0.02 sec)该操作对于
InnoDB表会成功,因为将列更改为TEXT避免了 MySQL 65,535 字节的行大小限制,并且InnoDB可变长度列的页外存储避免了InnoDB行大小限制。The operation succeeds for anInnoDBtable because changing a column toTEXTavoids the MySQL 65,535-byte row size limit, andInnoDBoff-page storage of variable-length columns avoids theInnoDBrow size limit.mysql>
CREATE TABLE t (a VARCHAR(10000), b VARCHAR(10000),c VARCHAR(10000), d VARCHAR(10000), e VARCHAR(10000),f VARCHAR(10000), g TEXT(6000)) ENGINE=InnoDB CHARACTER SET latin1;Query OK, 0 rows affected (0.02 sec)可变长度列的存储包括长度字节,这些字节计入行大小。例如,
VARCHAR(255) CHARACTER SET utf8mb3列需要两个字节来存储值的长度,因此每个值最多可以占用 767 个字节。Storage for variable-length columns includes length bytes, which are counted toward the row size. For example, aVARCHAR(255) CHARACTER SET utf8mb3column takes two bytes to store the length of the value, so each value can take up to 767 bytes.创建表
t1的语句成功,因为列需要 32,765 + 2 字节和 32,766 + 2 字节,这在最大行大小 65,535 字节之内:The statement to create tablet1succeeds because the columns require 32,765 + 2 bytes and 32,766 + 2 bytes, which falls within the maximum row size of 65,535 bytes:mysql>
CREATE TABLE t1(c1 VARCHAR(32765) NOT NULL, c2 VARCHAR(32766) NOT NULL)ENGINE = InnoDB CHARACTER SET latin1;Query OK, 0 rows affected (0.02 sec)创建表
t2的语句失败,因为虽然列长度在最大长度 65,535 字节之内,但还需要两个额外字节记录长度,导致行大小超过 65,535 字节:The statement to create tablet2fails because, although the column length is within the maximum length of 65,535 bytes, two additional bytes are required to record the length, which causes the row size to exceed 65,535 bytes:mysql>
CREATE TABLE t2(c1 VARCHAR(65535) NOT NULL)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将列长度减少到 65,533 或更短可以使语句成功。Reducing the column length to 65,533 or less permits the statement to succeed.
mysql>
CREATE TABLE t2(c1 VARCHAR(65533) NOT NULL)ENGINE = InnoDB CHARACTER SET latin1;Query OK, 0 rows affected (0.01 sec)对于
MyISAM表,NULL列需要行中额外的空间来记录它们的值是否为NULL。每个NULL列需要额外一位,向上舍入到最接近的字节。ForMyISAMtables,NULLcolumns require additional space in the row to record whether their values areNULL. EachNULLcolumn takes one bit extra, rounded up to the nearest byte.创建表
t3的语句失败,因为MyISAM还需要NULL列的空间到可变长度列长度字节所需的空间,导致行大小超过 65,535 字节:The statement to create tablet3fails becauseMyISAMrequires space forNULLcolumns in addition to the space required for variable-length column length bytes, causing the row size to exceed 65,535 bytes:mysql>
CREATE TABLE t3(c1 VARCHAR(32765) NULL, c2 VARCHAR(32766) NULL)ENGINE = MyISAM 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有关
InnoDBNULL列存储的信息,请参阅 第 17.10 节,“InnoDB 行格式”。For information aboutInnoDBNULLcolumn storage, see Section 17.10, “InnoDB Row Formats”.InnoDB将行大小(对于数据库页面中本地存储的数据)限制为略小于 4KB、8KB、16KB 和 32KB 的数据库页面的一半innodb_page_size设置,64KB 页面略小于 16KB。InnoDBrestricts row size (for data stored locally within the database page) to slightly less than half a database page for 4KB, 8KB, 16KB, and 32KBinnodb_page_sizesettings, and to slightly less than 16KB for 64KB pages.创建表
t4的语句失败,因为定义的列超出了 16KBInnoDB的行大小限制The statement to create tablet4fails because the defined columns exceed the row size limit for a 16KBInnoDBpage.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=DYNAMIC DEFAULT CHARSET latin1;ERROR 1118 (42000): Row size too large (> 8126). Changing some columns to TEXT or BLOB may help. In current row format, BLOB prefix of 0 bytes is stored inline.